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

2

7

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 ?

Weekly answered 26/11, 2014 at 14:47 Comment(0)
W
19

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 !

Weekly answered 28/11, 2014 at 10:34 Comment(1)
God bless you !Septa
O
3

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.

Oraliaoralie answered 25/7, 2018 at 8:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.