Created
February 6, 2013 03:45
-
-
Save wokamoto/4720064 to your computer and use it in GitHub Desktop.
[PHP][AWS] AWS SDK PHP 2 で S3 にファイルをアップロード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once("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; | |
$access_key = 'Your Access Key'; | |
$secret_key = 'Your Secret Key'; | |
$region = Region::AP_NORTHEAST_1; // Region::AP_NORTHEAST_1 = Tokyo Region | |
$bucket = 's3_bucket_name'; | |
try { | |
// S3 | |
$s3 = Aws::factory(array( | |
'key' => $access_key, | |
'secret' => $secret_key, | |
'region' => $region, | |
))->get('s3'); | |
$info = new FInfo(FILEINFO_MIME_TYPE); | |
// Upload File | |
$filename = 'test.txt'; | |
$filebody = EntityBody::factory(fopen($filename, 'r')); | |
$filetype = $info->file($filename); | |
$response = $s3->putObject(array( | |
'Bucket' => $bucket, | |
'Key' => $filename, | |
'Body' => $filebody, | |
'ContentType' => $filetype, | |
'StorageClass' => 'STANDARD', // STANDARD or REDUCED_REDUNDANCY | |
//'ServerSideEncryption => 'AES256', | |
'ACL' => CannedAcl::PUBLIC_READ, // PUBLIC_READ, PRIVATE_ACCESS... | |
)); | |
var_dump($response); | |
} catch (S3Exception $e) { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment