What is the syntax for uploading a file directly to a folder in AWS S3 with the php sdk?
I was successful in uploading to a bucket using:
$response = $s3->create_object($bucket, $file_name, array('fileUpload' => $file_path))
tx!
Try this way
$s3->create_object($bucket, 'folder/filename', $options)
folder/filename to present file hierarchies, but these are all simply files in a common data storage bucket. So you need to explicitly prepend your file names with any folder-like identifier you would like to use.Simply do 1 thing, add 'path/' ex: If you have a folder 'videos' in a bucket then you have to add path like this in 'key' =>'videos/video-name-here'.
try {
// Upload data.
$result = $s3Client->putObject([
'Bucket' => bucket-name,
'Key' => 'videos/video-name', //add path here
'Body' => fopen($video, 'r'),
'ACL' => 'public-read'
]);
print_r($result['ObjectURL']);
} catch (S3Exception $e) {
echo $e->getMessage() . PHP_EOL;
}
Since all the answers here are pretty old, and this is the first result on Google, I'm posting the updated version:
$client = new \Aws\S3\S3Client([
'region' => '-- your region --',
'version' => 'latest',
'credentials' => [
'key' => "-- your IAM --",
'secret' => "-- your secret --",
]
]);
$dir = '/local/directory';
$bucket = 'my-bucket';
$keyPrefix = '';
$options =[
'params' => ['ACL' => 'public-read'],
'concurrency' => 20,
'debug' => true
];
$client->uploadDirectory($dir, $bucket, $keyPrefix, $options);