amazons3.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:bksync 作者: oddy 项目源码 文件源码
def TestMain(cfg):
    # boto3.set_stream_logger(name='botocore')
    # os.environ["HTTP_PROXY"] = "http://localhost:8080"    
    # os.environ["HTTPS_PROXY"] = "https://localhost:8999"

    sess = boto3.session.Session( 
            aws_access_key_id       = cfg['access_key_id'],
            aws_secret_access_key   = cfg['access_key'],
            region_name             = cfg['region']
        )

    s3 = sess.resource('s3')

    print '\n--- List Buckets ---'

    for bucket in s3.buckets.all():
        print bucket.name

    bkt = s3.Bucket('fred4')

    # print '\n--- Key exists ---'
    # print bkt.get_key('zuxisaki')     # i dont think this works

    print '\n--- List objects in bucket fred4 ---'

    # len(list(bkt.objects.filter(Prefix='index')))     # One request, doesnt return tons of data!
    for i,obj in enumerate(bkt.objects.all()):          # 1348 items - 2 requests, and boto does it for me. 
        print i, obj.key, obj.size                      # does not fetch the actual object, score
        if i > 10000:  break

    print '\n--- Body reads ---'

    print obj.get()['Body'].read()                          # from iteration
    print bkt.Object('zuxisaki').get()['Body'].read()       # by name

    print '\n--- Body write ---'

    data = 'A'*128
    nm = RandChars(8)
    bkt.Object(nm).put(Body=data)
    print 'Wrote key=',nm

# Is Best rand chars
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号