Python googleapiclient.http() Examples

The following are 10 code examples of googleapiclient.http(). 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 googleapiclient , or try the search function .
Example #1
Source File: GoogleVault.py    From content with MIT License 5 votes vote down vote up
def connect():
    creds = get_credentials()
    try:
        service = build('vault', 'v1', http=creds.authorize(Http(disable_ssl_certificate_validation=(not USE_SSL))))
    except Exception as e:
        LOG('There was an error creating the Vault service in the \'connect\' function.')
        err_msg = 'There was an error creating the Vault service - {}'.format(str(e))
        return_error(err_msg)
    return service 
Example #2
Source File: GoogleVault.py    From content with MIT License 5 votes vote down vote up
def download_storage_object(object_ID, bucket_name):
    service = connect_to_storage()
    req = service.objects().get_media(bucket=bucket_name, object=object_ID)  # pylint: disable=no-member
    out_file = io.BytesIO()
    downloader = googleapiclient.http.MediaIoBaseDownload(out_file, req)
    done = False
    while not done:
        done = downloader.next_chunk()[1]
    return out_file 
Example #3
Source File: GoogleVault.py    From content with MIT License 5 votes vote down vote up
def connect_to_storage():
    try:
        creds = get_storage_credentials()
        ptth = authorized_http(creds)
        ptth.disable_ssl_certificate_validation = (not USE_SSL)
        service = build('storage', 'v1', http=ptth)
    except Exception as e:
        LOG('There was an error creating the Storage service in the \'connect_to_storage\' function.')
        err_msg = 'There was an error creating the Storage service - {}'.format(str(e))
        return_error(err_msg)
    return service 
Example #4
Source File: main.py    From python-docs-samples with Apache License 2.0 5 votes vote down vote up
def upload_object(self, bucket, file_object):
        body = {
            'name': 'storage-api-client-sample-file.txt',
        }
        req = storage.objects().insert(
            bucket=bucket, body=body,
            media_body=googleapiclient.http.MediaIoBaseUpload(
                file_object, 'application/octet-stream'))
        resp = req.execute()
        return resp 
Example #5
Source File: crud_object.py    From python-docs-samples with Apache License 2.0 5 votes vote down vote up
def create_service():
    # Construct the service object for interacting with the Cloud Storage API -
    # the 'storage' service, at version 'v1'.
    # You can browse other available api services and versions here:
    #     http://g.co/dv/api-client-library/python/apis/
    return googleapiclient.discovery.build('storage', 'v1') 
Example #6
Source File: crud_object.py    From python-docs-samples with Apache License 2.0 5 votes vote down vote up
def upload_object(bucket, filename, readers, owners):
    service = create_service()

    # This is the request body as specified:
    # http://g.co/cloud/storage/docs/json_api/v1/objects/insert#request
    body = {
        'name': filename,
    }

    # If specified, create the access control objects and add them to the
    # request body
    if readers or owners:
        body['acl'] = []

    for r in readers:
        body['acl'].append({
            'entity': 'user-%s' % r,
            'role': 'READER',
            'email': r
        })
    for o in owners:
        body['acl'].append({
            'entity': 'user-%s' % o,
            'role': 'OWNER',
            'email': o
        })

    # Now insert them into the specified bucket as a media insertion.
    # http://g.co/dv/resources/api-libraries/documentation/storage/v1/python/latest/storage_v1.objects.html#insert
    with open(filename, 'rb') as f:
        req = service.objects().insert(
            bucket=bucket, body=body,
            # You can also just set media_body=filename, but for the sake of
            # demonstration, pass in the more generic file handle, which could
            # very well be a StringIO or similar.
            media_body=googleapiclient.http.MediaIoBaseUpload(
                f, 'application/octet-stream'))
        resp = req.execute()

    return resp 
Example #7
Source File: crud_object.py    From python-docs-samples with Apache License 2.0 5 votes vote down vote up
def get_object(bucket, filename, out_file):
    service = create_service()

    # Use get_media instead of get to get the actual contents of the object.
    # http://g.co/dv/resources/api-libraries/documentation/storage/v1/python/latest/storage_v1.objects.html#get_media
    req = service.objects().get_media(bucket=bucket, object=filename)

    downloader = googleapiclient.http.MediaIoBaseDownload(out_file, req)

    done = False
    while done is False:
        status, done = downloader.next_chunk()
        print("Download {}%.".format(int(status.progress() * 100)))

    return out_file 
Example #8
Source File: gbcommon.py    From addon-hassiogooglebackup with MIT License 5 votes vote down vote up
def getDriveService(user_agent):

    with open(TOKEN) as f:
        creds = json.load(f)

    credentials = GoogleCredentials(None,creds["client_id"],creds["client_secret"],
                                          creds["refresh_token"],None,"https://accounts.google.com/o/oauth2/token",user_agent)
    http = credentials.authorize(Http())
    credentials.refresh(http)
    drive_service = build('drive', 'v3', http)

    return drive_service 
Example #9
Source File: gbcommon.py    From addon-hassiogooglebackup with MIT License 5 votes vote down vote up
def backupFile(fileName, backupDirID, drive_service, MIMETYPE, TITLE, DESCRIPTION):

    logging.info("Backing up " + fileName + " to " + backupDirID)

    logging.debug("drive_service = " + str(drive_service))
    logging.debug("MIMETYPE = " + MIMETYPE)
    logging.debug("TITLE = " + TITLE)
    logging.debug("DESCRIPTION = " + DESCRIPTION)

    shortFileName = ntpath.basename(fileName)

    media_body = googleapiclient.http.MediaFileUpload(
        fileName,
        mimetype=MIMETYPE,
        resumable=True
    )

    logging.debug("media_body: " + str(media_body))

    body = {
        'name': shortFileName,
        'title': TITLE,
        'description': DESCRIPTION,
        'parents': [backupDirID]
    }

    new_file = drive_service.files().create(
    body=body, media_body=media_body).execute()
    logging.debug(pformat(new_file)) 
Example #10
Source File: transfer.py    From google-drive-recursive-ownership with MIT License 5 votes vote down vote up
def get_drive_service():
    OAUTH2_SCOPE = 'https://www.googleapis.com/auth/drive'
    CLIENT_SECRETS = 'client_secrets.json'
    flow = oauth2client.client.flow_from_clientsecrets(CLIENT_SECRETS, OAUTH2_SCOPE)
    flow.redirect_uri = oauth2client.client.OOB_CALLBACK_URN
    authorize_url = flow.step1_get_authorize_url()
    print('Use this link for authorization: {}'.format(authorize_url))
    code = six.moves.input('Verification code: ').strip()
    credentials = flow.step2_exchange(code)
    http = httplib2.Http()
    credentials.authorize(http)
    drive_service = googleapiclient.discovery.build('drive', 'v2', http=http)
    return drive_service