Tag: PHP AmazonS3

Services_Amazon_S3でS3互換APIを使う

オリジナルをコピー&変更して使う

  • 変更が必要なファイルを適当なディレクトリにコピー
    # tree /usr/local/app/lib/Services/Amazon/
    /usr/local/app/lib/Services/Amazon/
    |-- S3
    |   `-- Resource
    |       |-- Bucket.php
    |       `-- Object.php
    `-- S3.php
    

変更

S3.php

  • include前にdefineしておけばいいようにする
  • content-typeはcharset付でもいいようにする
    diff Amazon/S3.php /usr/share/pear/Services/Amazon/S3.php
    53,65d52
    <  * for compatible API
    <  */
    < if (!defined('CMP_NS_S3')) {
    <     define('CMP_NS_S3', 'http://s3.amazonaws.com/doc/2006-03-01/');
    < }
    < if (!defined('CMP_HEADER')) {
    <     define('CMP_HEADER', 'x-amz-');
    < }
    < if (!defined('CMP_HEADER_AUTHORIZATION')) {
    <     define('CMP_HEADER_AUTHORIZATION', 'AWS');
    < }
    < 
    < /**
    146c133
    <     const NS_S3 = CMP_NS_S3;
    ---
    >     const NS_S3 = 'http://s3.amazonaws.com/doc/2006-03-01/';
    402c389
    <             if (strncmp($name, CMP_HEADER, strlen(CMP_HEADER)) === 0) {
    ---
    >             if (strncmp($name, 'x-amz-', 6) === 0) {
    491c478
    <             $headers['authorization'] = CMP_HEADER_AUTHORIZATION . ' ' . $this->accessKeyId . ':' .
    ---
    >             $headers['authorization'] = 'AWS ' . $this->accessKeyId . ':' .
    611c598
    <         if (strncmp($response->getHeader('content-type'), 'application/xml', 15) !== 0) {
    ---
    >         if ($response->getHeader('content-type') != 'application/xml') {

Bucket.php

diff Amazon/S3/Resource/Bucket.php /usr/share/pear/Services/Amazon/S3/Resource/Bucket.php
373c373
<             $headers[CMP_HEADER . 'acl'] = $this->acl;
---
>             $headers['x-amz-acl'] = $this->acl;

Object.php

  • ディレクトリ作成用にHTTP_Request2 0.4.1用のコードは無効にしておく
    diff Amazon/S3/Resource/Object.php /usr/share/pear/Services/Amazon/S3/Resource/Object.php
    250c250
    <             if (strncmp($name, CMP_HEADER . 'meta-', strlen(CMP_HEADER . 'meta-')) == 0) {
    ---
    >             if (strncmp($name, 'x-amz-meta-', 11) == 0) {
    297,301c297,301
    < //        if (strlen($this->data) == 0) {
    < //            // HTTP_Request2 0.4.1 does not send content-type when body is
    < //            // empty, so remove this before signing the request
    < //            unset($headers['content-type']);
    < //        }
    ---
    >         if (strlen($this->data) == 0) {
    >             // HTTP_Request2 0.4.1 does not send content-type when body is
    >             // empty, so remove this before signing the request
    >             unset($headers['content-type']);
    >         }
    304c304
    <             $headers[CMP_HEADER . 'meta-' . strtolower($name)] = $value;
    ---
    >             $headers['x-amz-meta-' . strtolower($name)] = $value;
    314c314
    <             $headers[CMP_HEADER . 'acl'] = $this->acl;
    ---
    >             $headers['x-amz-acl'] = $this->acl;
    358,359c358,359
    <         $headers[CMP_HEADER . 'copy-source'] = '/' . rawurlencode($source->bucket->name) .
    <                                           '/' . rawurlencode($source->key);
    ---
    >         $headers['x-amz-copy-source'] = '/' . rawurlencode($source->bucket->name) .
    >                                         '/' . rawurlencode($source->key);
    362c362
    <             $headers[CMP_HEADER . 'metadata-directive'] = 'COPY';
    ---
    >             $headers['x-amz-metadata-directive'] = 'COPY';
    364c364
    <             $headers[CMP_HEADER . 'metadata-directive'] = 'REPLACE';
    ---
    >             $headers['x-amz-metadata-directive'] = 'REPLACE';
    371c371
    <                 $headers[CMP_HEADER . 'meta-' . strtolower($name)] = $value;
    ---
    >                 $headers['x-amz-meta-' . strtolower($name)] = $value;
    392c392
    <             $headers[CMP_HEADER . 'acl'] = $this->acl;
    ---
    >             $headers['x-amz-acl'] = $this->acl;

利用サンプル(ニフクラ)

  • include_pathでpearより前にしておくことで変更版が使われる
  • endpointはpublicなので普通に変更すればいい
    set_include_path('/usr/local/app/lib' . PATH_SEPARATOR . get_include_path());
    
    define('CMP_NS_S3', 'http://doc.ncss.nifty.com/2011-09-29');
    define('CMP_HEADER', 'x-nifty-');
    define('CMP_HEADER_AUTHORIZATION', 'NIFTY');
    
    require_once 'Services/Amazon/S3.php';
    
    $accessKeyId     = '<ACCESSKEYID>';
    $secretAccessKey = '<SECRETACCESSKEY>';
    
    $s3 = Services_Amazon_S3::getAccount($accessKeyId, $secretAccessKey);
    $s3->endpoint = 'ncss.nifty.com';

Last-modified: 2011-11-30 (水) 13:42:13 (2150d)