2015-07-18 51 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密鑰是否足夠? –