Python 从Google云存储上bucket内的文件夹获取所有密钥

Python 从Google云存储上bucket内的文件夹获取所有密钥,python,google-cloud-storage,Python,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 i

我需要一些帮助,从谷歌云存储的一个存储桶中的文件夹中获取所有对象(密钥)。目前,我正在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>
这是src_uri:gs://basebucket/user2
这是关键:
这是关键:
这是关键:
这是关键:
它将返回bucket中的全部密钥列表。虽然可以手动过滤掉属于其他用户的密钥,但这并不能扩展,而且肯定有更好的方法可以做到这一点。如果您有这方面的经验,请告诉我。

如果您查看了,则需要传递
前缀
分隔符
参数

但是,我建议使用。像这样的方法应该会奏效:

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

谢谢你的帮助。事实证明,我需要一个额外的调用get_bucket(),以及一个前缀字符串来实现这一点:“keys_for_userid=src_uri.get_bucket().get_all_keys(prefix=userid)”“我相信你的方法也可以,我没有尝试过,但它是有意义的。”