-1

I'm using AWS SDK PHP v3 to copy objects from bucket to bucket in S3.

When I use the putObject method, I can normally access the file via the URL, but when using the copyObject, I don't have public access marked.

My code is as below.

$credentials = new Aws\Credentials\Credentials(S3_KEY, S3_SECRET);

$this->client = new S3Client([
'region'        => 'us-east-1',
'version'       => '2006-03-01',
'credentials'   => $credentials
]);

$this->client->copyObject([
    'Bucket'        => S3_NEW_BUCKET,
    'CopySource'    => S3_OLD_BUCKET.'/'.$source,
    'Key'           => $destination,
]);

How should I do to allow access to the files?

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Tom
  • 641
  • 2
  • 8
  • 21

1 Answers1

1

To make the copied object publicly readable, you need to set the ACL to 'public-read' in the copyObject call, like this:

$credentials = new Aws\Credentials\Credentials(S3_KEY, S3_SECRET);

$this->client = new S3Client([
    'region'        => 'us-east-1',
    'version'       => '2006-03-01',
    'credentials'   => $credentials
]);

$this->client->copyObject([
    'Bucket'       => S3_NEW_BUCKET,
    'CopySource'   => S3_OLD_BUCKET.'/'.$source,
    'Key'          => $destination,
    'ACL'          => 'public-read'  // This line ensures the copied object is publicly readable
]);

Now, when you copy the object, it will be publicly accessible similar to how it would be when using the putObject method with the same ACL setting.

Always be careful when setting ACLs, especially to public-read, to avoid unintentionally exposing sensitive data. Ensure that the files you are making public are meant to be publicly accessible.

Piyush Patil
  • 14,512
  • 6
  • 35
  • 54