2015-07-18 34 views

回答

2

这是它支付使用底层的一个例子Google Cloud Storage API更直接,使用Google API Client Library for Python消耗RESTful HTTP API。通过这种方法,可以使用request batching来检索单个HTTP请求中的所有对象的名称(从而减少额外的HTTP请求开销),并使用objects.get操作(通过设置&fields=name)获得partial response,以便您不通过网络发送所有其他字段和数据(或等待在后端检索不必要的数据)。

代码,这将是这样的:

def get_credentials(): 
    # Your code goes here... checkout the oauth2client documentation: 
    # http://google-api-python-client.googlecode.com/hg/docs/epy/oauth2client-module.html 
    # Or look at some of the existing samples for how to do this 

def get_cloud_storage_service(credentials): 
    return discovery.build('storage', 'v1', credentials=credentials) 

def get_objects(cloud_storage, bucket_name, autopaginate=False): 
    result = [] 
    # Actually, it turns out that request batching isn't needed in this 
    # example, because the objects.list() operation returns not just 
    # the URL for the object, but also its name, as well. If it had returned 
    # just the URL, then that would be a case where we'd need such batching. 
    projection = 'nextPageToken,items(name,selfLink)' 
    request = cloud_storage.objects().list(bucket=bucket_name, fields=projection) 
    while request is not None: 
    response = request.execute() 
    result.extend(response.items) 
    if autopaginate: 
     request = cloud_storage.objects().list_next(request, response) 
    else: 
     request = None 
    return result 

def main(): 
    credentials = get_credentials() 
    cloud_storage = get_cloud_storage_service(credentials) 
    bucket = # ... your bucket name ... 
    for obj in get_objects(cloud_storage, bucket, autopaginate=True): 
    print 'name=%s, selfLink=%s' % (obj.name, obj.selfLink) 

你会发现在搞清楚如何做到这一点的Google Cloud Storage Python ExampleAPI Client Library Examples很有帮助。 Google Developers channel上还有许多YouTube视频,例如Accessing Google APIs: Common code walkthrough提供演练。

+0

你可以给我的代码做到这一点 –

+0

当然。提供了一些示例代码。 –

+0

对于凭据是API密钥是否足够? –