使用Cloud Shell查看云存储中的文件

问题描述 投票:2回答:2

我一直在研究谷歌云平台,我希望从桶中读取文件,进行一些转换,然后最终将转换后的数据保存回云存储。截至目前,我无法读取数据并查看文件名。我能否从如何最好地读取和转换数据桶中的数据?

这是我的代码:

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' object is not iterable

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

None

print(blob.download_as_string())

AttributeError: 'NoneType' object has no attribute 'download_as_string'

python google-cloud-storage
2个回答
2
投票

你要:

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

供参考,文档位于:qazxsw poi

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


0
投票

如果在调用bucket.get_blob(filename)之后检查返回的对象类型以确保不会发生NoneType错误会更好:

https://googlecloudplatform.github.io/google-cloud-python/latest/storage/buckets.html
© www.soinside.com 2019 - 2024. All rights reserved.