从Firebase存储库中如何下载文件

3
我一直在尝试从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))

我看到的问题是,我得到了存储桶上所有的结果,但我只需要选择路径上的那些结果。
我做错了什么?还有其他方法可以实现我需要的吗?
谢谢您的帮助。
2个回答

1

You should try :

 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)

谢谢,它帮助我澄清了一些事情。 - Juan Esteban Ladron De Guevara

0
在您的示例中,存储桶名称只是“myapp.appspot.com”。Cloud Storage 存储桶名称中没有路径组件。一个存储桶不包含其他存储桶,只有具有不同路径的文件。您需要从存储桶名称中去掉路径,并将该字符串提供给list API以获取指定前缀的文件。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接