本文整理汇总了Python中apiclient.http.MediaIoBaseDownload方法的典型用法代码示例。如果您正苦于以下问题:Python http.MediaIoBaseDownload方法的具体用法?Python http.MediaIoBaseDownload怎么用?Python http.MediaIoBaseDownload使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类apiclient.http
的用法示例。
在下文中一共展示了http.MediaIoBaseDownload方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _download
# 需要导入模块: from apiclient import http [as 别名]
# 或者: from apiclient.http import MediaIoBaseDownload [as 别名]
def _download(self, filename=None, _fileType="spreadsheet"):
fileTypes = {
"csv": "text/csv",
"xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"ods": "application/x-vnd.oasis.opendocument.spreadsheet",
"pdf": "application/pdf",
"zip": "application/zip", # a zip file of html files
"tsv": "text/tab-separated-values",
}
if filename is None:
filename = _makeFilenameSafe(self._title) + "." + _fileType
request = DRIVE_SERVICE.files().export_media(fileId=self._spreadsheetId, mimeType=fileTypes[_fileType])
fh = open(filename, "wb")
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
return filename
示例2: view_file
# 需要导入模块: from apiclient import http [as 别名]
# 或者: from apiclient.http import MediaIoBaseDownload [as 别名]
def view_file(file_id):
drive_api = build_drive_api_v3()
metadata = drive_api.get(fields="name,mimeType", fileId=file_id).execute()
request = drive_api.get_media(fileId=file_id)
fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
fh.seek(0)
return flask.send_file(
fh,
attachment_filename=metadata['name'],
mimetype=metadata['mimeType']
)
示例3: download_file
# 需要导入模块: from apiclient import http [as 别名]
# 或者: from apiclient.http import MediaIoBaseDownload [as 别名]
def download_file(service, file_id, location, filename):
request = service.files().get_media(fileId=file_id)
fh = io.FileIO('{}{}'.format(location, filename), 'wb')
downloader = MediaIoBaseDownload(fh, request,chunksize=1024*1024)
done = False
while done is False:
status, done = downloader.next_chunk()
if status:
#print '\rDownload {}%.'.format(int(status.progress() * 100)),
print int(status.progress() * 100)," percent complete \r",
#sys.stdout.flush()
print ""
print colored(('%s downloaded!' % filename), 'green')
示例4: download_file
# 需要导入模块: from apiclient import http [as 别名]
# 或者: from apiclient.http import MediaIoBaseDownload [as 别名]
def download_file(self, file_name, file_id):
request = self._service.files().get_media(fileId=file_id)
fh = io.FileIO(file_name, 'wb')
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
logger.debug("Download %d%%." % int(status.progress() * 100))
示例5: retrieve_content
# 需要导入模块: from apiclient import http [as 别名]
# 或者: from apiclient.http import MediaIoBaseDownload [as 别名]
def retrieve_content(
self, context, bucket, path, transform=None, generation=None, **kwargs):
"""Retrieves the content at the specified path.
Args:
bucket: [string] The bucket to retrieve front.
path: [string] The path to the content to retrieve from the bucket.
generation: [long] Specifies version of object (or None for current).
transform: [callable(string)] transform the downloaded bytes into
something else (e.g. a JSON object). If None then the identity.
Returns:
transformed object.
"""
self.logger.info('Retrieving path=%s from bucket=%s [generation=%s]',
path, bucket, generation)
# Get Payload Data
bucket = context.eval(bucket)
path = context.eval(path)
generation = context.eval(generation)
request = self.service.objects().get_media(
bucket=bucket,
object=path,
generation=generation,
**kwargs)
data = io.BytesIO()
downloader = MediaIoBaseDownload(data, request, chunksize=1024*1024)
done = False
while not done:
status, done = downloader.next_chunk()
if status:
self.logger.debug('Download %d%%', int(status.progress() * 100))
result = bytes.decode(data.getvalue())
return result if transform is None else transform(result)
示例6: download_file_as
# 需要导入模块: from apiclient import http [as 别名]
# 或者: from apiclient.http import MediaIoBaseDownload [as 别名]
def download_file_as(service, file_id, media_type, file_name):
request = service.files().export_media(fileId=file_id, mimeType=media_type)
fh = io.FileIO(file_name, mode='wb')
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print("Download %d%%." % int(status.progress() * 100))
示例7: get_image_bytes_from_doc
# 需要导入模块: from apiclient import http [as 别名]
# 或者: from apiclient.http import MediaIoBaseDownload [as 别名]
def get_image_bytes_from_doc(service, file):
# Download file to memory
request = service.files().export_media(fileId=file['id'], mimeType='application/vnd.openxmlformats-officedocument.wordprocessingml.document')
fh = BytesIO()
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
# Extract image from file and return the image's bytes
zipRef = zipfile.ZipFile(fh, 'r')
imgBytes = zipRef.read('word/media/image1.png')
zipRef.close()
return BytesIO(imgBytes)