Cloudn の Object Strage を PHP で S3 の代わりに使用する

composer で S3 の ライブラリを取得する

$ curl -sS https://getcomposer.org/installer | php
$ vi composer.json

{
  "require": {
    "aws/aws-sdk-php": "*"
  }
}

$ php composer.phar install

利用する

$ vi hoge.php

<?php
require_once (dirname(__FILE__) . '/../vendor/autoload.php');

use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;

define ('AWS_ENDPOINT', 'https://str.cloudn-service.com');
define ('AWS_ACCESS_KEY_ID', ' ... ');
define ('AWS_SECRET_ACCESS_KEY', ' ... ');


function upload_s3 ($s3, $bucket, $key, $file_path) {

  $res = fopen ($file_path, 'r');

  $finfo = finfo_open (FILEINFO_MIME_TYPE);
  $mime = finfo_file ($finfo, $file_path);
  finfo_close ($finfo);

  $meta = array ('Metadata' => array('ContentType' => $mime));

  $s3->upload ($bucket, $key, $res, 'public-read', array('params' => $meta));
}

function main () {

  ...

  $s3 = S3Client::factory (array(
      'key' => AWS_ACCESS_KEY_ID,
      'secret' => AWS_SECRET_ACCESS_KEY,
      'base_url' => AWS_ENDPOINT,
    ));

  upload_s3 ($s3, $bucket, $key, $file_path);
}