gpt4 book ai didi

python-3.x - 如何从 Firebase 存储下载文件

转载 作者:行者123 更新时间:2023-12-04 00:06:52 31 4
gpt4 key购买 nike

我一直在尝试从 Firebase 存储中下载一些文件。现在我正在尝试列出特定路径中的所有文件,如下所示:

from google.cloud import storage


client = storage.Client()
bucket = client.get_bucket('myapp.appspot.com/path/to/folder')
blob = list(bucket.list_blobs())
print(len(blob))

我看到的问题是,我正在获取存储桶中的所有结果,而我只需要我选择的路径中的结果。

我做错了什么?还有其他方法可以实现我的需要吗?

感谢您的帮助。

最佳答案

你应该试试:

 from google.cloud import storage
from google.cloud.storage import Blob
storage_client = storage.Client()

# get all the buckets
buckets = storage_client.list_buckets()
for bucket in buckets:
print(bucket.name)
# output bucket_name

# get all the blobs in a bucket
bucket = client.get_bucket("bucket_name")
blobs = list(bucket.list_blobs())
for blob in blobs:
print (blob)
# print the blobs: blob_name

#check if the blob exists
assert isinstance(bucket.get_blob('blob_name'), Blob)

#get the blob from path
my_blob = Blob.from_string("gs://bucket_name/blob_name")

# List the files in a folder
files = bucket.list_blobs(prefix='folder_name')
for f in files:
print(f.name)

关于python-3.x - 如何从 Firebase 存储下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58673023/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com