プログラマでありたい

おっさんになっても、プログラマでありつづけたい

Amazon S3 - Simple Storage Service

思い立ってAmazon S3に申し込んでみた。
早速サンプルアプリで遊んで見る。

サンプルアプリは、perl,java,php,rubyと一通り揃っている。
勝手知ったるphpで試してみる。
http://developer.amazonwebservices.com/connect/entry.jspa?externalID=126&categoryID=47

ReadMeを読むと、Crypt_HMACとHTTP_Requestが必要とのこと。
ちなみにHTTP_RequestはNet_SocketとNet_URLを必要としている。
Pearからダウンロードして、サンプルコードを展開。
あっさり動く。ただし、入力項目の意味が解らない。

入力項目は、
AWSAccessKeyId
SecretAccessKey
Bucket
Key
Content-Type
Operation
Access Control Policy
File to PUT

AWSAccessKeyIdとSecretAccessKeyは、申し込み時に送られて来たので問題なし。
Content-TypeとOperation,Access Control Policy,File to PUTは想像がつく。
BucketとKeyは何を指す??
マニュアルを読んでみる。

Buckets

Objects are stored in buckets. The bucket provides a unique namespace for management of objects contained in the bucket. Each bucket you create is owned by you for purposes of billing, and you will be charged storage fees for all objects stored in the bucket and bandwidth fees for all data read from and written to the bucket. There is no limit to the number of objects that one bucket can hold. Since the namespace for bucket names is global, each developer is limited to owning 100 buckets at a time.

Objects

Objects are the fundamental entities stored in Amazon S3. Objects are composed of object data and metadata. The data portion is opaque to Amazon S3. The metadata is a set of name-value pairs that describe the object. These include some default metadata such as the date last modified, and standard HTTP metadata such as Content-Type. The developer may also specify custom metadata at the time the Object is stored.

Keys

A key is the unique identifier for an object within a bucket. Every object in a bucket has exactly one key. Since a bucket and key together uniquely identify each object, Amazon S3 can be thought of as a basic data map between "bucket + key" and the object itself. Every object in Amazon S3 can be uniquely addressed through the combination of the Service endpoint, bucket name, and key, as in http://s3.amazonaws.com/doc/2006-03-01/AmazonS3.wsdl, where "doc" is the name of the bucket, and "2006-03-01/AmazonS3.wsdl" is the key.

Bucketは、表領域やテーブルのようなもの。Keyは、Bucket中のオブジェクトを指定するもの。つまりBucket+Keyで一意のオブジェクトを指定出来るようだ。

それぞれ適当にtestと入力して、「Go」をクリック

すると、以下の内容を含んだ実行結果が。

AccessDenied
AWS authentication requires a valid Date or x-amz-date header

有効な日付かx-amz-date headerを必要としていますとのこと。
謎??

ソースを見てみると

// Note that version HTTP_Request 1.3.0 has a BUG in it! Change line
// 765 from:
// (HTTP_REQUEST_METHOD_POST != $this->_method && empty($this->_postData) && empty($this->_postFiles))) {
// to:
// (HTTP_REQUEST_METHOD_POST == $this->_method && empty($this->_postData) && empty($this->_postFiles))) {
// Without this change PUTs with non-empty content-type will fail!

えらい根本的なバグ仕込んでいるなぁ。HTTP_Request
検証もせずに盲目的に信じて、ソースを修正してみる。

しかし、やっぱり駄目。
USAで申し込んで、日本のサーバからアクセスしているせいか??
全く同じものを、USAのサーバに入れて実行してみると、

AccessDenied
Access Denied

今度は純粋にAccessが拒否されているっぽい。
Bucketsについて、もう一度マニュアルを調べてみる。
他の部分に以下の用な記述が。

Buckets
Objects are stored in buckets. Buckets can store an unlimited number of objects. Amazon designed S3 so that you could easily keep all of your objects in a single bucket if you like. Bucket names are strings that must be at 3 bytes long, and less than 255 characters long. For now, the names can contain ASCII letters, numbers, underscores and dashes. In the future, we may allow non-ASCII bucket names.

The bucket namespace is shared between all users of Amazon S3; no two buckets can have the same name. Each bucket has an owner, identified as the developer whose AWS Access Key ID was used to authenticate at the time the bucket was created. Bucket ownership is not transferable. Each developer is restricted to owning 100 buckets at a time.

Empty buckets can be deleted. Once a bucket is deleted, its name can be reused.

 どうやらbucket名はAmazon S3ユーザ間で共有されているようだ。つまり、ユーザ+bucket+keyでオブジェクトの識別ではなく、bucket+keyでオブジェクトを識別するということ。さっき、表領域かテーブルと言ったが、表領域の方が概念として近いようだ。そして、そもそもこのアプリにはbucketを作る機能がない。
 で、問題はどうやってbucketを作るのか?
SOAPとRESTでPutする方法は書いていた。まずは、そっちをやらないとあかんみたい。