このトピックでは、オブジェクト のACL を管理する方法について説明します。
次の表は、オブジェクトのアクセス制御リスト (ACL) に含まれるアクセス許可について説明しています。
アクセス許可 | 説明 | 値 |
---|---|---|
default | オブジェクトの ACL は、そのバケットの ACL と同一です。 | CannedAccessControlList.Default |
Private | オブジェクトを読み書きできるのは、オブジェクト所有者と許可ユーザーだけです。 | CannedAccessControlList.Private |
Public read | オブジェクトを読み書きできるのは、オブジェクト所有者と許可ユーザーだけです。 他のユーザーはオブジェクトを読むことしかできません。 この許可は慎重に承認してください。 | CannedAccessControlList.PublicRead |
Public read-write | すべてのユーザーがオブジェクトを読み書きできます。 この許可は慎重に承認してください。 | CannedAccessControlList.PublicReadWrite |
オブジェクトの ACL はバケットの ACL よりも優先されます。 たとえば、バケットの ACL が "Private" で、オブジェクトの ACL が "Public read-write" である場合、すべてのユーザーがそのオブジェクトを読み書きできます。 オブジェクトが ACL で設定されていない場合、既定ではオブジェクトの ACL はバケットの ACL です。
オブジェクトの ACL の設定
次のコードを実行して、指定したオブジェクトの ACL を設定できます。
// It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all APIs in OSS.
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
// Create an OSSClient instance.
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
// Configure the ACL for the object to public read.
ossClient.setObjectAcl("<yourBucketName>", "<yourObjectName>", CannedAccessControlList.PublicRead);
// Close your OSSClient.
ossClient.shutdown();
オブジェクトの ACL の取得
指定したオブジェクトの ACL を取得するには、次のコードを実行します。
// This example uses endpoint China East 1 (Hangzhou). Specify the actual endpoint based on your requirements.
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com.
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
// Create an OSSClient instance.
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
// Obtain the ACL for an object.
ObjectAcl objectAcl = ossClient.getObjectAcl("<yourBucketName>", "<yourObjectName>");
System.out.println(objectAcl.getPermission().toString());
// Close your OSSClient.
ossClient.shutdown();