7

我正在使用KnpGaufretteBundle在 Amazon S3 上存储图片,我可以使用以下代码轻松创建一个文件:

public function s3CreatFileAction(Request $request) {

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

   [...]
}

问题是我无法访问该文件...

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

我收到以下消息:

找不到文件“test.txt”。

我认为这是因为“test.txt”文件是使用“私有”acl 创建的(当我通过 S3 控制台将其公开时,我可以访问它)。

所以我的问题是如何在创建对象时定义公共 acl?

4

2 回答 2

19

好吧,伙计们,KnpGaufretteBundle 的文档看起来像丛林☹ ...

这是我的解决方案:

#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'

在我的第一次试验中,此代码不起作用,因为我的 AwsS3 用户对存储桶没有正确的权限!因此,请确保您的用户策略允许访问存储桶

于 2014-11-28T10:34:30.773 回答
3

这是一个更灵活的解决方案,它允许仅对某些文件设置“公共”访问权限:

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

元数据数组覆盖了文件系统的全局选项,在我的例子中,默认有一个私有 ACL。

于 2018-07-25T08:23:22.437 回答