【问题标题】:Listing all keys in a IBM COS S3 bucket using Python使用 Python 列出 IBM COS S3 存储桶中的所有键
【发布时间】:2019-01-28 00:19:00
【问题描述】:

我希望跟踪存储在 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 资源而不是客户端。

任何提示将不胜感激。

【问题讨论】:

    标签: python amazon-s3 ibm-cloud object-storage


    【解决方案1】:

    您可以使用与您链接的代码类似的代码。我可以用这样的代码来执行它:

    # 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 等上。

    【讨论】:

    • 完美,我做了一些实验后明白了这一点。你能推荐一个更快的方法吗,因为看起来最后的 for 循环需要相当长的时间来运行
    猜你喜欢
    • 1970-01-01
    • 2017-12-07
    • 2017-11-25
    • 2018-08-28
    • 2019-05-08
    • 1970-01-01
    • 2018-10-24
    • 2021-05-09
    • 2021-11-25
    相关资源
    最近更新 更多