Get Object ACL
Components
- Bucket Name
- Object Name
- .NET
- PHP
- Python
- Javascript
- GO
using Amazon;
using Amazon.S3;
using Amazon.S3.Model;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Reflection;
using Newtonsoft.Json;
namespace GetObjectAcl
{
class GetObjectAcl
{
private const string bucketName = "<BUCKET_NAME>";
private const string objectName = "<OBJECT_NAME>";
private static IAmazonS3 _s3Client;
public static void Main()
{
var awsCredentials = new Amazon.Runtime.BasicAWSCredentials("<ACCESS-KEY>", "<SECRET-KEY>");
var config = new AmazonS3Config { ServiceURL = "<ENDPOINT>" };
_s3Client = new AmazonS3Client(awsCredentials, config);
GetObjectAclAsync().Wait();
}
private static async Task GetObjectAclAsync()
{
try
{
GetACLResponse response = await _s3Client.GetACLAsync(new GetACLRequest
{
BucketName = bucketName,
Key = objectName
});
Console.WriteLine(JsonConvert.SerializeObject(response, Formatting.Indented));
}
catch (AmazonS3Exception amazonS3Exception)
{
Console.WriteLine("An AmazonS3Exception was thrown. Exception: " + amazonS3Exception.ToString());
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.ToString());
}
}
}
}
<?php
require('client.php');
$bucket = $config['sample_bucket'];
// Gets the access control list (ACL) of an object.
$key = 'file-uploaded-from-php-sdk.png';
try {
$resp = $client->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";
}
import boto3
import logging
from botocore.exceptions import ClientError
# Configure logging
logging.basicConfig(level=logging.INFO)
try:
# S3 resource
s3_resource = boto3.resource(
's3',
endpoint_url='endpoint_url',
aws_access_key_id='access_key',
aws_secret_access_key='secret_key'
)
except Exception as exc:
logging.error(exc)
else:
try:
bucket_name = 'sample_bucket_name'
object_name = 'sample_object_name'
bucket = s3_resource.Bucket(bucket_name)
object_acl = bucket.Object(object_name).Acl()
logging.info(object_acl.grants)
except ClientError as e:
logging.error(e)
// Import required AWS SDK clients and commands for Node.js
const { S3Client, GetObjectAclCommand } = require('@aws-sdk/client-s3');
// Create an S3 client service object
const s3 = new S3Client({
region: 'default',
endpoint: 'endpoint_url',
credentials: {
accessKeyId: 'access_key',
secretAccessKey: 'secret_key',
},
});
const run = async () => {
try {
const response = await s3.send(
new GetObjectAclCommand({
Bucket: 'sample_bucket',
Key: 'file.png',
})
);
console.log('Success', response.Grants);
} catch (err) {
console.log('Error', err);
}
};
run();
package main
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"fmt"
"os"
)
// Gets the ACL for a bucket object
//
// Usage:
// go run s3_get_object_acl.go BUCKET OBJECT
func main() {
if len(os.Args) != 3 {
exitErrorf("Bucket and object names required\nUsage: go run", os.Args[0], "BUCKET OBJECT")
}
bucket := os.Args[1]
key := os.Args[2]
// Initialize a session in us-west-2 that the SDK will use to load
// credentials from the shared credentials file ~/.aws/credentials.
sess, err := session.NewSession(&aws.Config{
Credentials: credentials.NewStaticCredentials("<ACCESS_KEY>", "<SECRET_KEY>", ""),
})
svc := s3.New(sess, &aws.Config{
Region: aws.String("default"),
Endpoint: aws.String("<ENDPOINT_URL>"),
})
// Get bucket ACL
result, err := svc.GetObjectAcl(&s3.GetObjectAclInput{Bucket: &bucket, Key: &key})
if err != nil {
exitErrorf(err.Error())
}
fmt.Println("Owner:", *result.Owner.DisplayName)
fmt.Println("")
fmt.Println("Grants")
for _, g := range result.Grants {
if g.Grantee.DisplayName != nil {
fmt.Println(" Grantee: ", *g.Grantee.DisplayName)
fmt.Println(" Type: ", *g.Grantee.Type)
fmt.Println(" Permission:", *g.Permission)
fmt.Println("")
} else if g.Grantee.URI != nil {
fmt.Println(" Type: ", *g.Grantee.Type)
fmt.Println(" URI: ", *g.Grantee.URI)
fmt.Println("")
}
}
}
func exitErrorf(msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, msg+"\n", args...)
os.Exit(1)
}
The following command can be used to execute the aforementioned code, presuming the code file is called s3_get_object_acl.go:
go run s3_get_object_acl.go BUCKET_NAME OBJECT