簡體   English   中英

使用Python列出IBM COS S3存儲桶中的所有鍵

[英]Listing all keys in a IBM COS S3 bucket using Python

我希望跟蹤存儲在cos存儲桶中的密鑰列表。

我正在使用Python,目前我的代碼是:

 files = cos.Bucket('bucketname').objects.all()

 for file in files:
     data[file.key] = 'not processed'
     data_array.append(data)

這對於我來說非常慢,因為目前我的存儲桶中有很多1M +密鑰。

有沒有更好的辦法? 我目前正在查看https://alexwlchan.net/2018/01/listing-s3-keys-redux/

但是我遇到了麻煩,因為在建立連接時,ibm-cos-sdk返回s3資源而不是客戶端。

任何提示將不勝感激。

您可以使用與鏈接到的代碼相似的代碼。 我能夠用這樣的代碼執行它:

# fetch endpoints
endpoints_list_uri="https://cos-service.bluemix.net/endpoints"
endpoints = requests.get(endpoints_list_uri).json()
cos_host = (endpoints['service-endpoints']['regional']['us-south']['public']['us-south'])

#create client
cos = ibm_boto3.client('s3',endpoint_url='https://'+cos_host)

# retrieve keys from bucket
keys=get_matching_s3_keys(s3=cos,bucket="encryptedbucket1",prefix='mypattern')
for key in keys:
    print key

請注意,我已經修改了兩個函數頭,以允許傳入S3客戶端對象。 如果有興趣,我可以將整個源代碼放在GitHub等上。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM