【发布时间】: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