Skip to main content
 首页 » 编程设计

python之使用 Cloud Shell 查看 Cloud Storage 中的文件

2024年10月01日8zhwl

我刚开始使用 Google Cloud Platform,希望从存储桶中读取文件,进行一些转换,然后最终将转换后的数据保存回 Cloud Storage。截至目前,我无法读取该数据,甚至无法查看文件名。我能否深入了解如何最好地读取和转换存储桶中的数据?

这是我的代码:

from google.cloud import storage 
from google.cloud.storage import Blob 
 
bucket_name = 'test_bucket' 
file_name = '*.txt' 
client = storage.Client() 
 
bucket = client.get_bucket(bucket_name) 
 
for f in bucket: 
    print(f) 

TypeError: 'Bucket' 对象不可迭代

blob = bucket.get_blob(file_name) 
print(blob) 

无

print(blob.download_as_string()) 

AttributeError: 'NoneType' 对象没有属性 'download_as_string'

请您参考如下方法:

你想要:

bucket = client.get_bucket(bucket) 
for f in bucket.list_blobs(): 
  print(f) 

供引用,文档位于:https://googlecloudplatform.github.io/google-cloud-python/latest/storage/buckets.html

您不能使用像“*.txt”这样的通配符。您可以设置前缀,但如果您想查找以“.txt”结尾的内容,则必须遍历所有内容并自行过滤。