コード例 #1
0
ファイル: BucketACL.cs プロジェクト: zsp7280/cos-snippets
        /// 获取存储桶 ACL
        public void GetBucketAcl()
        {
            //.cssg-snippet-body-start:[get-bucket-acl]
            try
            {
                string bucket = "examplebucket-1250000000"; //格式:BucketName-APPID
                GetBucketACLRequest request = new GetBucketACLRequest(bucket);
                //执行请求
                GetBucketACLResult result = cosXml.GetBucketACL(request);
                //存储桶的 ACL 信息
                AccessControlPolicy acl = result.accessControlPolicy;
            }
            catch (COSXML.CosException.CosClientException clientEx)
            {
                //请求失败
                Console.WriteLine("CosClientException: " + clientEx);
            }
            catch (COSXML.CosException.CosServerException serverEx)
            {
                //请求失败
                Console.WriteLine("CosServerException: " + serverEx.GetInfo());
            }

            //.cssg-snippet-body-end
        }
コード例 #2
0
        public Task <AccessMode> GetBucketAclAsync(string bucketName)
        {
            if (string.IsNullOrEmpty(bucketName))
            {
                throw new ArgumentNullException(nameof(bucketName));
            }
            bucketName = ConvertBucketName(bucketName);
            GetBucketACLRequest request = new GetBucketACLRequest(bucketName);
            //执行请求
            GetBucketACLResult result = _client.GetBucketACL(request);
            //存储桶的 ACL 信息
            AccessControlPolicy acl = result.accessControlPolicy;

            bool isPublicRead  = false;
            bool isPublicWrite = false;

            if (acl != null &&
                acl.accessControlList != null &&
                acl.accessControlList.grants != null &&
                acl.accessControlList.grants.Count > 0)
            {
                foreach (var item in acl.accessControlList.grants)
                {
                    if (string.IsNullOrEmpty(item.grantee.uri))
                    {
                        continue;
                    }
                    if (item.grantee.uri.Contains("allusers", StringComparison.OrdinalIgnoreCase))
                    {
                        switch (item.permission.ToLower())
                        {
                        case "read":
                            isPublicRead = true;
                            break;

                        case "write":
                            isPublicWrite = true;
                            break;
                        }
                    }
                }
            }

            //结果
            if (isPublicRead && !isPublicWrite)
            {
                return(Task.FromResult(AccessMode.PublicRead));
            }
            else if (isPublicRead && isPublicWrite)
            {
                return(Task.FromResult(AccessMode.PublicReadWrite));
            }
            else if (!isPublicRead && isPublicWrite)
            {
                return(Task.FromResult(AccessMode.Private));
            }
            else
            {
                return(Task.FromResult(AccessMode.Private));
            }
        }