Python apiclient.http.MediaIoBaseDownload() Examples

The following are 7 code examples of apiclient.http.MediaIoBaseDownload(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module apiclient.http , or try the search function .
Example #1
Source File: __init__.py    From ezsheets with GNU General Public License v3.0 6 votes vote down vote up
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 
Example #2
Source File: google_drive.py    From google-authentication-with-python-and-flask with MIT License 6 votes vote down vote up
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']
               ) 
Example #3
Source File: download.py    From gdrivedownloader with MIT License 5 votes vote down vote up
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') 
Example #4
Source File: google_drive.py    From Packt-Publishing-Free-Learning with MIT License 5 votes vote down vote up
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)) 
Example #5
Source File: gcp_storage_agent.py    From citest with Apache License 2.0 5 votes vote down vote up
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) 
Example #6
Source File: main.py    From DriveInvoicing with Apache License 2.0 5 votes vote down vote up
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)) 
Example #7
Source File: drive_api.py    From InfiniDrive with GNU General Public License v3.0 5 votes vote down vote up
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)