这2天接触了一下Amazon Web Services(AWS)中的Amazon Simple Storage Service,简称Amazon S3(下称S3)。S3站点上用下面的语句描述了自己的作用:

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.
通过S3这个接口,您可以把Amazon的存储服务当作一个硬盘,可以随时随地上传,下载数据,不过您得付钱才可以使用,即使您只是准备测试一 下。S3首页上有价格表。在S3注册并付费获取2个数:Access Key ID和Secret Access Key,才可以使用S3的服务,因为所有的操作都需要这2个数进行身份验证。

一、基本概念:Buckets,Objects,Keys,Operations

注册后,用户通过名为bucket的容器来管理所有数据,这些数据称之为对象(Object)。比如,一个名为20071211/logo- 320×240.gif的对象存放在livebaby这个bucket里,则可以通过URL:http: //s3.amazonaws.com/livebaby/20071211/logo-320×240.gif来访问这个对象。bucket有3个基本 概念:1、不能删除非空的bucket;2、bucket不能转让给其他用户;3、每个用户最多只能有100个buckets。简单的说,bucket类 似硬盘分区中的第一层目录。

这里还有一个key的概念,key是bucket中对象的唯一标识符,每个对象对应一个key。上例中,20071211/logo-320×240.gif称为key。

每个对象都有一组键值对,称为MetaData,分2种,系统metadata和 用户metadata。S3不处理用户metadata,只接收、存储、返回给用户,由用户自己处理。REST下,系统metadata以x-amz-开 头,用户metadata以x-amz-meta-开头。

Amazon提供2种API:Simple Object Access Protocol(SOAP)和Representational State Transfer(REST)来访问AWS。这里一篇文章,其中简单对比了一下2者的区别,区别后总结说:“ In fact, you can think of REST as a kind of data-access methodology whereas SOAP is a process-control methodology.”

通过SOAP和REST,最常用的操作(Operation)有这么几个:

* 创建bucket;
* 上传对象,上传时必须指定一个key,同时还可以设置这个对象的访问控制权限,默认是私有的;
* 下载对象,有HTTP 和 BitTorrent2种方式;
* 删除对象;
* 列表对象;列表时,可以通过一个前缀(prefix)来过滤显示不同的对象。

通过向AWS的服务端点(endpoint,http://s3.amazonaws.com/)发出PUT、GET、DELETE、HEAD等HTTP Requests,便可以操作S3上的数据。

如果通过web访问S3服务来获取数据(Object),则分页、搜索等功能是比不可少的。而S3提供的4个参数,可以让我们方便快捷的将数据取出来,这4个参数是基于key操作的。key的概念参见文章一。

1、prefix。比如http://bucket.s3.amazonaws.com/?prefix=123,则将key中以123开头的列出来。

2、delimiter。比如http://bucket.s3.amazonaws.com/?delimiter=/,此时,S3可能会返回CommonPrefixes,将key中采用“/”分隔的列出来。

<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>

这时输入http://bucket.s3.amazonaws.com/?delimiter=/&prefix=123/则将prefix中有123/的全部列出来。

如果key有这样的形式:ms_vb_5_src/sss.frm,ms_vb_6_src/ddd.frm,你可以把delimiter设为“_”来取数据。通过不同的delimiter和prefix可以非常灵活的获取数据。

如果数据量很大,则可以用第三个和第四个参数

3、MaxKeys。这个告诉S3一次性返回多少数据,默认返回1000个。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取出的数据不止10个;余下的数据怎么取?就用到了NextMarker元素中数据。

4、Marker。 URL输入http://bucket.s3.amazonaws.com/?delimiter=/&prefix=123/&max -keys=10&marker=123/10.txt,marker的值就是NextMarker元素中数据。这样S3就返回了下10条数据。

<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,则表示还有数据,我们把marker改为marker=123/19.txt便可以继续取下10条数据;如果IsTruncated为false,则表示按照条件已经将数据全部取出来了。

S3文档中,提供更为详细的解释。点击查看:http://docs.amazonwebservices.com/AmazonS3/2006-03-01/