Skip to content

Instantly share code, notes, and snippets.

@masazdream
Created January 31, 2013 13:26
Show Gist options
  • Save masazdream/4682846 to your computer and use it in GitHub Desktop.
Save masazdream/4682846 to your computer and use it in GitHub Desktop.
upload_for_s3_php_example。AWSのキーは管理画面から取得します。キーには書き込み権限のあるユーザのものを使用します。
<?php
// load SDK
require '/usr/share/pear/AWSSDKforPHP/aws.phar';
use Aws\Common\Aws;
use Aws\Common\Enum\Region;
use Aws\S3\Enum\CannedAcl;
use Aws\S3\Exception\S3Exception;
use Guzzle\Http\EntityBody;
class FileUploader
{
function upload($file)
{
try {
$s3 = Aws::factory(array('key' => '[ここにAWSの管理が画面から取得した鍵文字列を入れる',
'secret' => '[ここにAWSの管理画面から取得した秘密鍵文字列を入れる]',
'region' => Region::AP_NORTHEAST_1))->get('s3');
$response = $s3->putObject(array('Bucket' => '[S3上に作成したバケットの名前]',
'Key' => '[パケットの中にファイルをアップロードするときのパス]',
'Body' => EntityBody::factory(fopen('[ローカルのファイルパス]', 'r')),
'ContentType' => 'image/jpeg',
'StorageClass' => 'STANDARD',
'ServerSideEncryption' => 'AES256', ←暗号化タイプ
'ACL' => CannedAcl::PRIVATE_ACCESS)); ←プライベートに設定
}catch(S3Exception $e){
var_dump($e);
}
}
}
$obj = new FileUploader();
$obj->upload('/var/tmp/test.jpg');
echo "aws test svr 01 upload and update test.jpg";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment