V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
artms
V2EX  ›  程序员

有没有能把 onedrive 当成 cos 用的代码

  •  
  •   artms · Jan 23, 2021 · 4139 views
    This topic created in 1926 days ago, the information mentioned may be changed or developed.

    最近在做个项目,想尝试下 用 web 客户端把用户 post 的视频文件直接存到 youtube 或者 onedrive.

    youtube 的 api 好像不行了,现在只有 onedrive.. 但是 onedrive 怎么才能像腾讯云 cos 那样保存上去呢?

    求教各路大神 给点思路~

    16 replies    2021-01-25 14:55:05 +08:00
    favourstreet
        1
    favourstreet  
       Jan 23, 2021 via Android
    onedrive 有一套 oauth 认证的 graph api,可以以分片的方式上传,api 和一般对象存储的分片上传有点不一样,但基本用法是一样的
    skys215
        2
    skys215  
       Jan 23, 2021
    “像腾讯云 cos 那样” 是指怎么样?
    mm2x
        3
    mm2x  
       Jan 23, 2021
    onderive 不是有 API 的吗?可以申请啊。
    zqcolor
        4
    zqcolor  
       Jan 23, 2021
    'onedrive 有一套 oauth 认证的 graph api' 几年前用过,当时这个挺难用,分片部分好像我们自己实现的,不知道现在号线没有, 但是好像 app 还要注册一下,然后每次要登录一下才能用
    artms
        5
    artms  
    OP
       Jan 23, 2021
    @zqcolor 这么看起来还挺麻烦的。
    Ranying
        6
    Ranying  
       Jan 23, 2021
    不如传到后端,然后方法就多了起来
    MaiKuraki
        7
    MaiKuraki  
       Jan 23, 2021
    artms
        8
    artms  
    OP
       Jan 23, 2021
    @MaiKuraki 这个是第二步。现在第一步要做的是解决上传。。。
    Shazoo
        9
    Shazoo  
       Jan 23, 2021
    用 Graphic API 。

    ``` python
    def get_upload_session_url(path, filename):
    url = f"https://graph.microsoft.com/v1.0/me/drive/root:/{config['root_path']}/{path}/{filename}:/createUploadSession"
    payload = {
    "item" : {
    "@microsoft.graph.conflictBehavior": "replace",
    "name": filename
    }
    }
    resp = r.post(url, headers=add_token_header({'Content-Type': 'application/json'}), data=json.dumps(payload))
    return json.loads(resp.content)['uploadUrl']

    def upload_file_body(upload_url, localfile):
    f = open(localfile, 'rb')
    data = f.read()
    headers = {
    'Content-Length':f'{len(data)}',
    'Content-Range': f'bytes 0-{len(data)-1}/{len(data)}'
    }
    resp = r.put(upload_url, data=data, headers=headers)
    f.close()
    return json.loads(resp.content)['id']
    ```

    上传的核心代码。是适合大文件的那种。
    Shazoo
        10
    Shazoo  
       Jan 23, 2021   ❤️ 1
    @artms 最近刚做了个 OneDrive 当 Typora 图床的小工具。上面的是其中代码,C 程序员写 python,凑合看吧。

    Graphic API 麻烦的是认证授权部分。建议用 code 方案,最简单。

    直连方案,可能需要服务器配合(因为直连链接是几次 302 和 url 替换,其中有部分不可描述了,我是用香港服务器搞定的)

    1drv.ws 这个服务很不错,可是貌似有缓存,我这里出现几次跳转链接不更新了。所以还是自建比较好。
    stille
        11
    stille  
       Jan 23, 2021
    用 rclone 上传吧
    Winter1sComing
        12
    Winter1sComing  
       Jan 23, 2021   ❤️ 1
    IDAEngine
        13
    IDAEngine  
       Jan 23, 2021
    rclone
    artms
        14
    artms  
    OP
       Jan 23, 2021
    @Shazoo 多谢,这正是我要干的~ 有心了!
    artms
        15
    artms  
    OP
       Jan 23, 2021
    baobao1270
        16
    baobao1270  
       Jan 25, 2021 via Android
    oneindex?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2432 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 87ms · UTC 08:59 · PVG 16:59 · LAX 01:59 · JFK 04:59
    ♥ Do have faith in what you're doing.