全部产品
Search
文档中心

Object Storage Service:Mengelola izin akses objek (Node.js SDK)

更新时间:Nov 30, 2025

Selain daftar kontrol akses (ACL) tingkat bucket, Object Storage Service (OSS) juga menyediakan ACL tingkat objek. Anda dapat mengonfigurasi ACL untuk suatu objek saat mengunggahnya atau mengubah ACL objek yang sudah ada kapan saja.

Jenis izin akses

Izin akses objek (ACL) tersedia dalam empat jenis berikut:

Access permission

Description

Access permission value

Inherit from bucket

Objek mewarisi izin akses dari bucket-nya.

default

Private

Pemilik objek dan pengguna yang berwenang memiliki izin baca dan tulis. Pengguna lain tidak memiliki izin untuk mengakses objek tersebut.

private

Public-read

Pemilik objek dan pengguna yang berwenang memiliki izin baca dan tulis. Pengguna lain hanya memiliki izin baca. Gunakan izin ini dengan hati-hati.

public-read

Public-read-write

Semua pengguna memiliki izin baca dan tulis pada objek tersebut. Gunakan izin ini dengan hati-hati.

public-read-write

Mengatur izin akses file

Kode berikut menunjukkan contoh cara mengatur ACL suatu objek:

const oss = require('ali-oss');

const client = 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 running the 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,
  // Set bucket to the name of your bucket.
  bucket: 'yourbucketname'
});
  
async function setACL() {
  try {
    // Set yourObjectName to the full path of the object. The full path cannot contain the bucket name.
    await client.putACL('yourObjectName', 'private');
    console.log('Set ACL successfully');
  } catch (e) {
    console.error(e);
  }
}

setACL();

Mendapatkan izin akses file

Kode berikut menunjukkan contoh cara menanyakan (query) ACL suatu objek:

const oss = require('ali-oss');

const client = 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 running the 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,
  // Set bucket to the name of your bucket.
  bucket: 'yourbucketname',
});
  
async function getACL() {
  try {
    // Set yourObjectName to the full path of the object. The full path cannot contain the bucket name.
    const result = await client.getACL('yourObjectName');
    console.log(result.acl);
  } catch (e) {
    console.error(e);
  }
}

getACL();

Referensi

  • Untuk kode contoh lengkap tentang pengelolaan izin akses objek, lihat contoh di GitHub.

  • Untuk informasi selengkapnya tentang operasi API untuk mengatur izin akses objek, lihat PutObjectACL.

  • Untuk informasi selengkapnya tentang operasi API untuk mengambil izin akses objek, lihat GetObjectACL.