简体   繁体   English

DigitalOcean Spaces:无法将文件设置为公开

[英]DigitalOcean Spaces: unable to set file to public

I have a Digital Ocean Space setup with CDN configured.我有一个配置了 CDN 的 Digital Ocean Space 设置。 I know the CDN config is functional because files uploaded and set to public with Transmit work fine.我知道 CDN 配置是有效的,因为上传的文件和设置为公共的传输工作正常。 I'm trying to get my CMS to upload and publish files.我正在尝试让我的 CMS 上传和发布文件。

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.我试过使用这个SDK然后再次直接使用 S3 SDK 但到目前为止我无法在上传后将文件的隐私设置为公开,也无法将空间的(存储桶)隐私更改为公开。

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).尝试将文件的隐私设置为公开总是返回 404,尽管文件已成功上传且路径正确(我可以在 Spaces 仪表板中看到该文件)。 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.将空间设置为 public 不会出错,但似乎不会对其中的任何目录或文件产生影响。

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:这是返回的确切 404 错误:

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:我能够使用 boto3 将 object 设置为可公开访问:

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/使用以下说明创建s3_clienthttps://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在此处查看有关 boto3 的更多信息: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.put_object_acl

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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