使用 php 公开 AWS s3 目录


Make AWS s3 directory public using php

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

前提 条件:

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

问题:

可以使用 AWS PHP SDK 上传文件夹。但是,该文件夹只能由上传该文件夹的用户访问,并且不能像我希望的那样公开读取。

程序:

$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

我通过使用定义的"执行前"函数修复了它。

  $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';
        }
        ));

使用可以使用这个:

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