すべてのプロダクト
Search
ドキュメントセンター

Object Storage Service:OSS SDK for Node.jsを使用してバケットポリシーを構成する

最終更新日:Nov 01, 2024

バケットポリシーを使用すると、Alibaba Cloudアカウント、RAMユーザー、RAMロールなどの匿名ユーザーまたは識別されたユーザーの特定のOSS (Object Storage Service) リソースへのアクセスを許可または制限できます。 たとえば、特定のOSSリソースに対する読み取り専用権限を別のAlibaba CloudアカウントのRAMユーザーに付与できます。

使用上の注意

  • バケットポリシーを設定する前に、この機能に精通していることを確認してください。 詳細については、「バケットポリシー」をご参照ください。

  • バケットポリシーを設定するには、oss:PutBucketPolicy権限が必要です。 バケットポリシーをクエリするには、oss:GetBucketPolicy権限が必要です。 バケットポリシーを削除するには、oss:DeleteBucketPolicy権限が必要です。 詳細については、「RAMユーザーへのカスタムポリシーのアタッチ」をご参照ください。

バケットポリシーの設定

次のサンプルコードは、バケットポリシーを設定する方法の例を示しています。

const OSS = require('ali-oss')

const client = new OSS({
  // 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 oss-cn-hangzhou. 
  region: 'yourregion',
  // 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. 
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Specify the name of the bucket. Example: examplebucket. 
  bucket: 'examplebucket'
});
// In the following example, the bucket owner whose UID is 174649585760xxxx uses a bucket policy to authorize a RAM user whose UID is 20214760404935xxxx to list all objects in the examplebucket bucket. 
const policy = {
  Version: '1',
  Statement: [
  {
      Action: ['oss:ListObjects', 'oss:GetObject'],
      Effect: 'Allow',
      Principal: ['20214760404935xxxx'],
      Resource: ['acs:oss:*:174649585760xxxx:examplebucket']
    }
  ]
};

async function putPolicy() {
  const result = await client.putBucketPolicy('examplebucket', policy);
  console.log(result)
}

putPolicy()

バケットポリシーの照会

次のサンプルコードは、バケットポリシーを照会する方法を示しています。

const OSS = require('ali-oss')

const client = new OSS({
  // 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 oss-cn-hangzhou. 
  region: 'yourregion',
  // 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. 
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Specify the name of the bucket. 
  bucket: 'yourbucketname'
});

// Query the configurations of the bucket policies. 
async function getPolicy() {
  const result = await client.getBucketPolicy('yourbucketname');
  console.log(result.policy)
}

getPolicy()

バケットポリシーの削除

次のサンプルコードは、バケットポリシーを削除する方法の例を示しています。

const OSS = require('ali-oss')

const client = new OSS({
  // 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 oss-cn-hangzhou. 
  region: 'yourregion',
  // 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. 
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Specify the name of the bucket. 
  bucket: 'yourbucketname'
});

// Delete the bucket policy. 
async function deletePolicy() {
  const result = await client.deleteBucketPolicy('yourbucketname');
  console.log(result)
}

deletePolicy()

関連ドキュメント

  • バケットポリシーの管理に使用される完全なサンプルコードについては、『GitHub』をご参照ください。

  • バケットポリシーを設定するために呼び出すことができるAPI操作の詳細については、「PutBucketPolicy」をご参照ください。

  • バケットポリシーを照会するために呼び出すAPI操作の詳細については、「GetBucketPolicy」をご参照ください。

  • バケットポリシーを削除するために呼び出すことができるAPI操作の詳細については、「DeleteBucketPolicy」をご参照ください。