from boto.s3.connection import S3Connection


access_key = "*****5JJPZD6WYG*****"

secret_key = "*****GnYf/bmdJ9NfkveFF+Mb8IPjVIGybC*****"

region = "s3-ap-northeast-2.amazonaws.com"

conn = S3Connection(access_key, secret_key, host=region)

bucket = conn.get_bucket('ghpark-mp3')

for key in bucket.list():

    print key.name

 


regions 

http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-regions-availability-zones.html


seoul region을 명시해줘야 400 에러 발생하지 않음 ㅡㅜ 


http://boto.cloudhackers.com/en/latest/s3_tut.html


import os

import fnmatch

from boto.s3.connection import S3Connection


conn = S3Connection('*****5JJPZD6WYG*****','*****GnYf/bmdJ9NfkveFF+Mb8IPjVIGybC*****', host="s3-ap-northeast-2.amazonaws.com")

bucket = conn.get_bucket('ghpark-mp3')

for key in bucket.list():

    print key.name

    # download

    #key.get_contents_to_filename(key.name)

    # delete

    #key.delete()


for f in os.listdir("."):

    if fnmatch.fnmatch(f, "*.mp3"):

        f = f.encode("UTF-8")

        k = bucket.new_key(f)

        # upload

        k.set_contents_from_filename(f)



s3bucket 용량 구하는거 ... 깔끔하네

https://gist.github.com/robinkraft/2667939



import boto

s3 = boto.connect_s3(aws_id, aws_secret_key)


# based on http://www.quora.com/Amazon-S3/What-is-the-fastest-way-to-measure-the-total-size-of-an-S3-bucket


def get_bucket_size(bucket_name):

    bucket = s3.lookup(bucket_name)

    total_bytes = 0

    n = 0

    for key in bucket:

        total_bytes += key.size

        n += 1

        if n % 2000 == 0:

            print n

    total_gigs = total_bytes/1024/1024/1024

    print "%s: %i GB, %i objects" % (bucket_name, total_gigs, n)

    return total_gigs, n


bucket_list = []

bucket_sizes = []


for bucket_name in bucket_list:

    size, object_count = get_bucket_size(bucket_name)

    bucket_sizes.append(dict(name=bucket_name, size=size, count=object_count))


print "\nTotals:"

for bucket_size in bucket_sizes:

    print "%s: %iGB, %i objects" % (bucket_size["name"], bucket_size["size"], bucket_size["count"]) 




'Python' 카테고리의 다른 글

Django + djangorestframework + django_rest_swagger 시작  (0) 2017.02.01
Pika Python AMQP Client Library  (0) 2017.01.31
daemonizing  (0) 2016.12.22
Threading  (0) 2016.12.22
pidlockfile.py for windows  (0) 2016.12.19

+ Recent posts