いろいろ定義:
bootstrap.php
define('AWS_ACCESS_KEY', 'YOUR AWS ACCESS KEY');
define('AWS_ACCESS_SECRET', 'YOUR AWS ACCESS SECRET');
define('BUCKET_NAME', 'YOUR BUCKET NAME');
S3 にアップロードするクラス:
Upload.php
private static function uploadToS3($filePath, $mimeType)
{
$s3 = Services_Amazon_S3::getAccount(AWS_ACCESS_KEY, AWS_ACCESS_SECRET);
$bucket = $s3->getBucket(BUCKET_NAME);
$s3FilePath = date('Y/md/His') . basename($filePath);
$object = $bucket->getObject($s3FilePath);
$object->contentType = $mimeType;
$object->acl = Services_Amazon_S3_AccessControlList::ACL_PUBLIC_READ;
$object->data = file_get_contents($filePath);
$object->save();
$url = $object->getUrl();
//$url = str_replace('%2F', '/', $url);
return $url;
}
上のクラスを使うコントローラクラス:
ExampleController.php
Upload::uploadToS3('example.jpg', 'image/jpeg');