Python 从google bucket下载文件
我在google bucket上下载文件时遇到问题 我遵循了这一点(使用客户库和服务帐户) 以下是我使用的python代码(来自google示例): 导入json 从httplib2导入Http 从oauth2client.client导入SignedJwtAssertionCredentials 从apiclient.discovery导入生成Python 从google bucket下载文件,python,api,google-cloud-storage,storage,oauth2client,Python,Api,Google Cloud Storage,Storage,Oauth2client,我在google bucket上下载文件时遇到问题 我遵循了这一点(使用客户库和服务帐户) 以下是我使用的python代码(来自google示例): 导入json 从httplib2导入Http 从oauth2client.client导入SignedJwtAssertionCredentials 从apiclient.discovery导入生成 client_email = 'myclientname@myproject.iam.gserviceaccount.com' json_file
client_email = 'myclientname@myproject.iam.gserviceaccount.com'
json_file = 'resources/Google/myproject-anumber.json' ## <---- JSON provided when i created the service account
cloud_storage_bucket = 'mybucketname'
report_to_download = 'path/to/my/file.zip'
private_key = json.loads(open(json_file).read())['private_key']
credentials = SignedJwtAssertionCredentials(client_email, private_key,
'https://www.googleapis.com/auth/devstorage.read_only')
storage = build('storage', 'v1', http=credentials.authorize(Http()))
report = storage.objects().get(bucket = cloud_storage_bucket, object =
report_to_download).execute()
client\u email='1〕myclientname@myproject.iam.gserviceaccount.com'
json_file='resources/Google/myproject anumber.json'###您可以使用下载一个对象。我建议按照以下步骤安装云存储客户端库并设置身份验证
下面是基于上述信息下载对象的Python代码示例
from google.cloud import storage
if __name__ == '__main__':
bucket_name = 'your_bucket'
source_blob_name = 'your_object'
destination_file_name = 'local_file'
#DOWNLOAD
storage_client = storage.Client()
bucket = storage_client.get_bucket(bucket_name)
blob = bucket.blob(source_blob_name)
blob.download_to_filename(destination_file_name)
print('Blob {} downloaded to {}.'.format(source_blob_name, destination_file_name))
您可以使用下载对象。我建议按照以下步骤安装云存储客户端库并设置身份验证
下面是基于上述信息下载对象的Python代码示例
from google.cloud import storage
if __name__ == '__main__':
bucket_name = 'your_bucket'
source_blob_name = 'your_object'
destination_file_name = 'local_file'
#DOWNLOAD
storage_client = storage.Client()
bucket = storage_client.get_bucket(bucket_name)
blob = bucket.blob(source_blob_name)
blob.download_to_filename(destination_file_name)
print('Blob {} downloaded to {}.'.format(source_blob_name, destination_file_name))
这意味着所描述的方法已被弃用?我不想使用gsutilThatget
方法返回对象元数据。如果你想下载一个对象,你应该使用,我正在用一个代码示例编辑我的答案,用Python客户端库下载一个对象!我想我可以再次下载这个文件查询API,这意味着所描述的方法不推荐了?我不想使用gsutilThatget
方法返回对象元数据。如果你想下载一个对象,你应该使用,我正在用一个代码示例编辑我的答案,用Python客户端库下载一个对象!我想我可以再下载一次这个文件