Python 操作amazon s3

Python  操作amazon s3


import os
import boto

from boto.s3.key import Key
from boto.s3.connection import S3Connection
from boto.s3.connection import Location
from boto.exception import S3CreateError


os.environ["AWS_ACCESS_KEY_ID"] = "..."
os.environ["AWS_SECRET_ACCESS_KEY"] = "...."


def create_or_get_bucket(bucket_name, location):
    conn = boto.connect_s3()
    bucket = conn.lookup(bucket_name);


    if bucket is None:
        bucket = conn.create_bucket(bucket_name, None,location,None)
    else:
        if bucket.get_location() != location:
            raise Exception('bad bucket location')
    return bucket


if __name__ == "__main__":
    # print '\n'.join(i for i in dir(Location) if i[0].isupper())
    bucket = create_or_get_bucket('s3-dev1','ap-southeast-1')

    for key in bucket.get_all_keys():
        key.get_contents_to_filename(key.name) 



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章