Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AmazonS3 + KnpGaufretteBundle how to set public acl for a file?

I'm using the KnpGaufretteBundle to store pictures on Amazon S3, I can easily create a file with the following code :

public function s3CreatFileAction(Request $request) {

   $filesystem = $this->get('knp_gaufrette.filesystem_map')->get('profile_photos');
   $filesystem->write('test.txt', 'hello world');

   [...]
}

The problem is that I can't access to the file ...

$filesystem = $this->get('knp_gaufrette.filesystem_map')->get('profile_photos');
$file = $filesystem->get('test.txt');

I got the following message :

The file "test.txt" was not found.

I assume that is because the "test.txt" file is created with a "private" acl (when I make it public trought the S3 console I can access to it).

So my question is how to define a public acl when I create my object ?

like image 458
Ousmane Avatar asked Nov 26 '14 14:11

Ousmane


People also ask

What is ACL public read?

This ACL stores all the users and groups that have access to read — or write — an object. Assuming you want your files to be public readable, you have to give read access to the AWS AllUsers group. To do this, both AWS CLI and Boto 3, provide a tool to manage the ACL.

How do you attach policy to S3 bucket to give public access for object?

Sign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ . In the Buckets list, choose the name of the bucket that you want to create a bucket policy for or whose bucket policy you want to edit. Choose Permissions. Under Bucket policy, choose Edit.


2 Answers

Ok guys it seems that KnpGaufretteBundle's doc looks like a jungle ☹ ...

Here is my solution :

#app/config/config.yml

services:
    acme.aws_s3.client:
        class: Aws\S3\S3Client
        factory_class: Aws\S3\S3Client
        factory_method: 'factory'
        arguments:
            -
                credentials:
                    key: %amazon_s3.key%
                    secret: %amazon_s3.secret%
                region: %amazon_s3.region%
                version: %amazon_s3.version%

# knp_gaufrette
knp_gaufrette:
    adapters:
        profile_photos:
            aws_s3:
                service_id: 'acme.aws_s3.client'
                bucket_name: 'myBucket'
                options:
                    directory: 'myDirectory'   
                    acl: 'public-read'

At my first trials this code did not work because my AwsS3 user did not have the correct permissions on the bucket ! So be sure that your user's policy allows access on the bucket !

like image 198
Ousmane Avatar answered Sep 26 '22 22:09

Ousmane


Here a more flexible solution that allows to set the "public" access only on some files:

$filesystem = $this->get('knp_gaufrette.filesystem_map')->get('profile_photos');
$filesystem->setMetadata('test.txt', ['ACL' => 'public-read']);
$filesystem->write('test.txt', 'hello world');

The metadata array overrides the global options of the filesystem, that in my case has a private ACL as default.

like image 38
Luigi C. Avatar answered Sep 25 '22 22:09

Luigi C.