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!
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:
- $client = new Aws\S3\S3Client([
- 'version' => 'latest',
- 'region' => 'us-east-1',
- **'endpoint' => 'https://nyc3.digitaloceanspaces.com'**,
- 'credentials' => [
- 'key' => getenv('storage.access_key'),
- 'secret' => getenv('storage.secret_key'),
- ]
- ]);
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!
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.