简体   繁体   中英

DigitalOcean Spaces: unable to set file to public

I have a Digital Ocean Space setup with CDN configured. I know the CDN config is functional because files uploaded and set to public with Transmit work fine. I'm trying to get my CMS to upload and publish files.

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 (I can see the file in the Spaces dashboard). 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
}

Here is the exact 404 error returned:

Error executing "PutObjectAcl" on "https://xx.nyc3.digitaloceanspaces.com//media/pages/journal/11-27-2020/2478608562-1607828382/m6-79-28-38x.jpg?acl"; AWS HTTP error: Client error: 'PUT https://xx.nyc3.digitaloceanspaces.com//media/pages/journal/11-27-2020/2478608562-1607828382/m6-79-28-38x.jpg?acl' resulted in a 404 Not Found response:

<?xml version="1.0" encoding="UTF-8"?>
  <Error>
    <Code>NoSuchKey</Code>
    <BucketName>xx</BucketName>
    <RequestId>tx000000000 (truncated...) NoSuchKey (client): - 
<?xml version="1.0" encoding="UTF-8"?>
  <Error>
    <Code>NoSuchKey</Code>
    <BucketName>xx</BucketName>
    <RequestId>tx000000000000000d949ce-005fd5839f-2a11a58-nyc3c</RequestId>
    <HostId>abcd-abcd-abcd</HostId>
  </Error>

Unlike another question posted there are no special characters in the filenames nor in 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

I was able to set an object to be publicly accessible using boto3:

s3_client.put_object_acl(Bucket="my-bucket", Key="favicon.ico", ACL='public-read')

Create the s3_client with the following instructions: https://docs.digitalocean.com/reference/api/spaces-api/

see more info on boto3 here: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.put_object_acl

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM