[英]Not able to upload_from_filename files into GCP bucket using python script and GCP service account cred
• Installed Python 3.7.2 • Created GCP service account and given owner role to it, also enabled storage API and created a cloud storage bucket • Now I'm trying to upload files to GCP cloud storage folder using python script but I couldn't. •安装了Python 3.7.2•创建了GCP服务帐户并为其赋予了所有者角色,还启用了存储API并创建了一个云存储桶•现在,我正在尝试使用python脚本将文件上传到GCP云存储文件夹,但是我无法。 But, by using the same structure, I'm able to create new cloud storage bucket and able to edit existing files in it • Here with have attached pythonscript 但是,通过使用相同的结构,我能够创建新的云存储桶并能够在其中存储现有文件•此处附带了pythonscript
Ref used: https://googleapis.github.io/google-cloud-python/latest/storage/blobs.html https://cloud.google.com/storage/docs/uploading-objects#storage-upload-object-python 使用的参考: https : //googleapis.github.io/google-cloud-python/latest/storage/blobs.html https://cloud.google.com/storage/docs/uploading-objects#storage-upload-object-蟒蛇
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)
I was able to run your code and debug it. 我能够运行你的代码并进行调试。 I will put what I used below and explain the changes I made. 我将把我在下面使用的内容解释并解释我所做的更改。
As you did, I put my service account as Owner and was able to upload. 正如您所做的那样,我将我的服务帐户设置为所有者,并且能够上传。 I recommend following the best practices of least privileges when your done testing. 我建议您在完成测试后遵循最少特权的最佳做法。
client.create_bucket
since buckets are unique we shouldn't be hard coding bucket names to create. 我删除了client.create_bucket
因为存储桶是唯一的,我们不应该硬编码要创建的存储桶名称。 You can come up with a naming convention for your needs, however, for testing I removed it. 您可以根据需要提出命名约定,但是,为了测试,我删除了它。 I fixed the variable destination_blob_name
since you were using it as a folder for the file to be placed. 我修改了变量destination_blob_name
因为您将其用作要放置的文件的文件夹。 This would not work as GCS does not use folders, it instead just uses file names. 这不起作用,因为GCS不使用文件夹,而只是使用文件名。 What was happening is that you were actually "converting" your TXT files into a folder named 'test'. 发生的事情是你实际上将你的TXT文件“转换”到名为'test'的文件夹中。 For a better understanding, I recommend looking through the documentation on How Sub-directories Work . 为了更好地理解,我建议查看有关子目录如何工作的文档。
from google.cloud import storage bucket_name='bucket-test-18698335' source_file_name='./hello.txt' destination_blob_name='test/hello.txt' def upload_blob(bucket_name, source_file_name, destination_blob_name): """Uploads a file to the bucket.""" client = storage.Client.from_service_account_json('./test.json') bucket = client.get_bucket(bucket_name) blob = bucket.blob(destination_blob_name) blob.upload_from_filename(source_file_name) print('File {} uploaded to {}.'.format( source_file_name, destination_blob_name)) if __name__ == '__main__': upload_blob(bucket_name, source_file_name, destination_blob_name)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.