获取桶访问权限

GetACL操作可以获取bucket的access control list(ACL)信息。bucket的ACL可以在创建的时候设置并且通过API查看,用户需要具有READ_ACP(读取bucket ACL信息)权限才可以查询bucket的ACL信息。

代码示例:

using System;
using System.Threading.Tasks;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;

namespace DotNetSDK.BucketOperation
{
    public class GetBucketACLExample
    {
        public static async Task GetBucketACL()
        {
            var accessKey = "YOUR_ACCESS_KEY";
            var secretKey = "YOUR_SECRET_KEY";
            var endpoint = "YOUR_ENDPOINT";
            var bucketName = "EXAMPLE_BUCKET";
            try
            {
                var credentials = new BasicAWSCredentials(accessKey, secretKey);
                var conf = new AmazonS3Config
                {
                    ServiceURL = endpoint
                };
                var s3Client = new AmazonS3Client(credentials, conf);
                var getAclRequest = new GetACLRequest()
                {
                    BucketName = bucketName
                };

                var result = await s3Client.GetACLAsync(getAclRequest);
                if (result.HttpStatusCode != System.Net.HttpStatusCode.OK)
                {
                    Console.WriteLine("fail to get ACL of bucket {0}, HttpStatusCode:{1}, ErrorCode:{2}.", bucketName, (int) result.HttpStatusCode, result.HttpStatusCode);
                    return;
                }

                foreach (var grant in result.AccessControlList.Grants)
                {
                    Console.WriteLine("Type:{0}, CanonicalUser:{1}, DisplayName:{2}, EmailAddress:{3}, URI:{4}, Permission:{5}",
                        grant.Grantee.Type, grant.Grantee.CanonicalUser, grant.Grantee.DisplayName, grant.Grantee.EmailAddress, grant.Grantee.URI, grant.Permission.Value);
                }

                Console.WriteLine("OwnerId:{0}, OwnerDisplayName:{1}.", result.AccessControlList.Owner.Id, result.AccessControlList.Owner.DisplayName);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}

GetACLRequest可设置的参数如下:

参数 类型 说明 是否必要
BucketName string bucket的名称。

GetACL返回的结果如下:

属性名 类型 说明
Grants List<S3Grant> Grant信息的数组,包含了每项授权和被授予人的信息
Owner Owner bucket的所有者信息。

 响应结果:

HTTP状态码 响应码 描述
200 OK 操作成功。
403 Forbidden 用户没有权限执行操作。
404 NotFound 操作指定的bucket不存在。