stevepop
By:
stevepop

File Upload Read Permissions on DO Spaces

September 17, 2017 306 views
Storage PHP Frameworks Ubuntu 16.04

I have been trying out spaces with in my Laravel application's file uploads feature. While I have successfully uploaded the files from the application using the S3 driver, users cannot view the images uploaded because the images are uploaded with permissions as private in spite of the fact that the bucket itself has public read access. Links to the images return the following error;
<Error>
<Code>AccessDenied</Code>
<BucketName>stevepop</BucketName>
<RequestId>99999-nyc3a</RequestId>
<HostId>aaaaa-nyc</HostId>
</Error>

With Amazon S3, I am able to set the bucket items permission with a policy like the one below;
{
"Version": "2008-10-17",
"Statement": [
{
"Sid": "AllowPublicRead",
"Effect": "Allow",
"Principal": {
"AWS": ""
},
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::uploads/
"
}
]
}

This way, all file uploads are immediately readable by users.

Is there an equivalent way of doing this in Spaces? If I can't do this then this feature will not be of any use to me.

Thanks!

2 Answers

If you use the API, you can add the header x-amz-acl:public-read.

  • Just tried it, I get a 403.

    PUT /2017-10-17/acl.conf4 HTTP/1.1
    User-Agent: curl/7.29.0
    Accept: /
    x-amz-acl: public-read
    Host: videorecordings.nyc3.digitaloceanspaces.com
    Date: Tue, 17 Oct 2017 09:48:59 +0000
    Content-Type: text/plain
    Authorization: AWS ...
    Content-Length: 2816
    Expect: 100-continue

    Got 403

I find the easiest way to do this is to use Laravel's putFile method, which allows you to pass the visibility of the uploaded file as an argument. So to store a file with public visibility, you'd do something like the following:

Storage::disk('spaces')->putFile('uploads', request()->file, 'public');
Have another answer? Share your knowledge.