Tüm Ürünler
Search
Belge Merkezi

Object Storage Service:Bucket policy (Node.js SDK)

Son Güncelleme:Oct 01, 2026

Paket ilkeleri; Alibaba Cloud hesapları, Resource Access Management (RAM) kullanıcıları ve RAM rolleri gibi kimliği doğrulanmış kullanıcılara veya anonim kullanıcılara belirli Object Storage Service (OSS) kaynaklarına erişim izni vermenizi veya bu erişimi kısıtlamanızı sağlar. Örneğin, başka bir Alibaba Cloud hesabındaki bir RAM kullanıcısına belirtilen OSS kaynakları için salt okunur erişim izni verebilirsiniz.

Dikkat Edilmesi Gerekenler

  • Bir paket ilkesini yapılandırmadan önce bu özelliği tanıyın. Daha fazla bilgi için bkz. Paket ilkesi.

  • Bir paket ilkesi ayarlamak için oss:PutBucketPolicy iznine sahip olmanız gerekir. Bir paket ilkesini almak için oss:GetBucketPolicy iznine sahip olmanız gerekir. Bir paket ilkesini silmek için oss:DeleteBucketPolicy iznine sahip olmanız gerekir. Daha fazla bilgi için bkz. RAM kullanıcısına özel erişim ilkeleri atama.

Paket ilkesi ayarlama

Aşağıda, bir paket ilkesi ayarlamaya yönelik örnek kod verilmiştir:

const OSS = require('ali-oss')

const client = new OSS({
  // Set region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set region to oss-cn-hangzhou.
  region: 'yourregion',
  // Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Specify the bucket name. For example, examplebucket.
  bucket: 'examplebucket'
});
// In this example, the resource owner (bucket owner with UID 174649585760xxxx) uses a bucket policy to grant a RAM user (with UID 20214760404935xxxx) the permissions to list and get 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()

Paket ilkesini alma

Aşağıda, bir paket ilkesini almaya yönelik örnek kod verilmiştir:

const OSS = require('ali-oss')

const client = new OSS({
  // Set region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set region to oss-cn-hangzhou.
  region: 'yourregion',
  // Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Specify the bucket name.
  bucket: 'yourbucketname'
});

// Get the bucket policy configuration.
async function getPolicy() {
  const result = await client.getBucketPolicy('yourbucketname');
  console.log(result.policy)
}

getPolicy()

Paket ilkesini silme

Aşağıda, bir paket ilkesini silmeye yönelik örnek kod verilmiştir:

const OSS = require('ali-oss')

const client = new OSS({
  // Set region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set region to oss-cn-hangzhou.
  region: 'yourregion',
  // Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Specify the bucket name.
  bucket: 'yourbucketname'
});

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

deletePolicy()

İlgili Konular

  • Tam örnek kodlar için bkz. GitHub.

  • Bir paket ilkesi ayarlamak için kullanılan API işlemi hakkında bilgi edinmek için bkz. PutBucketPolicy.

  • Bir paket ilkesini almak için kullanılan API işlemi hakkında bilgi edinmek için bkz. GetBucketPolicy.

  • Bir paket ilkesini silmek için kullanılan API işlemi hakkında bilgi edinmek için bkz. DeleteBucketPolicy.