Report this

What is the reason for this report?

Unable to set file to public

Posted on December 12, 2020

I’ve tried using this SDK and then again directly with the S3 SDK but so far I’ve been unable to set a file’s privacy to public after upload, nor change the space’s (bucket) privacy to public.

Trying to set a file’s privacy to public always returns a 404, despite the file being successfully uploaded and the path correct. Setting the space to public doesn’t give an error but doesn’t seem to have an effect on any directory or file inside it.

Here is the code I’m using

$client = new Aws\S3\S3Client([
  'version' => 'latest',
  'region'  => 'us-east-1',
  'endpoint' => getenv('storage.region'),
  'credentials' => [
    'key'    => getenv('storage.access_key'),
    'secret' => getenv('storage.secret_key'),
  ]
]);

$client->uploadDirectory($filePath, "XX", $dirName); // successful
$client->putBucketAcl(["Bucket" => "XX", "ACL" => 'public-read']); // no error, but no effect on space

foreach (new DirectoryIterator($filePath) as $file) {
  if($file->isDot()) continue;
  $client->putObjectAcl(["Bucket" => "XX", "Key" => $dirname . $file->getFilename(), "ACL" => "public-read"]); // always returns 404
}

Unlike another question posted here there are no special characters in the filenames nor the paths used, here is one example:

https://xx.nyc3.digitaloceanspaces.com/media/pages/journal/11-27-2020/3551000373-1607742797/m6-75-29a-100x.jpg


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hello,

It seems like you might be using the wrong endpoint in your code. The endpoint should be the Space’s endpoint (like ‘https://nyc3.digitaloceanspaces.com’), not the.Compute instance’s region. Please update your ‘endpoint’ value to match your DigitalOcean Spaces endpoint.

Here’s an example:

  1. $client = new Aws\S3\S3Client([
  2. 'version' => 'latest',
  3. 'region' => 'us-east-1',
  4. **'endpoint' => 'https://nyc3.digitaloceanspaces.com'**,
  5. 'credentials' => [
  6. 'key' => getenv('storage.access_key'),
  7. 'secret' => getenv('storage.secret_key'),
  8. ]
  9. ]);

Also, it is important to ensure the file exists before trying to set its ACL to ‘public-read’. Double-check your file paths and make sure they are correct.

For more information about using DigitalOcean Spaces with the AWS S3 SDK, you can refer to this s3-sdk-examples.

Hope that this helps!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.