python - 使用 python 将上传进度添加到 Google Drive 脚本
问题描述
这是我的代码:
media_body = MediaFileUpload(file_name , resumable=True)
body = {
'title': file_name,
'description': 'Uploaded By Bardulf'}
file = DRIVE.files().insert( body=body, media_body=media_body, fields='id, alternateLink').execute()
if file:
print(file_name + " uploaded successfully")
prem = DRIVE.permissions().insert( fileId=file.get('id'), body={'type': 'anyone', 'value': 'anyone', 'role': 'reader', 'withLink': True}).execute()
print ("Your sharable link: "+ "https://drive.google.com/file/d/" + file.get('id')+'/view')
我想添加进度条,所以我使用这个文档,将我的代码添加到:
media_body = MediaFileUpload(file_name , resumable=True)
body = {
'title': file_name,
'description': 'Uploaded By Bardulf'}
file = DRIVE.files().insert( body=body, media_body=media_body, fields='id, alternateLink').execute()
if file:
print(file_name + " uploaded successfully")
prem = DRIVE.permissions().insert( fileId=file.get('id'), body={'type': 'anyone', 'value': 'anyone', 'role': 'reader', 'withLink': True})
response = None
while response is None:
status, response = prem.next_chunk()
if status:
print ("Uploaded %d%%." % int(status.progress() * 100))
print ("Your sharable link: "+ "https://drive.google.com/file/d/" + file.get('id')+'/view')
当我运行我的脚本时,我得到:
status, response = prem.next_chunk()
File "/usr/local/lib/python3.5/dist-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper
return wrapped(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/googleapiclient/http.py", line 899, in next_chunk
if self.resumable.size() is None:
AttributeError: 'NoneType' object has no attribute 'size'
我使用 api v2 和 python 3.5
解决方案
您需要监控上传文件的进度,而不是设置权限:
media_body = MediaFileUpload(file_name, resumable=True)
body = {
'title': file_name,
'description': 'Uploaded By Bardulf'}
file = DRIVE.files().insert( body=body, media_body=media_body, fields='id, alternateLink')
response = None
while response is None:
status, response = file.next_chunk()
if status:
print ("Uploaded %d%%." % int(status.progress() * 100))
if file:
print(file_name + " uploaded successfully")
prem = DRIVE.permissions().insert( fileId=response.get('id'), body={'type': 'anyone', 'value': 'anyone', 'role': 'reader', 'withLink': True})
print ("Your sharable link: "+ "https://drive.google.com/file/d/" + response.get('id')+'/view')
execute()
当您使用可恢复媒体时,您还需要删除该杂散。
推荐阅读
- flutter - Dio 改造子库的 Flutter 缓存机制
- r - 将数据框列拆分为多列
- react-native - 如何在android中设置react native expo picker的样式
- excel - 从Excel中的单元格中删除字母
- reactjs - ReactJS 通过 props 传递对象或每次从数据库中读取性能更高
- php - 在 post 方法中将变量传递到另一个页面
- c# - 在带有 WCF 对象的 Visual Studio QuickWatch 中使用 LINQ
- postgresql - 如何从 PostgreSQL 的 if 语句中触发脚本?
- google-apps-script - GAS:如何转换 [hh].mm.ss 字符串
- r - 当月份是 3 个字符变量时,在 R 中转换为日期的最佳方法?