获取对象权限信息

本SDK提供GetObjectAcl类用于获取对象权限信息。具体内容详见 官方文档

参数说明

  • Bucket: 文件所在的存储空间
  • Key: 文件在存储空间内的名称

访问权限定义(ACL)

US3 ACL AWS S3 Canned ACL
private private
public-read public-read
public-read-write public-read-write
不支持 aws-exec-read
authenticated-read
bucket-owner-read
bucket-owner-full-control
log-delivery-write

示例

执行该示例前请确保配置文件的正确性
以下代码段需要在上下文中运行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// 初始化客户端
try {
    $s3Client = new S3Client([
        'endpoint' => $endpoint,
        'region' => $region,
        'signature_version' => $signatureVersion,
        'force_path_style' => $forcePathStyle,
        'credentials' => [
            'key' => $accessKey,
            'secret' => $secretKey,
        ],
    ]);
    echo "Client initialized successfully!" . PHP_EOL;
} catch (InvalidArgumentException $e) {
    die("Failed to initialize Client: " . $e->getMessage());
}

try {
    // 获取对象的ACL
    $result = $s3Client->getObjectAcl([
        'Bucket' => $bucketName,
        'Key' => $keyName,
    ]);

    // 输出ACL信息
    echo "Object ACL:" . PHP_EOL;
    foreach ($result['Grants'] as $grant) {
        $permission = $grant['Permission'];
        $grantee = isset($grant['Grantee']['DisplayName']) ? $grant['Grantee']['DisplayName'] : $grant['Grantee']['URI'];
        echo "Grantee: $grantee, Permission: $permission" . PHP_EOL;
    }
} catch (AwsException $e) {
    echo "Error getting object ACL: " . $e->getMessage() . PHP_EOL;
}