【问题标题】:Using Google Service Account to Resumable Upload Videos使用 Google 服务帐号恢复上传视频
【发布时间】:2020-10-09 20:41:56
【问题描述】:

我正在使用 Google 服务帐户通过 Resumable Method 将视频上传到 Google Drive。 python 代码运行良好,但我遇到了 Google 服务帐户存储问题。

Google 服务帐户似乎只能拥有 15 GB 的存储空间。即使我将视频上传到常规的 Google Drive 文件夹,该视频仍归服务帐户所有。因此,我尝试将视频的所有者转移到其他帐户,但没有成功,错误为bad request. User message: \"You can't yet change the owner of this item. (We're working on it.)

下面是我的 python 代码,它从服务帐户生成访问令牌并执行可恢复上传

    credentials = ServiceAccountCredentials.from_json_keyfile_name(
        'creds.json',
        scopes='https://www.googleapis.com/auth/drive'
    )

    delegated_credentials = credentials.create_delegated('service_account_email')

    access_token = delegated_credentials.get_access_token().access_token
    
    filesize = os.path.getsize(file_location)

    # Retrieve session for resumable upload.

    headers1 = {"Authorization": "Bearer " + access_token, "Content-Type": "application/json"}
    params = {
        "name": file_name,
        "mimeType": "video/mp4",
        "parents": [folder_id]
    }
    r = requests.post(
        "https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable",
        headers=headers1,
        data=json.dumps(params)
    )

    location = r.headers['Location']

    # Upload the file.

    headers2 = {"Content-Range": "bytes 0-" + str(filesize - 1) + "/" + str(filesize)}
    r = requests.put(
        location,
        headers=headers2,
        data=open(file_location, 'rb')
    )

是否有解决方法或增加 Google 服务帐户的存储限制?

任何建议将不胜感激。谢谢!

【问题讨论】:

  • 我不认为这个可以增加。但是,您为什么要使用服务帐户?也许您想使用域范围的委派并模拟一个普通帐户?
  • 嗨@lamblichus,我不确定是否有任何其他方法可以为可恢复上传方法生成访问令牌。这就是我从服务帐户生成访问令牌并使用它将视频上传到 Google 云端硬盘的原因。
  • 我不确定您所说的I'm not sure if there is any other way to generate the access token for the Resumable Upload Method 是什么意思,但如果您只是使用服务帐户来关注2-legged OAuth workflow,您可以使用 SA 来模拟一个足够多的用户驱动存储。这样,SA 存储将不受限制,因为文件将直接上传到用户的驱动器。如果您认为此解决方法适合您,我会发布答案。
  • 谢谢,如果您能发布解决方案,那将非常有帮助。我将发布我的代码,以便您可以看到我在做什么。
  • 您能否提供更多有关I'm not sure if there is any other way to generate the access token for the Resumable Upload Method 的详细信息?您是否尝试使用普通帐户上传文件?你遇到了什么问题?另外,您是否已经使用过域范围的委派? (您在代码中使用create_delegated,用于代表其他帐户行事)。另外,我认为您使用的是deprecated library,最好使用this one

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


【解决方案1】:
  • 您想使用服务帐户进行可恢复上传到云端硬盘。
  • 您希望视频的所有者不是服务帐户,而是具有足够云端硬盘存储容量的普通帐户。

如果这是正确的,那么您只需将域范围的权限委派给服务帐户,以便它可以代表域中的任何用户行事,并在上传文件时模拟您想要成为的帐户文件的所有者。

委派全域权限:

授予域范围权限的过程解释here

  • Service accounts page 上,选择您的服务帐户,在编辑 SA 时,单击 SHOW DOMAIN-WIDE DELEGATION,然后在刚刚显示的内容上,选中选项启用 G Suite 域范围委派.
  • 完成此操作后,转到Admin console,然后转到主菜单 > 安全 > API 控制
  • 域范围委派窗格中选择管理域范围委派,然后单击新增
  • 填写相应字段:(1)在Client ID中,输入SA的Client ID,您可以在凭证JSON文件和Service中找到它帐户页面,以及 (2) 在 OAuth 范围 中,添加与您希望 SA 代表域中用户访问的资源相对应的范围。在这种情况下,我想这只是https://www.googleapis.com/auth/drive
  • 点击授权后,您已授予服务帐户代表域中任何用户访问资源的能力。

冒充其他用户:

现在服务帐户可以模拟域中的任何用户,但您必须指定您希望它模拟的用户。为此,您只需对代码进行一些小改动。现在,您在通过create_delegated 委派凭据时设置service_account_email

delegated_credentials = credentials.create_delegated('service_account_email')

也就是说,服务帐户代表服务帐户行事。如果您不想冒充另一个帐户,则不需要这行代码(它没有任何作用,因为credentialsdelegated_credentials 都指同一个帐户(服务帐户) .

但由于您想使用服务帐户代表另一个帐户行事,因此您必须在此行指定此其他帐户的电子邮件地址:

delegated_credentials = credentials.create_delegated('user_account_email')

这是您需要对代码进行的唯一更改。如果您已授予域范围委派,则服务帐户将像其他用户一样行事。就像是这个其他用户上传了文件一样,所以这个用户将是文件的所有者。

注意:

  • 您正在使用deprecated library (oauth2client)。由于这仍然有效,因此现在不需要这样做,但请考虑将您的代码更改为 google-auth

参考:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-02
    • 2016-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-06
    相关资源
    最近更新 更多