<p><strong>更新</strong>:下面是针对Python的旧“Google API客户端库”的,但如果不使用该客户端,则更喜欢针对Python的新“Google Cloud客户端库”(<a href="https://googleapis.dev/python/storage/latest/index.html" rel="nofollow noreferrer">https://googleapis.dev/python/storage/latest/index.html</a>)。对于较新的库,等效于以下代码:</p>
<pre><code>from google.cloud import storage
client = storage.Client()
for blob in client.list_blobs('bucketname', prefix='abc/myfolder'):
print(str(blob))
</code></pre>
<p>下面是老客户的答案。</p>
<p>您可能会发现使用JSON API更容易,它有一个功能齐全的Python客户端。它有一个函数,用于列出带有前缀参数的对象,您可以使用该参数以这种方式检查某个目录及其子目录:</p>
<pre><code>from apiclient import discovery
# Auth goes here if necessary. Create authorized http object...
client = discovery.build('storage', 'v1') # add http=whatever param if auth
request = client.objects().list(
bucket="mybucket",
prefix="abc/myfolder")
while request is not None:
response = request.execute()
print json.dumps(response, indent=2)
request = request.list_next(request, response)
</code></pre>
<p>列表调用的完整文档如下:<a href="https://developers.google.com/storage/docs/json_api/v1/objects/list" rel="nofollow noreferrer">https://developers.google.com/storage/docs/json_api/v1/objects/list</a></p>
<p>Google Python API客户端的文档如下:
<a href="https://code.google.com/p/google-api-python-client/" rel="nofollow noreferrer">https://code.google.com/p/google-api-python-client/</a></p>