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