【问题标题】:How to upload file into specific folder with Google Drive API and Python?如何使用 Google Drive API 和 Python 将文件上传到特定文件夹?
【发布时间】:2020-08-15 02:56:43
【问题描述】:

我正在将 jpg 文件上传到我的 Google Drive 帐户。它工作正常,但我需要它上传到特定文件夹,但不确定如何在元数据中设置 parents 参数。

这是我的代码:

data = {"file": open(filedirectory, 'rb').read(), "title" : filename, "parents" : [{"id": "<folderid>"}]}
drive_url = "https://www.googleapis.com/upload/drive/v3/files?uploadType=media"
drive_r = requests.post(drive_url, data=data, headers={"Authorization": "Bearer " + access_token, "Content-type": "image/jpeg"})

【问题讨论】:

    标签: python google-api google-drive-api


    【解决方案1】:

    我相信你的目标如下。

    • 您想将文件上传到 Google 云端硬盘中的特定文件夹。
    • 您想使用 python 的requests 来实现此目的。

    修改点:

    • uploadType=media为例,官方文档是这样说的。

      简单上传(uploadType=media)。使用此上传类型可快速传输小型媒体文件(5 MB 或更少),而无需提供元数据。要执行简单上传,请参阅执行简单上传。

    • 所以为了上传文件内容和文件元数据,请使用uploadType=multipart

    • 此外,在您的端点中,使用了 Drive API v3。但是"parents" : [{"id": "&lt;folderid&gt;"}] 是针对 Drive API v2 的。也需要修改。

    当你的脚本修改为uploadType=multipart时,它变成如下。

    修改后的脚本:

    使用本脚本时,请设置filedirectory, filename, folderid, access_token的变量。

    import json
    import requests
    
    filedirectory = '###'
    filename = '###'
    folderid = '###'
    access_token = '###'
    
    metadata = {
        "name": filename,
        "parents": [folderid]
    }
    files = {
        'data': ('metadata', json.dumps(metadata), 'application/json'),
        'file': open(filedirectory, "rb").read()  # or  open(filedirectory, "rb")
    }
    r = requests.post(
        "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart",
        headers={"Authorization": "Bearer " + access_token},
        files=files
    )
    print(r.text)
    

    注意:

    • 此修改后的脚本假定您的访问令牌可用于将文件上传到 Google Drive。

    参考资料:

    【讨论】:

    • 非常感谢您的快速回答!我看到了文档的多部分部分,但真的很困惑。也感谢你为我写出这个例子,因为我很难找到一些东西来作为我的代码的基础。
    • @tammi 感谢您的回复。很高兴您的问题得到解决。
    • 但问题是访问令牌过期了。有没有办法让它永久化?
    • @ThuggyTM 关于您的问题,您可以将其作为新问题发布吗?因为我认为您的问题对其他用户有用。如果你能与之合作,我很高兴。可以合作吗?
    【解决方案2】:

    似乎我已经能够使用这段代码让 Javascript SDK 做出反应

                                    gapi.client.drive.files.create({
                                        name: 'multapart.jpg',   //OPTIONAL
                                        uploadType: 'multipart',
                                    },{body:'your content here',content:'your content here'})
    

    其中媒体是图像的字节表示

    在 chronium edge 中它抱怨说

    在 'https://content.googleapis.com/drive/v3/files?name=resumable.jpg&body=%EF%BF%BD%EF%....%BD&uploadType=multipart&alt=json&key= 访问 XMLHttpRequest [你看不到这个哈哈]' 来自原点“https://content.googleapis.com” 已被 CORS 策略阻止:跨源请求仅支持以下协议方案:http、data、chrome-extension、edge、https、chrome-untrusted。

    在 edge legacy 中,它说它格式不正确,当我执行 uploadType=media 时,它说使用的是 https://content.googleapis.com/drive/v3/files 而不是 https://content.googleapis.com/upload/drive/v3/files,所以 JS SDK 不可靠并且充满了错误,很高兴我得到了它反应,如果只有有人能找到逻辑给它正确的 URL,因为我相信 js SDK 没有错误,谷歌不希望 ppl 使用它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-26
      • 1970-01-01
      • 1970-01-01
      • 2023-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-20
      相关资源
      最近更新 更多