Question:
I need to upload my files inside specific directories that I created on my amazon s3 storage. I always uploaded the files on the “absolute path” of my bucket doing something like so:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$s3->putObject(array( 'Bucket' => $bucket, 'ContentType' => $mime, 'Key' => $localImage, 'ACL' => 'public-read', 'SourceFile' => $localImage, 'CacheControl' => 'max-age=172800', "Expires" => gmdate("D, d M Y H:i:s T", strtotime("+5 years")), 'Metadata' => array( 'profile' => $localImage, ), )); |
How can I define where this file should be uploaded on a given directory?
Answer:
You must include that information in the “Key” parameter. S3 isn’t actually a filesystem, it’s more like a big (hash table) associative array. The “Bucket” is the name of the hash table, and the “Key” is the key (e.g., $bucket[$key] = $content
). So all path/directory information must be a part of the “Key”.
1 2 3 4 5 6 7 |
$localImage = '/Users/jim/Photos/summer-vacation/DP00342654.jpg'; $s3->putObject(array( 'Bucket' => 'my-uniquely-named-bucket', 'SourceFile' => $localImage, 'Key' => 'photos/summer/' . basename($localImage) )); |