本文整理汇总了Python中googleapiclient.http方法的典型用法代码示例。如果您正苦于以下问题:Python googleapiclient.http方法的具体用法?Python googleapiclient.http怎么用?Python googleapiclient.http使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类googleapiclient
的用法示例。
在下文中一共展示了googleapiclient.http方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: connect
# 需要导入模块: import googleapiclient [as 别名]
# 或者: from googleapiclient import http [as 别名]
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
示例2: download_storage_object
# 需要导入模块: import googleapiclient [as 别名]
# 或者: from googleapiclient import http [as 别名]
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
示例3: connect_to_storage
# 需要导入模块: import googleapiclient [as 别名]
# 或者: from googleapiclient import http [as 别名]
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
示例4: upload_object
# 需要导入模块: import googleapiclient [as 别名]
# 或者: from googleapiclient import http [as 别名]
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
示例5: create_service
# 需要导入模块: import googleapiclient [as 别名]
# 或者: from googleapiclient import http [as 别名]
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')
示例6: upload_object
# 需要导入模块: import googleapiclient [as 别名]
# 或者: from googleapiclient import http [as 别名]
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
示例7: get_object
# 需要导入模块: import googleapiclient [as 别名]
# 或者: from googleapiclient import http [as 别名]
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
示例8: getDriveService
# 需要导入模块: import googleapiclient [as 别名]
# 或者: from googleapiclient import http [as 别名]
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
示例9: backupFile
# 需要导入模块: import googleapiclient [as 别名]
# 或者: from googleapiclient import http [as 别名]
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))
示例10: get_drive_service
# 需要导入模块: import googleapiclient [as 别名]
# 或者: from googleapiclient import http [as 别名]
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