Skip to content

Instantly share code, notes, and snippets.

@sc0Vu
Last active September 12, 2016 04:42
Show Gist options
  • Save sc0Vu/90cde61d67ebabf018ffd6f49358945c to your computer and use it in GitHub Desktop.
Save sc0Vu/90cde61d67ebabf018ffd6f49358945c to your computer and use it in GitHub Desktop.
Upload directory recursively and concurently to AWS S3
require_once __DIR__.DIRECTORY_SEPARATOR.'Aws'.DIRECTORY_SEPARATOR.'aws-autoloader.php';
// require_once './vendor/autoload.php' if you composer
$ds = DIRECTORY_SEPARATOR;
$srcDir = 'your src dir;
$filePath = [];
$promises = [];
$bucket = 'your bucket';
$options = [
'version' => 'latest',
'region' => 'your S3 region',
'credentials' => [
'key' => 'your S3 key',
'secret' => 'your S3 secret'
],
'debug' => false
];
$s3Client = new Aws\S3\S3Client($options); // remember check timestamp
try {
$filePath = recursiveGetFilePath($srcDir);
foreach ($filePath as $path) {
if ((basename($path) == ".") || (basename($path) == "..") || is_dir($path)) {
continue;
}
// http://stackoverflow.com/questions/7039771/upload-a-file-to-s3-buckets-folder-using-asp-net-sdk
$key = explode($srcDir.'/', $path)[1];
// use GuzzleHttp promise to upload concurrently
// check:
// https://github.com/guzzle/promises
// http://blogs.aws.amazon.com/php/
// http://docs.aws.amazon.com/aws-sdk-php/v3/guide/guide/promises.html
$promises[] = $s3Client->putObjectAsync([
'ACL' => 'public-read',
'Bucket' => $bucket,
'Key' => $key,
'SourceFile' => $path
]);
}
$allPromise = \GuzzleHttp\Promise\all($promises);
$allPromise->wait();
} catch (Aws\S3\Exception\S3Exception $e) {
echoLine('S3: ' . $e->getMessage());
} catch (Aws\Exception\AwsException $e) {
echoLine('Aws: ' . $e->getAwsRequestId());
echoLine('Aws: ' . $e->getAwsErrorType());
echoLine('Aws: ' . $e->getAwsErrorCode());
}
function echoLine ($str) {
echo $str.'<br>';
}
function recursiveGetFilePath ($target) {
if (is_dir($target)) {
return new RecursiveIteratorIterator(new RecursiveDirectoryIterator($target), RecursiveIteratorIterator::SELF_FIRST);
}
return [
$target
];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment