Anders Larsen
Anders Larsen

Reputation: 600

Make AWS s3 directory public using php

I know there is no concept of folders in S3, it uses a flat file structure. However, i will use the term "folder" for the sake of simplicity.

Preconditions:

The problem:

It's possible to upload a folder using the AWS PHP SDK. However, the folder is then only accessible by the user that uploaded the folder and not public readable as i would like it to be.

Procedure:

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

Success Criteria:

I would be able to access a file in the uploaded folder using a "static" link. Fx:

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

Upvotes: 0

Views: 1665

Answers (2)

Linh Pham
Linh Pham

Reputation: 25

Use can use this:

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

Upvotes: 0

Anders Larsen
Anders Larsen

Reputation: 600

I fixed it by using a defined "Before Execute" function.

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

        ));

Upvotes: 1

Related Questions