简体   繁体   English

无法使用python脚本和GCP服务帐户信用将upload_from_filename文件上传到GCP存储桶

[英]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. 我建议您在完成测试后遵循最少特权的最佳做法。

  1. I removed 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. 您可以根据需要提出命名约定,但是,为了测试,我删除了它。
  2. 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.

相关问题 Python GCP-将文件上传到 GCP 存储桶 - Python GCP- Upload Files to GCP Bucket 如何从 GCP 中的 python 脚本将文件上传到 GCS? - How to upload files to GCS from a python script in GCP? 使用 python 从 GCP 存储桶中递归读取所有子文件夹中的 csv 个文件 - Read csv files recursively in all sub folders from a GCP bucket using python 如何在upload_from_filename() 中设置元数据而不使用patch() 方法? - How to set the metadata in upload_from_filename() without using patch() method? AttributeError'NoneType'对象没有属性'upload_from_filename' - AttributeError 'NoneType' object has no attribute 'upload_from_filename' Python 脚本,用于自动化 GCP 存储上传 - Python script for automating GCP storage UPLOAD 如何将文件夹从 gcp 存储桶文件夹复制到另一个存储桶文件夹并使用 python 将其删除 - how to copy folders from a gcp bucket folder into another bucket folder and delete it using python 如何使用python将文件从本地写入GCP - How to write files from local to GCP using python GCP服务帐户密钥轮换 - GCP Service Account Key Rotation gcp - 如何在没有密钥文件的情况下将 Python 应用程序作为服务帐户运行 - gcp - how to run Python application as Service Account without a key file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM