Java管理存儲空間讀寫權(quán)限
存儲空間(Bucket)是存儲對象(Object)的容器。對象都隸屬于存儲空間。本文介紹如何設(shè)置和獲取存儲空間讀寫權(quán)限(ACL)。
注意事項
本文以華東1(杭州)外網(wǎng)Endpoint為例。如果您希望通過與OSS同地域的其他阿里云產(chǎn)品訪問OSS,請使用內(nèi)網(wǎng)Endpoint。關(guān)于OSS支持的Region與Endpoint的對應(yīng)關(guān)系,請參見OSS訪問域名、數(shù)據(jù)中心、開放端口。
本文以從環(huán)境變量讀取訪問憑證為例。如何配置訪問憑證,請參見Java配置訪問憑證。
本文以OSS域名新建OSSClient為例。如果您希望通過自定義域名、STS等方式新建OSSClient,請參見新建OSSClient。
要設(shè)置存儲空間讀寫權(quán)限,您必須有
oss:PutBucketAcl
權(quán)限;要獲取存儲空間讀寫權(quán)限,您必須有oss:GetBucketAcl
權(quán)限。具體操作,請參見為RAM用戶授權(quán)自定義的權(quán)限策略。
設(shè)置存儲空間讀寫權(quán)限
存儲空間的讀寫權(quán)限(ACL)有以下三類:
取值 | 描述 | 方法 |
私有 | 存儲空間的擁有者和授權(quán)用戶有該存儲空間內(nèi)的文件的讀寫權(quán)限,其他用戶沒有權(quán)限操作該存儲空間內(nèi)的文件。 | CannedAccessControlList.Private |
公共讀 | 存儲空間的擁有者和授權(quán)用戶有該存儲空間內(nèi)的文件的讀寫權(quán)限,其他用戶只有該存儲空間內(nèi)的文件的讀權(quán)限。請謹慎使用該權(quán)限。 | CannedAccessControlList.PublicRead |
公共讀寫 | 所有用戶都有該存儲空間內(nèi)的文件的讀寫權(quán)限。請謹慎使用該權(quán)限。 | CannedAccessControlList.PublicReadWrite |
以下代碼用于設(shè)置存儲空間的訪問權(quán)限:
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.CannedAccessControlList;
public class Demo {
public static void main(String[] args) throws Exception {
// Endpoint以華東1(杭州)為例,其它Region請按實際情況填寫。
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 從環(huán)境變量中獲取訪問憑證。運行本代碼示例之前,請確保已設(shè)置環(huán)境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// 填寫B(tài)ucket名稱,例如examplebucket。
String bucketName = "examplebucket";
// 填寫B(tài)ucket所在地域。以華東1(杭州)為例,Region填寫為cn-hangzhou。
String region = "cn-hangzhou";
// 創(chuàng)建OSSClient實例。
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
// 設(shè)置存儲空間的讀寫權(quán)限。例如將examplebucket的讀寫權(quán)限ACL設(shè)置為私有Private。
ossClient.setBucketAcl(bucketName, CannedAccessControlList.Private);
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}
獲取存儲空間讀寫權(quán)限
以下代碼用于獲取存儲空間的讀寫權(quán)限:
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.AccessControlList;
public class Demo {
public static void main(String[] args) throws Exception {
// Endpoint以華東1(杭州)為例,其它Region請按實際情況填寫。
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 從環(huán)境變量中獲取訪問憑證。運行本代碼示例之前,請確保已設(shè)置環(huán)境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// 填寫B(tài)ucket名稱,例如examplebucket。
String bucketName = "examplebucket";
// 填寫B(tài)ucket所在地域。以華東1(杭州)為例,Region填寫為cn-hangzhou。
String region = "cn-hangzhou";
// 創(chuàng)建OSSClient實例。
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
// 獲取存儲空間的讀寫權(quán)限。
AccessControlList acl = ossClient.getBucketAcl(bucketName);
System.out.println(acl.toString());
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}
相關(guān)文檔
關(guān)于管理存儲空間讀寫權(quán)限的完整示例代碼,請參見GitHub示例。
關(guān)于設(shè)置存儲空間讀寫權(quán)限的API接口說明,請參見PutBucketAcl。
關(guān)于獲取存儲空間讀寫權(quán)限的API接口說明,請參見GetBucketAcl。