【问题标题】:Make AWS s3 directory public using php使用 php 将 AWS s3 目录公开
【发布时间】:2018-02-26 14:52:24
【问题描述】:

我知道 S3 中没有文件夹的概念,它使用平面文件结构。不过,为了简单起见,我将使用术语“文件夹”。

前提条件:

  • 一个名为 foo 的 s3 存储桶
  • 文件夹 foo 已使用 AWS 管理控制台公开
  • 阿帕奇
  • PHP 5
  • 标准 AWS 开发工具包

问题:

可以使用 AWS PHP 开发工具包上传文件夹。但是,该文件夹只能由上传该文件夹的用户访问,而不是像我希望的那样公开可读。

程序:

$sharedConfig = [
    'region'  => 'us-east-1',
    'version' => 'latest',
    'visibility' => 'public',
    'credentials' => [
        'key'    => 'xxxxxx',
        'secret' => 'xxxxxx',
    ],
];

// Create an SDK class used to share configuration across clients.
$sdk = new Aws\Sdk($sharedConfig);

// Create an Amazon S3 client using the shared configuration data.
$client = $sdk->createS3();

$client->uploadDirectory("foo", "bucket", "foo", array(
            'params'      => array('ACL' => 'public-read'),
            'concurrency' => 20,
            'debug'       => true
        ));

成功标准:

我可以使用“静态”链接访问上传文件夹中的文件。外汇:

https://s3.amazonaws.com/bucket/foo/001.jpg

【问题讨论】:

    标签: php amazon-web-services amazon-s3


    【解决方案1】:

    我通过使用定义的“执行前”功能修复了它。

      $result = $client->uploadDirectory("foo", "bucket", "foo", array(
                'concurrency' => 20,
                'debug'       => true,
                'before' => function (\Aws\Command $command) {
                $command['ACL'] = strpos($command['Key'], 'CONFIDENTIAL') === false
                    ? 'public-read'
                    : 'private';
            }
    
            ));
    

    【讨论】:

      【解决方案2】:

      使用可以使用这个:

      $s3->uploadDirectory('images', 'bucket', 'prefix',
                  ['params' => array('ACL' => 'public-read')]
              );
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-12-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-08
        • 2017-04-15
        相关资源
        最近更新 更多