I can't seem to find sync and copy options in the php sdk for amazon-s3. Currently I am using aws s3 cp/sync CLI command from within PHP code to achieve this which doesn't seem very neat to me. Any ideas?
Asked
Active
Viewed 3,017 times
1 Answers
2
You can do this with a combination of two features in the PHP SDK.
First, checkout the S3Client::uploadDirectory()
method (see user guide). You can upload a while directory's contents like this:
$s3Client->uploadDirectory('/local/directory', 'your-bucket-name');
If you combine this with the S3 Stream Wrapper, then instead of using a local directory as the source, you can specify a bucket.
$s3Client->registerStreamWrapper();
$s3Client->uploadDirectory('s3://your-other-bucket-name', 'your-bucket-name');
This is pretty powerful, because you can even do something across regions if you use a differently configured S3Client
object for the Stream Wrapper than the one doing the uploadDirectory()
.

Jeremy Lindblom
- 6,437
- 27
- 30
-
Thanks a lot Jeremy. It works like a charm. The only issue it caused was the fatal error because of missing timezone information from php.ini file. Adding the timezone information made it work. Ref: http://stackoverflow.com/questions/16765158/date-it-is-not-safe-to-rely-on-the-systems-timezone-settings-in-codeigniter One question though. Is it as efficient as CLI tool ? – Ajay Narang Feb 15 '14 at 23:12
-
Not sure. I guess you'd have to test that out on your own. – Jeremy Lindblom Feb 16 '14 at 05:51
-
I used the aws cli client to try to copy over 500k objects. It missed a lot of objects and also did not copy ACL permissions. Trying the above method now will report back. – Justin Vincent Oct 30 '15 at 17:41
-
@JeremyLindblom, I am getting error while using uploadDirectory() with registerStreamWrapper(). Please review my code below - $s3 = new S3Client(['credentials' => $credentials, 'version' => 'latest','region' => $region,'debug' => false ]); $s3->registerStreamWrapper(); $result = $s3->uploadDirectory("s3://{$source_bucket}/{$key}",$target_bucket); – Neeraj Rathod Feb 23 '17 at 14:59
-
One major thing to be aware of though, in `v3` of the PHP SDK `S3Client::uploadDirectory()` does not intelligently sync, it uploads everything again. See https://stackoverflow.com/questions/41960048/is-sync-in-amazon-s3-php-sdk-api-really-a-sync-operation/. – chronon Jun 27 '17 at 16:49