Google 开车 Python API;上传不是文件的媒体对象
Google Drive Python API; upload a media object which is NOT a file
我可以使用 MediaUpload
[=] this example 中提供的 python API 将文件上传到 Google 驱动器 class。
但我需要上传一个动态创建的文件,我不想保存它并再次打开。
目前还没有这样的实现。 This guide says I need to create a subclass of MediaUpload 并且必须完全实现 MediaUpload
接口。
我检查了代码,它真的很混乱。如果有人已经实现了它或者可以帮助我,请分享代码。
谢谢
回答我自己的问题
我想做的是从 url 获取文件并上传到驱动器。
使用 MediaIoBaseUpload
class 而不是 MediaUpload
class.
response = urllib2.urlopen(url)
fh = BytesIO(response.read())
media_body = MediaIoBaseUpload(fh, mimetype='image/jpeg',
chunksize=1024*1024, resumable=True)
body = {
'title': 'pic.jpg'
}
drive_service.files().insert(body=body, media_body=media_body).execute()
对于那些正在寻找上传文件方式的人不是通过 url,而是使用表单数据或 object/string 中的任何媒体[=] 30=]字节,这里是示例代码。
以下情况是,我使用 formdata 从我的前端应用程序发送一个 docx 文件并在后端接收它。现在这个文件,我想上传到google驱动器。
我使用的技术堆栈是 Angular 8 前端和 Tornado Python 后端。
后端Python代码:
import io
from googleapiclient.discovery import build
from googleapiclient.http import MediaIoBaseUpload
from gdrive_config import credentials # Import your credentials object created using a service account
# Refer this link for more info - https://github.com/googleapis/google-api-python-client/blob/master/docs/oauth-server.md
drive_service = build("drive", "v3", credentials=credentials, cache_discovery=False)
file = self.request.files
file_info = file["my_docx_file"][0] # Here "my_docx_file" is the key name you have set in form data
def upload_file(file_info):
file_name = file_info["filename"]
file_metadata = {
"name": file_name,
"mimeType": "application/vnd.openxmlformats-officedocument.wordprocessingml.document", # Mimetype for docx
}
media = MediaIoBaseUpload(io.BytesIO(file_info["body"]), # **Pass your bytes object/string here
mimetype="application/vnd.google-apps.document", # I'm converting docx to native google docs
resumable=True)
file = drive_service.files().create(body=file_metadata,
media_body=media,
fields="id, name").execute()
print("Uploaded File '{}' with ID: {}".format(file.get("name"), file.get("id")))
参考链接:
服务帐户的 Oauth 流程 - https://github.com/googleapis/google-api-python-client/blob/master/docs/oauth-server.md
Google 驱动器 python 客户端文档 - https://developers.google.com/resources/api-libraries/documentation/drive/v3/python/latest/drive_v3.files.html
Google 驱动上传文件指南 - https://developers.google.com/drive/api/v3/manage-uploads
我可以使用 MediaUpload
[=] this example 中提供的 python API 将文件上传到 Google 驱动器 class。
但我需要上传一个动态创建的文件,我不想保存它并再次打开。
目前还没有这样的实现。 This guide says I need to create a subclass of MediaUpload 并且必须完全实现 MediaUpload
接口。
我检查了代码,它真的很混乱。如果有人已经实现了它或者可以帮助我,请分享代码。
谢谢
回答我自己的问题
我想做的是从 url 获取文件并上传到驱动器。
使用 MediaIoBaseUpload
class 而不是 MediaUpload
class.
response = urllib2.urlopen(url)
fh = BytesIO(response.read())
media_body = MediaIoBaseUpload(fh, mimetype='image/jpeg',
chunksize=1024*1024, resumable=True)
body = {
'title': 'pic.jpg'
}
drive_service.files().insert(body=body, media_body=media_body).execute()
对于那些正在寻找上传文件方式的人不是通过 url,而是使用表单数据或 object/string 中的任何媒体[=] 30=]字节,这里是示例代码。
以下情况是,我使用 formdata 从我的前端应用程序发送一个 docx 文件并在后端接收它。现在这个文件,我想上传到google驱动器。
我使用的技术堆栈是 Angular 8 前端和 Tornado Python 后端。
后端Python代码:
import io
from googleapiclient.discovery import build
from googleapiclient.http import MediaIoBaseUpload
from gdrive_config import credentials # Import your credentials object created using a service account
# Refer this link for more info - https://github.com/googleapis/google-api-python-client/blob/master/docs/oauth-server.md
drive_service = build("drive", "v3", credentials=credentials, cache_discovery=False)
file = self.request.files
file_info = file["my_docx_file"][0] # Here "my_docx_file" is the key name you have set in form data
def upload_file(file_info):
file_name = file_info["filename"]
file_metadata = {
"name": file_name,
"mimeType": "application/vnd.openxmlformats-officedocument.wordprocessingml.document", # Mimetype for docx
}
media = MediaIoBaseUpload(io.BytesIO(file_info["body"]), # **Pass your bytes object/string here
mimetype="application/vnd.google-apps.document", # I'm converting docx to native google docs
resumable=True)
file = drive_service.files().create(body=file_metadata,
media_body=media,
fields="id, name").execute()
print("Uploaded File '{}' with ID: {}".format(file.get("name"), file.get("id")))
参考链接:
服务帐户的 Oauth 流程 - https://github.com/googleapis/google-api-python-client/blob/master/docs/oauth-server.md
Google 驱动器 python 客户端文档 - https://developers.google.com/resources/api-libraries/documentation/drive/v3/python/latest/drive_v3.files.html
Google 驱动上传文件指南 - https://developers.google.com/drive/api/v3/manage-uploads