Skip to content

Instantly share code, notes, and snippets.

@wokamoto
Created February 6, 2013 06:39
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wokamoto/4720784 to your computer and use it in GitHub Desktop.
Save wokamoto/4720784 to your computer and use it in GitHub Desktop.
[PHP][AWS] S3 永代供養用のスクリプト
<?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;
$bucket = 'bucket_name';
$dir = './path/';
function scan_file($dir, $target = '{*.html,*.htm,*.css,*.js,*.gif,*.png,*.jpg,*.jpeg,*.zip,*.xml}') {
$list = $tmp = array();
foreach(glob($dir . '*/', GLOB_ONLYDIR) as $child_dir) {
if ($tmp = scan_file($child_dir, $target)) {
$list = array_merge($list, $tmp);
}
}
foreach(glob($dir . $target, GLOB_BRACE) as $image) {
$list[] = $image;
}
return $list;
}
try {
$s3 = Aws::factory(array(
'key' => $access_key,
'secret' => $secret_key,
'region' => $region,
))->get('s3');
$info = new FInfo(FILEINFO_MIME_TYPE);
$files = scan_file($dir);
foreach ($files as $filename) {
$filetype = $info->file($filename);
$filebody = EntityBody::factory(fopen($filename, 'r'));
if ( $filetype == 'text/plain') {
if (preg_match('/\.css$/i', $filename))
$filetype = 'text/css';
else if (preg_match('/\.js$/i', $filename))
$filetype = 'application/x-javascript';
else if (preg_match('/\.html?$/i', $filename))
$filetype = 'text/html';
else if (preg_match('/\.xml$/i', $filename))
$filetype = 'application/xml';
}
echo $filetype . ':' . str_replace($dir, '', $filename) . "\n";
$response = $s3->putObject(array(
'Bucket' => $bucket,
'Key' => str_replace($dir, '', $filename),
'Body' => $filebody,
'ContentType' => $filetype,
'StorageClass' => 'STANDARD',
'ACL' => CannedAcl::PUBLIC_READ,
));
}
} catch (S3Exception $e) {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment