本文整理汇总了Python中apiclient.http.MediaIoBaseUpload方法的典型用法代码示例。如果您正苦于以下问题:Python http.MediaIoBaseUpload方法的具体用法?Python http.MediaIoBaseUpload怎么用?Python http.MediaIoBaseUpload使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类apiclient.http
的用法示例。
在下文中一共展示了http.MediaIoBaseUpload方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: save_image
# 需要导入模块: from apiclient import http [as 别名]
# 或者: from apiclient.http import MediaIoBaseUpload [as 别名]
def save_image(file_name, mime_type, file_data):
drive_api = build_drive_api_v3()
generate_ids_result = drive_api.generateIds(count=1).execute()
file_id = generate_ids_result['ids'][0]
body = {
'id': file_id,
'name': file_name,
'mimeType': mime_type,
}
media_body = MediaIoBaseUpload(file_data,
mimetype=mime_type,
resumable=True)
drive_api.create(body=body,
media_body=media_body,
fields='id,name,mimeType,createdTime,modifiedTime').execute()
return file_id
示例2: store_doc
# 需要导入模块: from apiclient import http [as 别名]
# 或者: from apiclient.http import MediaIoBaseUpload [as 别名]
def store_doc(service, folderId, file_name, crc32, sha256, file_path):
file_metadata = {
'name': file_name,
'mimeType': 'application/vnd.google-apps.document',
'parents': [folderId],
'properties': {
'crc32': str(crc32),
'sha256': str(sha256)
}
}
media = MediaIoBaseUpload(file_path, mimetype='application/vnd.openxmlformats-officedocument.wordprocessingml.document')
service.files().create(body=file_metadata,
media_body=media,
fields = 'id').execute()
# Updates a given fragment
示例3: insert_object
# 需要导入模块: from apiclient import http [as 别名]
# 或者: from apiclient.http import MediaIoBaseUpload [as 别名]
def insert_object(self, object_name, content_type, data):
"""Uploads an object to the bucket.
Args:
object_name (str): Name of the object.
content_type (str): MIME type string of the object.
data (str): The content of the object.
"""
media = MediaIoBaseUpload(StringIO.StringIO(data), mimetype=content_type)
self.service.objects().insert(
bucket=self.bucket_name,
name=object_name,
body={
# This let browsers to download the file instead of opening
# it in a browser.
'contentDisposition':
'attachment; filename=%s' % object_name,
},
media_body=media).execute()
示例4: update_fragment
# 需要导入模块: from apiclient import http [as 别名]
# 或者: from apiclient.http import MediaIoBaseUpload [as 别名]
def update_fragment(service, frag_id, crc32, sha256, file_path):
media = MediaIoBaseUpload(file_path, mimetype='application/vnd.openxmlformats-officedocument.wordprocessingml.document')
file_metadata = {
'properties': {
'crc32': str(crc32),
'sha256': str(sha256)
}
}
service.files().update(
fileId=frag_id,
body=file_metadata,
media_body=media,
fields='id').execute()
#Returns folder id and service object for document insertion into the folder
示例5: _upload
# 需要导入模块: from apiclient import http [as 别名]
# 或者: from apiclient.http import MediaIoBaseUpload [as 别名]
def _upload(self):
with gcs.open(self._file_name, read_buffer_size=self._BUFFER_SIZE) as f:
media = MediaIoBaseUpload(f, mimetype='application/octet-stream',
chunksize=self._BUFFER_SIZE, resumable=True)
request = self._ga_client.management().uploads().uploadData(
accountId=self._account_id,
webPropertyId=self._params['property_id'],
customDataSourceId=self._params['dataset_id'],
media_body=media)
response = None
tries = 0
milestone = 0
while response is None and tries < 5:
try:
status, response = request.next_chunk()
except HttpError, e:
if e.resp.status in [404, 500, 502, 503, 504]:
tries += 1
delay = 5 * 2 ** (tries + random())
self.log_warn('%s, Retrying in %.1f seconds...', e, delay)
time.sleep(delay)
else:
raise WorkerException(e)
else:
tries = 0
if status:
progress = int(status.progress() * 100)
if progress >= milestone:
self.log_info('Uploaded %d%%.', int(status.progress() * 100))
milestone += 20
self.log_info('Upload Complete.')