Skip to content

Instantly share code, notes, and snippets.

@wschwarz
Created March 1, 2013 07:26
Show Gist options
  • Save wschwarz/5063044 to your computer and use it in GitHub Desktop.
Save wschwarz/5063044 to your computer and use it in GitHub Desktop.
Simple php script to upload .jpg files from a folder to a bucket with file name format file_name_1.jpg
<?php
require 'vendor/autoload.php';
use Aws\Common\Aws;
use Aws\S3\Exception\S3Exception;
$aws = Aws::factory('/path_to_config/config.php');
$s3 = $aws->get('s3');
$dir = '/path_to_images';
$files = scandir($dir);
$file_type = "image/jpeg";
//file name prefix format: file_name_1.jpg
$uploadName = "file_name";
$i = 0;
foreach ($files as $file) {
echo "{$file}\n";
if (strstr($file, ".jpg")) {
$i += 1;
uploadFile($dir . "/" . $file, $uploadName . "_" . $i . ".jpg", $file_type, $s3);
}
}
function getFileHandle($filePath) {
return fopen($filePath, "r");
}
function uploadFile($file_path, $upload_name, $file_type, $aws_obj) {
$result = $aws_obj->putObject( array(
'Bucket' => 'bucket_name',
'Key' => $upload_name,
'Body' => getFileHandle($file_path),
'ContentType' => $file_type,
'ContentMD5' => false,
'ValidateMD5' => false
));
echo "{$result['ETag']}\n";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment