How does paging work in the list_blobs function in Google Cloud Storage Python Client Library

前端 未结 3 1934
深忆病人
深忆病人 2021-02-20 02:42

I want to get a list of all the blobs in a Google Cloud Storage bucket using the Client Library for Python.

According to the documentation I should use the list_bl

3条回答
  •  醉话见心
    2021-02-20 02:45

    It was a bit confusing, but I found the answer here

    https://googlecloudplatform.github.io/google-cloud-python/latest/iterators.html

    You can iterate through the pages and call the items needed

    iterator=self.bucket.list_blobs()        
    
    self.get_files=[]        
    for page in iterator.pages:
        print('    Page number: %d' % (iterator.page_number,))
        print('  Items in page: %d' % (page.num_items,))
        print('     First item: %r' % (next(page),))
        print('Items remaining: %d' % (page.remaining,))
        print('Next page token: %s' % (iterator.next_page_token,))        
        for f in page:
            self.get_files.append("gs://" + f.bucket.name + "/" + f.name)
    
    print( "Found %d results" % (len( self.get_files))) 
    

提交回复
热议问题