本文整理汇总了Python中apiclient.http.MediaFileUpload._mimetype方法的典型用法代码示例。如果您正苦于以下问题:Python MediaFileUpload._mimetype方法的具体用法?Python MediaFileUpload._mimetype怎么用?Python MediaFileUpload._mimetype使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类apiclient.http.MediaFileUpload
的用法示例。
在下文中一共展示了MediaFileUpload._mimetype方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: uploadFile
# 需要导入模块: from apiclient.http import MediaFileUpload [as 别名]
# 或者: from apiclient.http.MediaFileUpload import _mimetype [as 别名]
def uploadFile(self, path, filename, parentId, fileId=None):
# body = {'title': filename, 'mimeType': mimetype, 'description': filename}
body = {'title': filename, 'description': filename}
body['parents'] = [{'id': parentId}]
filepath = os.path.join(path, filename)
# mbody = MediaFileUpload(filepath, mimetype=mimetype, resumable=True)
mbody = MediaFileUpload(filepath, resumable=True)
if mbody._mimetype is None: mbody._mimetype = 'application/octet-stream'
if fileId is None:
fileObj = self.service.files().insert(
body=body, media_body=mbody).execute()
else:
fileObj = self.service.files().update(
fileId=fileId, body=body, media_body=mbody).execute()
return (fileObj['id'], fileObj)
示例2: update
# 需要导入模块: from apiclient.http import MediaFileUpload [as 别名]
# 或者: from apiclient.http.MediaFileUpload import _mimetype [as 别名]
def update(self, new_path=None, parent_id='root'):
try:
if not hasattr(self, 'id'):
return self.create(parent_id)
existing_file = self.get_file(self.id)
if new_path is not None:
self.path = new_path
mime_type = defaul_mime_type
media_body = None
if not os.path.isdir(self.path):
media_body = MediaFileUpload(self.path, resumable=True)
if media_body.size() == 0:
logger.error('cannot update no content file %s', self.path)
return None
if media_body.mimetype() is not None:
mime_type = media_body.mimetype()
else:
media_body._mimetype = mime_type
else:
mime_type = folder_mime_type
existing_file['title'] = os.path.basename(self.path)
existing_file['parents'] = [{'id': parent_id}]
existing_file['mimeType'] = mime_type
logger.info('updated %s', self.path)
with drive.lock:
metadata = drive.service.files().update(
fileId=self.id,
body=existing_file,
media_body=media_body).execute()
self.id = metadata['id']
if metadata.has_key('downloadUrl'):
self.download_url = metadata['downloadUrl']
if metadata.has_key('md5Checksum'):
self.md5Checksum = metadata['md5Checksum']
return metadata
except errors.HttpError, error:
logger.error('an error occurred: %s', error)
return None
示例3: create
# 需要导入模块: from apiclient.http import MediaFileUpload [as 别名]
# 或者: from apiclient.http.MediaFileUpload import _mimetype [as 别名]
def create(self, parent_id='root'):
mime_type = defaul_mime_type
media_body = None
if not os.path.isdir(self.path):
media_body = MediaFileUpload(self.path, resumable=True)
if media_body.size() == 0:
logger.error('cannot create no content file %s', self.path)
return None
if media_body.mimetype() is not None:
mime_type = media_body.mimetype()
else:
media_body._mimetype = mime_type
else:
mime_type = folder_mime_type
body = {
'title': os.path.basename(self.path),
'mimeType': mime_type,
'parents': [{'id': parent_id}]
}
try:
with drive.lock:
metadata = drive.service.files().insert(
body=body,
media_body=media_body).execute()
logger.info('created %s, %s', self.path, body['mimeType'])
self.id = metadata['id']
if metadata.has_key('downloadUrl'):
self.download_url = metadata['downloadUrl']
if metadata.has_key('md5Checksum'):
self.md5Checksum = metadata['md5Checksum']
return metadata
except errors.HttpError, error:
logger.error('an error occurred: %s', error)
return None