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

2.7k views Asked by At

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 ?

2

There are 2 answers

1
Ousmane On BEST ANSWER

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 !

0
Luigi C. On

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.