このトピックでは、オブジェクトのアクセス制御リスト (ACL) を管理する方法について説明します。
使用上の注意
このトピックでは、中国 (杭州) リージョンのパブリックエンドポイントを使用します。 OSSと同じリージョンにある他のAlibaba CloudサービスからOSSにアクセスする場合は、内部エンドポイントを使用します。 OSSリージョンとエンドポイントの詳細については、「リージョンとエンドポイント」をご参照ください。
このトピックでは、OSSエンドポイントを使用してOSSClientインスタンスを作成します。 カスタムドメイン名またはSecurity Token Service (STS) を使用してOSSClientを作成する場合は、「OSSClientインスタンスの作成」をご参照ください。
オブジェクトのACLを設定するには、
oss:PutObjectAcl
権限が必要です。 オブジェクトACLをクエリするには、oss:GetObjectAcl
権限が必要です。 詳細については、「RAMユーザーへのカスタムポリシーのアタッチ」をご参照ください。
ACLタイプ
次の表に、オブジェクトに対して構成できるACLを示します。
オブジェクトのACLは、オブジェクトが格納されているバケットのACLよりも優先されます。 たとえば、プライベートバケット内のオブジェクトのACLがpublic-readに設定されている場合、匿名ユーザーを含むすべてのユーザーがオブジェクトを読み取ることができます。
ACLタイプ | 説明 | 値 |
バケットから継承 | オブジェクトのACLは、オブジェクトが格納されているバケットのACLと同じです。 これはオブジェクトのデフォルトACLです。 | CannedAccessControlList.Default |
プライベート | オブジェクトを読み書きできるのは、オブジェクト所有者だけです。 他のユーザーはオブジェクトにアクセスできません。 | CannedAccessControlList.Private |
公開読み取り | オブジェクト所有者のみがオブジェクトを書き込むことができます。 匿名ユーザーを含む他のユーザーは、オブジェクトのみを読み取ることができます。 警告 これにより、バケット内のデータへの不正アクセスと高コストが発生する可能性があります。 オブジェクトACLをpublic-readに設定する場合は注意してください。 | CannedAccessControlList.PublicRead |
パブリック読み取り /書き込み | 匿名ユーザーを含むすべてのユーザーがオブジェクトを読み書きできます。 警告 オブジェクトACLをこの値に設定すると、すべてのユーザーがオブジェクトにアクセスし、インターネット経由でオブジェクトにデータを書き込むことができます。 これにより、バケット内のデータへの不正アクセスと高コストが発生する可能性があります。 ユーザーが禁止されているデータまたは情報をバケットにアップロードすると、正当な利益と権利が侵害される可能性があります。 したがって、必要がない限り、バケットのACLをpublic-read-writeに設定しないことをお勧めします。 | CannedAccessControlList.PublicReadWrite |
オブジェクトの ACL の設定
次のサンプルコードは、オブジェクトのACLを設定する方法の例を示しています。
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize information about the account that is used to access OSS. */
/* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "yourEndpoint";
/* Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the name of the bucket. Example: examplebucket. */
std::string BucketName = "examplebucket";
/* Specify the full path of the object. Do not include the bucket name in the full path of the object. Example: exampledir/exampleobject.txt. */
std::string ObjectName = "exampledir/exampleobject.txt";
/* Initialize resources such as network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
/* Configure the ACL of the object. */
SetObjectAclRequest request(BucketName, ObjectName);
request.setAcl(CannedAccessControlList::Private);
auto outcome = client.SetObjectAcl(request);
if (!outcome.isSuccess()) {
/* Handle exceptions. */
std::cout << "SetObjectAcl fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
/* Release resources such as network resources. */
ShutdownSdk();
return 0;
}
オブジェクトのACLを照会する
次のサンプルコードは、オブジェクトのACLを照会する方法の例を示しています。
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize information about the account that is used to access OSS. */
/* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "yourEndpoint";
/* Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the name of the bucket. Example: examplebucket. */
std::string BucketName = "examplebucket";
/* Specify the full path of the object. Do not include the bucket name in the full path of the object. Example: exampledir/exampleobject.txt. */
std::string ObjectName = "exampledir/exampleobject.txt";
/* Initialize resources such as network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
/* Query the ACL of the object. */
GetObjectAclRequest request(BucketName, ObjectName);
auto outcome = client.GetObjectAcl(request);
if (!outcome.isSuccess()) {
/* Handle exceptions. */
std::cout << "GetObjectAcl fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
else {
std::cout << " GetObjectAcl success, Acl:" << outcome.result().Acl() << std::endl;
}
/* Release resources such as network resources. */
ShutdownSdk();
return 0;
}
関連ドキュメント
オブジェクトのACLを設定するために呼び出すAPI操作の詳細については、「PutObjectACL」をご参照ください。
オブジェクトのACLを照会するために呼び出すAPI操作の詳細については、「GetObjectACL」をご参照ください。