本文整理汇总了Python中apiclient.http.MediaIoBaseDownload类的典型用法代码示例。如果您正苦于以下问题:Python MediaIoBaseDownload类的具体用法?Python MediaIoBaseDownload怎么用?Python MediaIoBaseDownload使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了MediaIoBaseDownload类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
def main():
"""Shows basic usage of the Google Drive API.
Creates a Google Drive API service object and outputs the names and IDs
for up to 10 files.
"""
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('drive', 'v3', http=http)
#file_id = '1cxgbJZKnysKOKBDg-ZbV1E3S4B-iAG7XY-1x7U8Yfsg' # For the grocery doc
file_id = '18r3cUWKbMaWVYtNKLJjxZFHB2m7y1QJdkSPlrU197PA' # For the test doc
request = service.files().export_media(fileId=file_id, mimeType='text/plain')
response = request.execute()
fh = open(FILE_LOC, 'wb')
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print("Download %.2f%%." % (status.progress() * 100.0))
fh.close()
# Now read doc.txt to get information
find_date()
# Upload the file back
update_request = service.files().update(fileId=file_id, media_body=FILE_LOC)
update_response = update_request.execute()
示例2: download
def download(argv):
bucket_name, object_name = argv[1][5:].split('/', 1)
filename = argv[2]
assert bucket_name and object_name
service = get_authenticated_service(RO_SCOPE)
print 'Building download request...'
f = file(filename, 'w')
request = service.objects().get_media(bucket=bucket_name,
object=object_name)
media = MediaIoBaseDownload(f, request, chunksize=CHUNKSIZE)
print 'Downloading bucket: %s object: %s to file: %s' % (bucket_name,
object_name,
filename)
progressless_iters = 0
done = False
while not done:
error = None
try:
progress, done = media.next_chunk()
if progress:
print_with_carriage_return(
'Download %d%%.' % int(progress.progress() * 100))
except HttpError, err:
error = err
if err.resp.status < 500:
raise
except RETRYABLE_ERRORS, err:
error = err
示例3: download_file
def download_file(service, filename):
result = []
result = retrieve_all_files(service)
for i in result :
if i['title'] == filename:
tmp = i['id']
break
f = open (filename, 'wb')
request = service.files().get_media(fileId=tmp)
media_request = MediaIoBaseDownload(f, request)
while True:
try:
download_progress, done = media_request.next_chunk()
except errors.HttpError, error:
print 'An error occurred: %s' % error
return
#if download_progress:
# print 'Download Progress: %d%%' % int(download_progress.progress() * 100)
if done:
print 'Google download complete'
return
示例4: download_file
def download_file(self,file_id, file_name):
"""Download a Drive file's content to the local filesystem.
Args:
service: Drive API Service instance.
file_id: ID of the Drive file that will downloaded.
file_name: used as name for to write content in.
"""
fd = open(file_name,'w+')
request = self._service.files().get_media(fileId=file_id)
media_request = MediaIoBaseDownload(fd, request)
while True:
try:
download_progress, done = media_request.next_chunk()
except:
print('An error occurred')
fd.close()
return
if download_progress:
print('Download Progress: %d%%' % int(download_progress.progress() * 100))
if done:
print('Download Complete')
fd.close()
return
示例5: get
def get(service):
try:
file_name = raw_input('Which filename to download from cloud?\n')
req = service.objects().get(bucket=_BUCKET_NAME,object=file_name,)
resp = req.execute()
print json.dumps(resp, indent=2)
req = service.objects().get_media(bucket=_BUCKET_NAME,object=file_name,)
fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, req, chunksize=1024*1024)
done = False
while not done:
status, done = downloader.next_chunk()
if status:
print 'Download %d%%.' % int(status.progress() * 100)
print 'Download Complete!'
reader = csv.reader(open('dict_pw.csv', 'rb'))
newD = dict(x for x in reader)
key= newD[file_name]
print key
decrypt_file(fh.getvalue(),key,file_name)
except client.AccessTokenRefreshError:
print ("Error in the credentials")
示例6: File
def File(gs_path, chunk_size=CHUNK_SIZE):
"""Download a file from the cloud, and return a file that's readable.
Args:
gs_path: Fully qualified gfs path, eg, 'gfs://bucket/path/to/FILE'.
chunk_size: The chunk_size to download, defaults to CHUNK_SIZE.
Returns:
An IO stream to be read.
"""
bucket_name, object_name = gs_path[5:].split('/', 1)
logging.info('Downloading file: %s/%s', bucket_name, object_name)
credentials = GoogleCredentials.get_application_default()
service = discovery.build('storage', 'v1', credentials=credentials)
request = service.objects().get_media(bucket=bucket_name, object=object_name)
output = StringIO.StringIO()
media = MediaIoBaseDownload(output, request, chunksize=chunk_size)
done = False
while not done:
try:
_, done = media.next_chunk()
except HttpError, err:
if err.resp.status < 500:
raise
except RETRYABLE_ERRORS, err:
pass
示例7: test_media_io_base_download
def test_media_io_base_download(self):
self.request.http = HttpMockSequence([
({'status': '200',
'content-range': '0-2/5'}, '123'),
({'status': '200',
'content-range': '3-4/5'}, '45'),
])
download = MediaIoBaseDownload(
fd=self.fd, request=self.request, chunksize=3)
self.assertEqual(self.fd, download._fd)
self.assertEqual(3, download._chunksize)
self.assertEqual(0, download._progress)
self.assertEqual(None, download._total_size)
self.assertEqual(False, download._done)
self.assertEqual(self.request.uri, download._uri)
status, done = download.next_chunk()
self.assertEqual(self.fd.getvalue(), '123')
self.assertEqual(False, done)
self.assertEqual(3, download._progress)
self.assertEqual(5, download._total_size)
self.assertEqual(3, status.resumable_progress)
status, done = download.next_chunk()
self.assertEqual(self.fd.getvalue(), '12345')
self.assertEqual(True, done)
self.assertEqual(5, download._progress)
self.assertEqual(5, download._total_size)
示例8: scan_item_contents
def scan_item_contents(item):
if item.get('mimType') == self.FOLDER_MIME:
return
shared = item.get('shared', None)
if shared is None or shared:
return
# only process files
file_id = item.get('id', None)
name = item.get('name', None)
available = True
if file_id and name:
f_path = create_temp_name(temp_dir, name)
logger.warning("Processing file {}...{}".format(name, f_path))
f = open(f_path, 'wb')
try:
request = self.client.files().get_media(fileId=file_id)
downloader = MediaIoBaseDownload(f, request)
done = False
while done is False:
status, done = downloader.next_chunk()
except Exception as ex:
logger.error("Unable to download file {}. {}".format(name, ex))
available = False
f.close()
if available:
try:
matches.extend(search_content(f_path, expressions))
except Exception as ex:
logger.error("Unable to parse content in file {}. {}".format(name, ex))
try:
os.remove(f_path)
except Exception as ex:
logger.error("Unable to clean up temprary file {}. {}".format(f_path, ex))
示例9: download
def download(self, folder_service_callback):
uploadFolder = self.helper.get_fileid_by_name(helper.UPLOADFOLDER)
# get newest file download url
downloadInfo = self.helper.get_newest_file_down_info(uploadFolder)
progressless_iters = 0
# download file
print('Downloading latest backup ...')
filename = folder_service_callback.getTempFolder() + downloadInfo['title']
fh = io.FileIO(filename, 'wb')
downloader = MediaIoBaseDownload(fh, downloadInfo['request'])
done = False
while not done:
error = None
try:
status, done = downloader.next_chunk()
print("Download %d%%." % int(status.progress() * 100), end="\r")
except HttpError as err:
error = err
if err.resp.status < 500:
raise
except self.RETRYABLE_ERRORS as err:
error = err
if error:
progressless_iters += 1
self.handle_progressless_iter(error, progressless_iters)
else:
progressless_iters = 0
return filename
示例10: test_media_io_base_download
def test_media_io_base_download(self):
self.request.http = HttpMockSequence(
[({"status": "200", "content-range": "0-2/5"}, "123"), ({"status": "200", "content-range": "3-4/5"}, "45")]
)
download = MediaIoBaseDownload(fh=self.fh, request=self.request, chunksize=3)
self.assertEqual(self.fh, download.fh_)
self.assertEqual(3, download.chunksize_)
self.assertEqual(0, download.progress_)
self.assertEqual(None, download.total_size_)
self.assertEqual(False, download.done_)
self.assertEqual(self.request.uri, download.uri_)
status, done = download.next_chunk()
self.assertEqual(self.fh.getvalue(), "123")
self.assertEqual(False, done)
self.assertEqual(3, download.progress_)
self.assertEqual(5, download.total_size_)
self.assertEqual(3, status.resumable_progress)
status, done = download.next_chunk()
self.assertEqual(self.fh.getvalue(), "12345")
self.assertEqual(True, done)
self.assertEqual(5, download.progress_)
self.assertEqual(5, download.total_size_)
示例11: get
def get(self, remote_path, local_path, report_to=None):
LOG.debug("Downloading %s from cloud storage (local path: %s)", remote_path, local_path)
bucket, name = self._parse_url(remote_path)
local_path = os.path.join(local_path, os.path.basename(remote_path))
request = self.cloudstorage.objects().get_media(bucket=bucket, object=name)
f = open(local_path, "w")
try:
media = MediaIoBaseDownload(f, request, chunksize=self.chunk_size)
last_progress = 0
done = False
while not done:
status, done = media.next_chunk()
if status:
percentage = int(status.progress() * 100)
if percentage - last_progress >= self.report_interval:
if report_to:
report_to(status.resumable_progress, status.total_size)
last_progress = percentage
finally:
f.close()
LOG.debug("Finished downloading %s", os.path.basename(local_path))
return local_path
示例12: processObject
def processObject(client, itemname):
try:
#try to access json object in Google Compute Storage
# Get Payload Data
req = client.objects().get_media(
bucket = errorsBucket,
object=itemname)
#store info whether a json-object exists in the bucket or not
fileExists = True
try:
resp = req.execute()
except:
fileExists = False
pass
#continue only when the object exists
if (fileExists):
# The BytesIO object may be replaced with any io.Base instance.
fh = io.BytesIO()
#prepare for reading a json-object
downloader = MediaIoBaseDownload(fh, req, chunksize=1024*1024)
done = False
while not done:
status, done = downloader.next_chunk()
#print (fh.getvalue())
#load accessed json-object into dictionary
jsonFile = fh.getvalue()
#print(jsonFile)
jf = open("../" + errorsBucket + "/" + itemname.encode(), 'w')
jf.write(jsonFile)
jf.close()
#store error message into respective errors bucket
except oauth2_client.AccessTokenRefreshError:
pass
示例13: retrieve_content
def retrieve_content(
self, 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
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 = data.getvalue()
return result if transform is None else transform(result)
示例14: processObject
def processObject(client, itemname):
try:
#try to access json object in Google Compute Storage
# Get Payload Data
req = client.objects().get_media(
bucket = comm.jsonsDir,
object=itemname)
#store info whether a json-object exists in the bucket or not
fileExists = True
try:
resp = req.execute()
except:
fileExists = False
pass
#continue only when the object exists
if (fileExists):
# The BytesIO object may be replaced with any io.Base instance.
fh = io.BytesIO()
#prepare for reading a json-object
downloader = MediaIoBaseDownload(fh, req, chunksize=1024*1024)
done = False
while not done:
status, done = downloader.next_chunk()
#load accessed json-object into dictionary
jsonToDict = json.loads(fh.getvalue())#json.loads(fh.getvalue())#return value
#print ("RETURNED VALUE: " + jsonToDict)
doDownloadJob(jsonToDict, itemname)
#store error message into respective errors bucket
except oauth2_client.AccessTokenRefreshError:
comm.printException(comm.pathToSaveDownloadErrors, errString="False credentials")
pass
示例15: main
def main(argv):
#print (argv)
#print(argv["object"])
#print(argv["bucket"])
_BUCKET_NAME = argv["bucket"]
_FILE1_NAME = argv["object"]
http = httplib2.Http()
token_uri = '%s/%s/token' % (METADATA_SERVER, SERVICE_ACCOUNT)
resp, content = http.request(token_uri, method='GET',
body=None,
headers={'Metadata-Flavor': 'Google'})
if resp.status == 200:
d = json.loads(content)
access_token = d['access_token'] # Save the access token
credentials = oauth2_client.AccessTokenCredentials(access_token, 'my-user-agent/1.0')
client = api_discovery.build('storage', _API_VERSION, http=credentials.authorize(http))
try:
# Get Metadata
req = client.objects().get(
bucket=_BUCKET_NAME,
object=_FILE1_NAME) # optional
fileExists = True
try:
resp = req.execute()
except HttpError:
fileExists = False
print (str(fileExists))
except:
fileExists = False
print (str(fileExists))
if (fileExists):
# Get Payload Data
req = client.objects().get_media(
bucket=_BUCKET_NAME,
object=_FILE1_NAME) # optional
# The BytesIO object may be replaced with any io.Base instance.
fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, req, chunksize=1024*1024)
done = False
while not done:
status, done = downloader.next_chunk()
returnValue = json.loads(fh.getvalue())#return value
#print ("RETURNED VALUE: " + returnValue)
''''''
print ("STR ")
print (type(returnValue) is str)
print ("DICT ")
print (type(returnValue) is dict)
print ("LIST ")
print (type(returnValue) is list)
except oauth2_client.AccessTokenRefreshError:
print ("False credentials")
else:
print (str(False) + str(resp.status))