public Task <AccessMode> GetBucketAclAsync(string bucketName) { if (string.IsNullOrEmpty(bucketName)) { throw new ArgumentNullException(nameof(bucketName)); } GetBucketAclRequest request = new GetBucketAclRequest { BucketName = bucketName, }; GetBucketAclResponse response = _client.GetBucketAcl(request); if (response.StatusCode != HttpStatusCode.OK || response.AccessControlList == null) { throw new Exception($"Get bucket '{bucketName}' ACL failed. response code is {response.StatusCode}, response data: {JsonUtil.SerializeObject(response)}"); } bool hasAllUser = false; List <PermissionEnum> permissions = new List <PermissionEnum>(); foreach (var item in response.AccessControlList.Grants) { if (item.Grantee is GroupGrantee grantee) { if (grantee.GroupGranteeType == GroupGranteeEnum.AllUsers) { hasAllUser = true; if (item.Permission != null) { permissions.Add(item.Permission.Value); } } } } if (hasAllUser && permissions.Count > 0) { if (permissions.Any(p => p == PermissionEnum.FullControl) || permissions.Any(p => p == PermissionEnum.Write) || (permissions.Any(p => p == PermissionEnum.Read) && permissions.Any(p => p == PermissionEnum.Write))) { return(Task.FromResult(AccessMode.PublicReadWrite)); } else if (permissions.Any(p => p == PermissionEnum.Read)) { return(Task.FromResult(AccessMode.PublicRead)); } } return(Task.FromResult(AccessMode.Private)); }
static void GetBucketACL() { try { GetBucketAclRequest request = new GetBucketAclRequest() { BucketName = bucketName }; GetBucketAclResponse response = client.GetBucketAcl(request); Console.WriteLine("Get bucket acl response: {0}", response.StatusCode); foreach (Grant grant in response.AccessControlList.Grants) { Console.WriteLine("Grant permission: {0}", grant.Permission); } } catch (ObsException ex) { Console.WriteLine(string.Format("Exception errorcode: {0}, when get bucket acl.", ex.ErrorCode)); Console.WriteLine("Exception errormessage: {0}", ex.ErrorMessage); } }
/// <summary> /// 获取桶访问权限。 /// </summary> /// <param name="request">获取桶访问权限的请求参数。</param> /// <returns>获取桶访问权限的响应结果。</returns> public GetBucketAclResponse GetBucketAcl(GetBucketAclRequest request) { return(DoRequest <GetBucketAclRequest, GetBucketAclResponse>(request)); }
/// <summary> /// Start the asynchronous request for obtaining a bucket ACL. /// </summary> /// <param name="request">Parameters in a request for obtaining a bucket ACL</param> /// <param name="callback">Asynchronous request callback function</param> /// <param name="state">Asynchronous request status object</param> /// <returns>Response to the asynchronous request</returns> public IAsyncResult BeginGetBucketAcl(GetBucketAclRequest request, AsyncCallback callback, object state) { return(BeginDoRequest <GetBucketAclRequest>(request, callback, state)); }