我在Google存储桶上下载文件时遇到问题 我遵循了这个Google tutorial(使用客户库和服务帐户)
这是我使用的python代码(来自google示例): 导入json 从httplib2导入Http 从oauth2client.client导入SignedJwtAssertionCredentials 从apiclient.discovery导入构建中
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()
我以为我必须将'report'输出到文件中,但是没有,如此处所述,report只是一个dict对象:
然后我尝试使用selfLink属性或mediaLink,但没有成功。与“ https://cloud.google.com/storage/docs/json_api/v1/objects/get”相同 也会返回ACL
先谢谢您
答案 0 :(得分:0)
您可以使用code shown here下载对象。我建议遵循this document安装Cloud Storage客户端库并设置身份验证。
下面是一个基于上述信息的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))