来自Google云端存储中存储区内文件夹的get_all_keys

时间:2014-07-29 23:10:13

标签: python google-cloud-storage

我需要一些帮助,以便从Google云端存储中的文件夹中获取所有对象(键)。目前,我在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>

它返回桶中的整个密钥列表。虽然可以手动过滤掉属于其他用户的密钥,但这并不能扩展,并且肯定有更好的方法可以做到这一点。如果您对此有任何经验,请与我们联系。

1 个答案:

答案 0 :(得分:1)

如果您查看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