我需要一些帮助来从 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 ,您需要传递 prefix
和 delimiter
参数。
但是,我建议使用 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/