【发布时间】:2019-08-05 05:57:26
【问题描述】:
• 已安装 Python 3.7.2 • 创建 GCP 服务帐户并为其赋予所有者角色,还启用存储 API 并创建云存储桶 • 现在我正在尝试使用 python 脚本将文件上传到 GCP 云存储文件夹,但我不能。但是,通过使用相同的结构,我能够创建新的云存储桶并能够编辑其中的现有文件 • 这里附上了pythonscript
参考使用: https://googleapis.github.io/google-cloud-python/latest/storage/blobs.html https://cloud.google.com/storage/docs/uploading-objects#storage-upload-object-python
from google.cloud import storage
bucket_name='buckettest'
source_file_name='D:/file.txt'
source_file_name1='D:/jenkins structure.png'
destination_blob_name='test/'
def upload_blob(bucket_name, source_file_name, destination_blob_name):
"""Uploads a file to the bucket."""
client = storage.Client.from_service_account_json('D:\gmailseviceaccount.json')
bucket = client.create_bucket('bucketcreate')
bucket = client.get_bucket(bucket_name)
blob = bucket.blob(destination_blob_name)
blob.upload_from_filename(source_file_name)
blob.upload_from_filename(source_file_name1)
print('File {} uploaded to {}.'.format(
source_file_name,
destination_blob_name))
if __name__ == '__main__':
upload_blob(bucket_name, source_file_name, destination_blob_name)
【问题讨论】:
-
你好@Yasho R,欢迎来到 StackOverflow!您的服务帐号授予了哪些角色?让他们运行this command 并使用结果编辑问题。
-
错误信息是什么(堆栈跟踪)?
标签: python google-cloud-platform cloud storage