2

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?

1 Answer 1

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().

Sign up to request clarification or add additional context in comments.

5 Comments

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: stackoverflow.com/questions/16765158/… One question though. Is it as efficient as CLI tool ?
Not sure. I guess you'd have to test that out on your own.
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.
@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);
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 stackoverflow.com/questions/41960048/….

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.