0
我想弄清楚如何在files().insert()
方法中指定media_body
方法来自缓冲区(甚至是文件类对象),而不是保存临时文件,然后使用MediaFileUpload
插入Google Drive,然后,然后删除临时文件。在Python中从缓冲区插入Google Drive API文件
有没有人有这个运气,可以指出我在正确的方向吗?
我想弄清楚如何在files().insert()
方法中指定media_body
方法来自缓冲区(甚至是文件类对象),而不是保存临时文件,然后使用MediaFileUpload
插入Google Drive,然后,然后删除临时文件。在Python中从缓冲区插入Google Drive API文件
有没有人有这个运气,可以指出我在正确的方向吗?
我已经使用AppEngine上这个代码从缓冲上传:
def insert_file(service, title, description, parent_id, mime_type, filebody):
"""Insert new file and deletes old file, if with same title.
Args:
service: Drive API service instance.
title: Title of the file to insert, including the extension.
description: Description of the file to insert.
parent_id: Parent folder's ID.
mime_type: MIME type of the file to insert.
filebody: string, Bytes of body content.
filename: Filename of the file to insert.
Returns:
Inserted file metadata if successful, None otherwise.
"""
urlfetch.set_default_fetch_deadline(45)
# check if file is already there
param = {}
param['q'] = "title = '"+title+"'"
filelist = service.files().list(**param).execute()
if filelist:
for f in filelist['items']:
file_id = f['id']
# logging.info("file: %s - %s" % (f['title'], file_id))
# here we could delete the file, skip trash
service.files().delete(fileId=file_id).execute()
media_body = MediaInMemoryUpload(filebody, mime_type)
body = {
'title': title,
'description': description,
'mimeType': mime_type
}
# Set the parent folder.
if parent_id:
body['parents'] = [{'id': parent_id}]
try:
file = service.files().insert(
body=body,
media_body=media_body).execute()
return file
except errors.HttpError, error:
logging.info('An error occured: %s' % error)
return None