V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
artms
V2EX  ›  程序员

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

  •  
  •   artms · 2021-01-23 08:06:19 +08:00 · 3213 次点击
    这是一个创建于 1182 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

    求教各路大神 给点思路~

    16 条回复    2021-01-25 14:55:05 +08:00
    favourstreet
        1
    favourstreet  
       2021-01-23 08:16:27 +08:00 via Android
    onedrive 有一套 oauth 认证的 graph api,可以以分片的方式上传,api 和一般对象存储的分片上传有点不一样,但基本用法是一样的
    skys215
        2
    skys215  
       2021-01-23 08:32:08 +08:00
    “像腾讯云 cos 那样” 是指怎么样?
    mm2x
        3
    mm2x  
       2021-01-23 08:35:37 +08:00
    onderive 不是有 API 的吗?可以申请啊。
    zqcolor
        4
    zqcolor  
       2021-01-23 08:36:17 +08:00
    'onedrive 有一套 oauth 认证的 graph api' 几年前用过,当时这个挺难用,分片部分好像我们自己实现的,不知道现在号线没有, 但是好像 app 还要注册一下,然后每次要登录一下才能用
    artms
        5
    artms  
    OP
       2021-01-23 08:39:30 +08:00
    @zqcolor 这么看起来还挺麻烦的。
    Ranying
        6
    Ranying  
       2021-01-23 08:43:42 +08:00
    不如传到后端,然后方法就多了起来
    MaiKuraki
        7
    MaiKuraki  
       2021-01-23 08:53:49 +08:00
    artms
        8
    artms  
    OP
       2021-01-23 09:07:17 +08:00
    @MaiKuraki 这个是第二步。现在第一步要做的是解决上传。。。
    Shazoo
        9
    Shazoo  
       2021-01-23 09:28:06 +08:00
    用 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  
       2021-01-23 09:31:37 +08:00   ❤️ 1
    @artms 最近刚做了个 OneDrive 当 Typora 图床的小工具。上面的是其中代码,C 程序员写 python,凑合看吧。

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

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

    1drv.ws 这个服务很不错,可是貌似有缓存,我这里出现几次跳转链接不更新了。所以还是自建比较好。
    stille
        11
    stille  
       2021-01-23 11:07:52 +08:00
    用 rclone 上传吧
    Winter1sComing
        12
    Winter1sComing  
       2021-01-23 11:56:06 +08:00   ❤️ 1
    IDAEngine
        13
    IDAEngine  
       2021-01-23 13:04:19 +08:00
    rclone
    artms
        14
    artms  
    OP
       2021-01-23 18:29:38 +08:00
    @Shazoo 多谢,这正是我要干的~ 有心了!
    artms
        15
    artms  
    OP
       2021-01-23 18:30:37 +08:00
    baobao1270
        16
    baobao1270  
       2021-01-25 14:55:05 +08:00 via Android
    oneindex?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5459 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 03:12 · PVG 11:12 · LAX 20:12 · JFK 23:12
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.