【问题标题】:AmazonS3 + KnpGaufretteBundle how to set public acl for a file?AmazonS3 + KnpGaufretteBundle 如何为文件设置公共 acl?
【发布时间】:2014-11-26 14:47:12
【问题描述】:

我正在使用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?

【问题讨论】:

    标签: symfony amazon-s3 acl gaufrette


    【解决方案1】:

    好吧,伙计们,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 用户对存储桶没有正确的权限!所以请确保您的用户政策允许访问存储桶

    【讨论】:

    • 上帝保佑你!
    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多