AWS之s3

AWS已在中國落地!詳情請猛擊此處


S3是AWS中的存儲服務,爲用戶隨時隨地存儲和訪問大量數據提供了Web service接口,爲開發者提供了一種可以快速低廉訪問數據存儲的服務。並且,開發者可以利用s3實現ec2訪問大量的數據資源。其價格低廉,受到了廣泛開發者的青睞。


s3中均是以bucket,key來區分數據,類似與文件系統中的文件夾,文件的概念。有所不同的是,s3中沒有文件系統中的層級關係,只有bucket-key兩層,key保存在指定的bucket中。當然key的名稱可以自己定義,你可以將‘/'添加在key名字中(如'dir1/dir2/file'),以作爲子文件夾,但s3並不會將其視爲文件夾的層級結構,在查看bucket的key時,返回的結果也不會考慮。


既然是Web service,那麼其使用方式就是set_contents_from_file非常簡單的,不外乎兩種形式,一種是用AWS的command line工具(cli),另一種是aws利用各種開發語言的sdk進行數據的存儲、訪問、修改。下面分別簡單介紹cli和aws在python中的sdk boto中,s3的使用方法。


1. cli

在這裏我們假設已經配置好cli的key,關於配置可以參看此文

與shell風格類似地,cli提供了以下幾種對s3的方法:

單個文件操作:
cp, mv, rm (single local file and s3 object operations)
文件夾相關操作:

sync, mb, rb, ls (directory and s3 prefix operations)

其中,對於文件夾操作,可以利用--recursive和--exclude實現文件夾的遞歸操作以及過濾操作。

查看當前帳戶中的所有buckets

$ aws s3 ls
$ aws s3 ls s3://mybucket


When you may need to specify the region of the bucket

$ aws s3 rm s3://mybucket --recursive --region us-east-1


# Uploading local files onto s3

$ aws s3 cp myfolder s3://mybucket/myfolder --recursive


# A sync command makes it easy to synchronize the contents of a local folder with a copy in an S3 bucket.

$ aws s3 sync myfolder s3://mybucket/myfolder --exclude *.tmp


You can also get help with the command:

$ aws s3 help


2.boto

在這裏我們假設已經配置好boto的key,關於配置可以參看此文

1)創建s3的連接:

conn=S3Connection(access_key_id, secret_key)

2)創建bucket:

bucket=conn.create_bucket(bucket_name, headers=None, location='', policy=None)

你需要注意你的bucket_name的設置,可以將其考慮成域名的格式,使用period(.)進行層級劃分,關於bucket_name設置的規則請猛擊這裏


你可以通過設置location來安排你上傳的位置,默認是us-east-1,其他位置如下:

class boto.s3.connection.LocationAPNortheast = 'ap-northeast-1'
APSoutheast = 'ap-southeast-1'
APSoutheast2 = 'ap-southeast-2'
CNNorth1 = 'cn-north-1'
DEFAULT = ''
EU = 'EU'
SAEast = 'sa-east-1'
USWest = 'us-west-1'
USWest2 = 'us-west-2'

       需要注意的是,在創建bucket你有可能遇到

       創建bucket時你有可能會遇到S3CreateError的異常,返回BucketAlreadExists的錯誤,這是由於在s3中所有用戶的bucket都放在同一層,如果你創建的bucket名稱比較常見,如test,則很有可能已被其他用戶使用,從而出現該問題。

       在boto的reference文檔中提到了這一點,原文如下

"Well, the thing you have to know aboutbuckets is that they are kind of like domain names.  It’s one flat namespace that everyone who uses S3 shares.  So, someone has already createa bucket called “mybucket” in S3 and that means no one else can grab that bucket name.  So, you have to come up with a name that hasn’t been taken yet.For example, something that uses a unique string as a prefix.  YourAWS_ACCESS_KEY (NOT YOUR SECRET KEY!) could work but I’ll leave it toyour imagination to come up with something.  I’ll just assume that youfound an acceptable name."


3)設置bucket的訪問控制規則:

bucket.set_acl('public-read')

4)寫入key:

key=bucket.new_key(file_path)
key.set_contents_from_filename(os.path.join(dir_path, file_path))

或者使用set_contents_from_file


5)讀取所有的key

for bucket in conn.get_all_buckets():
    for key in bucket.get_all_keys():
        print bucket, key.key, key.md5


需要注意的是get_all_buckets()函數是一個底層的實現,其一次最多返回1000個key(one paging of results),因此在實際中可以使用以下方式

for key in [key for key in bucket]:
    key.get_contents_to_filename(local_file_path)


更多關於boto操作s3,可以參看boto的reference

http://boto.readthedocs.org/en/latest/ref/s3.html


後面我還會介紹如何使用boto操作ec2,並在ec2上構建、配置自己的hadoop平臺,敬請關注!

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