這2天接觸了一下Amazon Web Services(AWS)中的Amazon Simple Storage Service,簡(jiǎn)稱Amazon S3(下稱S3)。S3站點(diǎn)上用下面的語(yǔ)句描述了自己的作用:
Amazon S3 is storage for the Internet. It is designed to make web-scale computing easier for developers.
Amazon S3 provides a simple web services interface that can be used to store and retrieve any amount of data, at any time, from anywhere on the web. It gives any developer access to the same highly scalable, reliable, fast, inexpensive data storage infrastructure that Amazon uses to run its own global network of web sites. The service aims to maximize benefits of scale and to pass those benefits on to developers.
通過(guò)S3這個(gè)接口,您可以把Amazon的存儲(chǔ)服務(wù)當(dāng)作一個(gè)硬盤(pán),可以隨時(shí)隨地上傳,下載數(shù)據(jù),不過(guò)您得付錢(qián)才可以使用,即使您只是準(zhǔn)備測(cè)試一 下。S3首頁(yè)上有價(jià)格表。在S3注冊(cè)并付費(fèi)獲取2個(gè)數(shù):Access Key ID和Secret Access Key,才可以使用S3的服務(wù),因?yàn)樗械牟僮鞫夹枰@2個(gè)數(shù)進(jìn)行身份驗(yàn)證。
一、基本概念:Buckets,Objects,Keys,Operations
注冊(cè)后,用戶通過(guò)名為bucket的容器來(lái)管理所有數(shù)據(jù),這些數(shù)據(jù)稱之為對(duì)象(Object)。比如,一個(gè)名為20071211/logo- 320×240.gif的對(duì)象存放在livebaby這個(gè)bucket里,則可以通過(guò)URL:http: //s3.amazonaws.com/livebaby/20071211/logo-320×240.gif來(lái)訪問(wèn)這個(gè)對(duì)象。bucket有3個(gè)基本 概念:1、不能刪除非空的bucket;2、bucket不能轉(zhuǎn)讓給其他用戶;3、每個(gè)用戶最多只能有100個(gè)buckets。簡(jiǎn)單的說(shuō),bucket類 似硬盤(pán)分區(qū)中的第一層目錄。
這里還有一個(gè)key的概念,key是bucket中對(duì)象的唯一標(biāo)識(shí)符,每個(gè)對(duì)象對(duì)應(yīng)一個(gè)key。上例中,20071211/logo-320×240.gif稱為key。
每個(gè)對(duì)象都有一組鍵值對(duì),稱為MetaData,分2種,系統(tǒng)metadata和 用戶metadata。S3不處理用戶metadata,只接收、存儲(chǔ)、返回給用戶,由用戶自己處理。REST下,系統(tǒng)metadata以x-amz-開(kāi) 頭,用戶metadata以x-amz-meta-開(kāi)頭。
Amazon提供2種API:Simple Object Access Protocol(SOAP)和Representational State Transfer(REST)來(lái)訪問(wèn)AWS。這里一篇文章,其中簡(jiǎn)單對(duì)比了一下2者的區(qū)別,區(qū)別后總結(jié)說(shuō):“ In fact, you can think of REST as a kind of data-access methodology whereas SOAP is a process-control methodology.”
通過(guò)SOAP和REST,最常用的操作(Operation)有這么幾個(gè):
* 創(chuàng)建bucket;
* 上傳對(duì)象,上傳時(shí)必須指定一個(gè)key,同時(shí)還可以設(shè)置這個(gè)對(duì)象的訪問(wèn)控制權(quán)限,默認(rèn)是私有的;
* 下載對(duì)象,有HTTP 和 BitTorrent2種方式;
* 刪除對(duì)象;
* 列表對(duì)象;列表時(shí),可以通過(guò)一個(gè)前綴(prefix)來(lái)過(guò)濾顯示不同的對(duì)象。
通過(guò)向AWS的服務(wù)端點(diǎn)(endpoint,http://s3.amazonaws.com/)發(fā)出PUT、GET、DELETE、HEAD等HTTP Requests,便可以操作S3上的數(shù)據(jù)。
如果通過(guò)web訪問(wèn)S3服務(wù)來(lái)獲取數(shù)據(jù)(Object),則分頁(yè)、搜索等功能是比不可少的。而S3提供的4個(gè)參數(shù),可以讓我們方便快捷的將數(shù)據(jù)取出來(lái),這4個(gè)參數(shù)是基于key操作的。key的概念參見(jiàn)文章一。
1、prefix。比如http://bucket.s3.amazonaws.com/?prefix=123,則將key中以123開(kāi)頭的列出來(lái)。
2、delimiter。比如http://bucket.s3.amazonaws.com/?delimiter=/,此時(shí),S3可能會(huì)返回CommonPrefixes,將key中采用“/”分隔的列出來(lái)。
<Contents>
<Key>123/zzz.txt</Key>
<LastModified>2007-12-11T07:41:51.000Z</LastModified>
<ETag>”d725dfc2167445d1db23067de33ebd28″</ETag>
<Size>203</Size>
<StorageClass>STANDARD</StorageClass>
</Contents>
<Contents>
<Key>abc/ooo/yxx.txt</Key>
<LastModified>2007-12-12T05:34:35.000Z</LastModified>
<ETag>”4fdf8a4dd42bd4d24855eebd5c9b9434″</ETag>
<Size>41</Size>
<StorageClass>STANDARD</StorageClass>
</Contents>
用了“delimiter=/”返回,告訴你 有prefix為123/和abc/可用。
<CommonPrefixes>
<Prefix>123/</Prefix>
</CommonPrefixes>
<CommonPrefixes>
<Prefix>abc/</Prefix>
</CommonPrefixes>
這時(shí)輸入http://bucket.s3.amazonaws.com/?delimiter=/&prefix=123/則將prefix中有123/的全部列出來(lái)。
如果key有這樣的形式:ms_vb_5_src/sss.frm,ms_vb_6_src/ddd.frm,你可以把delimiter設(shè)為“_”來(lái)取數(shù)據(jù)。通過(guò)不同的delimiter和prefix可以非常靈活的獲取數(shù)據(jù)。
如果數(shù)據(jù)量很大,則可以用第三個(gè)和第四個(gè)參數(shù)
3、MaxKeys。這個(gè)告訴S3一次性返回多少數(shù)據(jù),默認(rèn)返回1000個(gè)。URL輸入http://bucket.s3.amazonaws.com/?delimiter=/&prefix=123/&max-keys=10,則返回:
<Name>bucket</Name>
<Prefix>123/</Prefix>
<Marker />
<NextMarker>123/10.txt</NextMarker>
<MaxKeys>10</MaxKeys>
<Delimiter>/</Delimiter>
<IsTruncated>true</IsTruncated>
<Contents>
<Key>123/1.txt</Key>
<LastModified>2007-12-12T06:02:33.000Z</LastModified>
<ETag>”9dd4e461268c8034f5c8564e155c67a6″</ETag>
<Size>1</Size>
<StorageClass>STANDARD</StorageClass>
</Contents>
里面IsTruncated為true,表示按照delimiter和prefix和max-keys取出的數(shù)據(jù)不止10個(gè);余下的數(shù)據(jù)怎么取?就用到了NextMarker元素中數(shù)據(jù)。
4、Marker。 URL輸入http://bucket.s3.amazonaws.com/?delimiter=/&prefix=123/&max -keys=10&marker=123/10.txt,marker的值就是NextMarker元素中數(shù)據(jù)。這樣S3就返回了下10條數(shù)據(jù)。
<Name>bucket</Name>
<Prefix>123/</Prefix>
<Marker>123/10.txt</Marker>
<NextMarker>123/19.txt</NextMarker>
<MaxKeys>10</MaxKeys>
<Delimiter>/</Delimiter>
<IsTruncated>true</IsTruncated>
<Contents>
<Key>123/1.txt</Key>
<LastModified>2007-12-12T06:02:33.000Z</LastModified>
<ETag>”9dd4e461268c8034f5c8564e155c67a6″</ETag>
<Size>1</Size>
<StorageClass>STANDARD</StorageClass>
</Contents>
如果IsTruncated仍為true,則表示還有數(shù)據(jù),我們把marker改為marker=123/19.txt便可以繼續(xù)取下10條數(shù)據(jù);如果IsTruncated為false,則表示按照條件已經(jīng)將數(shù)據(jù)全部取出來(lái)了。
S3文檔中,提供更為詳細(xì)的解釋。點(diǎn)擊查看:http://docs.amazonwebservices.com/AmazonS3/2006-03-01/