AWS Code Sample
Catalog

s3ObjectAcl.php

s3ObjectAcl.php demonstrates how to set access control list (ACL) permissions for a file (or object) in an Amazon S3 bucket.

<?php /** * Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * This file is licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. A copy of * the License is located at * * http://aws.amazon.com/apache2.0/ * * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ require 'vendor/autoload.php'; use Aws\S3\S3Client; use Aws\Exception\AwsException; // Create a S3Client $s3Client = new S3Client([ 'region' => 'us-west-2', 'version' => '2006-03-01' ]); // Gets the access control list (ACL) of an object. $bucket = 'my-s3-bucket'; $key = 'my-object'; try { $resp = $s3Client->getObjectAcl([ 'Bucket' => $bucket, 'Key' => $key, ]); echo "Succeed in retrieving object ACL as follows: \n"; var_dump($resp); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; } // Use acl subresource to set the access control list (ACL) permissions // for an object that already exists in a bucket $params = [ 'ACL' => 'public-read', 'AccessControlPolicy' => [ // Information can be retrieved from `getObjectAcl` response 'Grants' => [ [ 'Grantee' => [ 'DisplayName' => '<string>', 'EmailAddress' => '<string>', 'ID' => '<string>', 'Type' => 'CanonicalUser', 'URI' => '<string>', ], 'Permission' => 'FULL_CONTROL', ], // ... ], 'Owner' => [ 'DisplayName' => '<string>', 'ID' => '<string>', ], ], 'Bucket' => $bucket, 'Key' => $key, ]; try { $resp = $s3Client->putObjectAcl($params); echo "Succeed in setting object ACL.\n"; } catch (AwsException $e) { // Display error message echo $e->getMessage(); echo "\n"; }

Sample Details

Service: s3

Last tested: 2018-09-20

Author: jschwarzwalder (AWS)

Type: full-example

On this page: