Search code examples
symfonyamazon-s3aclgaufrette

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 ?


Solution

  • 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 !