python - 从 Google Cloud Storage 上的存储桶内的文件夹中获取所有 key

标签 python google-cloud-storage

我需要一些帮助来从 Google Cloud Storage 上的存储桶内的文件夹中获取所有对象(键)。目前,我正在 Python 中执行以下命令:

GOOGLE_STORAGE = 'gs'
src_uri = boto.storage_uri(base_bucket_name + '/' + userid, GOOGLE_STORAGE)

print 'this is the src_uri: %s' % src_uri

for key in src_uri.get_all_keys():
  print 'this is the key: %s' % key

它返回:

this is the src_uri: gs://basebucket/user2
this is the key: <Key: basebucket,user1/key1>
this is the key: <Key: basebucket,user1/key2>
this is the key: <Key: basebucket,user1/key3>
this is the key: <Key: basebucket,user1/key4>

它返回存储桶中的整个键列表。尽管可以手动过滤掉属于其他用户的 key ,但这无法扩展,而且肯定有更好的方法来做到这一点。如果您有这方面的经验,请告诉我。

最佳答案

如果你看一下documentation for get_all_keys ,您需要传递 prefixdelimiter 参数。

但是,我建议使用 list function反而。像这样的东西应该有效:

bucket_uri = boto.storage_uri(base_bucket_name, GOOGLE_STORAGE)
for object_uri in bucket_uri.list_bucket(prefix='/%s' % userid, delimiter='/'):
    print object_uri

关于python - 从 Google Cloud Storage 上的存储桶内的文件夹中获取所有 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25026407/

相关文章:

ios - CloudStorageTools::serve() 音频无法在 iOS 上播放

google-cloud-storage - 如何将 Google Cloud Storage Bucket 从一个项目复制到另一个项目?

python - Scrapy-splash response.css() 获取不到元素

python - 从随机矩阵生成样本

python - 了解 Python 中的 scikitlearn PCA.transform 函数

typescript - 如何使用 Firebase Firestore 在客户端上缓存 getDownloadUrl() 调用?

javascript - 谷歌云存储 Javascript 使用

python - 云端的 Scrapy

python - Python运行程序的热插拔

php - Google Cloud SDK 安装程序无法在 Windows 上完成组件安装