设置文件权限
文件权限的详细说明请参看
权限控制,您可以通过SetBucketAcl设置文件权限。
- using Aliyun.OSS;
- // 初始化OssClient
- var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
- /// <summary>
- /// 设置文件权限
- /// </summary>
- /// <param name="bucketName">文件所在存储空间的名称</param>
- /// <param name="key">文件的名称</param>
- public void SetObjectAclSample(string bucketName, string key)
- {
- try
- {
- client.SetObjectAcl(bucketName, key, CannedAccessControlList.PublicRead);
- Console.WriteLine("Set Object:{0} Acl succeeded ", key);
- }
- catch (Exception ex)
- {
- Console.WriteLine("Failed with error info: {0}", ex.Message);
- }
- }
说明:
- 文件的四种访问权限:private、public-read、public-read-write、default分别对应于CannedAccessControlList.Private、CannedAccessControlList.PublicRead、CannedAccessControlList.PublicReadWrite、CannedAccessControlList.Default。
- 完整代码参考GitHub。
读取文件权限
您可以通过GetBucketAcl读取文件权限。
- using Aliyun.OSS;
- // 初始化OssClient
- var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
- /// <summary>
- /// 读取文件权限
- /// </summary>
- /// <param name="bucketName">文件所在存储空间的名称</param>
- /// <param name="key">文件的名称</param>
- public void SetObjectAclSample(string bucketName, string key)
- {
- try
- {
- var result = client.GetObjectAcl(bucketName, key);
- Console.WriteLine("Get Object Acl succeeded,Id:{0} Acl:{1} succeeded",
- result.Owner.Id, result.ACL.ToString());
- }
- catch (Exception ex)
- {
- Console.WriteLine("Failed with error info: {0}", ex.Message);
- }
- }
说明: 完整代码参考
GitHub。