Updated on 2023-11-09 GMT+08:00

Managing Object ACLs

If you have any questions during development, post them on the Issues page of GitHub. For details about parameters and usage of each API, see the API Reference.

Object ACLs, similar to bucket ACLs, support pre-defined access control policies and direct configuration. For details, see Managing Bucket ACLs.

An object ACL can be configured in any of the following ways:

  1. Specify a pre-defined access control policy during object upload.
  2. Call ObsClient.setObjectAcl to specify a pre-defined access control policy.
  3. Call ObsClient.setObjectAcl to set the ACL directly.

Specifying a Pre-defined Access Control Policy During Object Upload

Sample code:

// Import the OBS library.
// Use npm to install the client.
var ObsClient = require('esdk-obs-nodejs');
// Use the source code to install the client.
// var ObsClient = require('./lib/obs');

// Create an ObsClient instance.
var obsClient = new ObsClient({
       //Obtain an AK/SK pair using environment variables or import the AK/SK pair in other ways. Using hard coding may result in leakage.
       //Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
       access_key_id: process.env.ACCESS_KEY_ID,
       secret_access_key: process.env.SECRET_ACCESS_KEY,
       server : 'https://your-endpoint'
});

obsClient.putObject({
       Bucket : 'bucketname',
       Key : 'objectname',
       Body : 'Hello OBS',
       // Set the object ACL to public-read.
       ACL : obsClient.enums.AclPublicRead
}, (err, result) => {
       if(err){              
          console.error('Error-->' + err);       
       }else{              
          console.log('Status-->' + result.CommonMsg.Status);        
       }
});

Setting a Pre-defined Access Control Policy for an Object

Sample code:

// Import the OBS library.
// Use npm to install the client.
var ObsClient = require('esdk-obs-nodejs');
// Use the source code to install the client.
// var ObsClient = require('./lib/obs');

// Create an ObsClient instance.
var obsClient = new ObsClient({
       //Obtain an AK/SK pair using environment variables or import the AK/SK pair in other ways. Using hard coding may result in leakage.
       //Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
       access_key_id: process.env.ACCESS_KEY_ID,
       secret_access_key: process.env.SECRET_ACCESS_KEY,
       server : 'https://your-endpoint'
});

obsClient.setObjectAcl({
       Bucket : 'bucketname',
       Key : 'objectname',
       // Set the object ACL to private.
       ACL : obsClient.enums.AclPrivate
}, (err, result) => {
       if(err){              
          console.error('Error-->' + err);       
       }else{              
          console.log('Status-->' + result.CommonMsg.Status);        
       }
});

Use the ACL parameter to specify ACL for an object.

Directly Setting an Object ACL

Sample code:

// Import the OBS library.
// Use npm to install the client.
var ObsClient = require('esdk-obs-nodejs');
// Use the source code to install the client.
// var ObsClient = require('./lib/obs');

// Create an ObsClient instance.
var obsClient = new ObsClient({
       //Obtain an AK/SK pair using environment variables or import the AK/SK pair in other ways. Using hard coding may result in leakage.
       //Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
       access_key_id: process.env.ACCESS_KEY_ID,
       secret_access_key: process.env.SECRET_ACCESS_KEY,
       server : 'https://your-endpoint'
});

obsClient.setObjectAcl({
       Bucket : 'bucketname',
       Key : 'objectname',
       // Set the object owner.  
       Owner:{'ID':'ownerid'},
       Grants:[                               
            // Grant all permissions to a specified user.
           { Grantee : {Type : 'CanonicalUser',ID : 'userid'}, Permission : obsClient.enums.PermissionFullControl},
               // Grant the READ permission to all users.
           { Grantee: {Type : 'Group', URI : obsClient.enums.GroupAllUsers}, Permission : obsClient.enums.PermissionRead}, 
       ]
}, (err, result) => {
       if(err){              
          console.error('Error-->' + err);       
       }else{              
          console.log('Status-->' + result.CommonMsg.Status);        
       }
});
  • Use the Owner parameter to specify the object owner and use the Grants parameter to grant permissions for authorized users.
  • The owner or grantee ID needed in the ACL indicates the account ID, which can be viewed on the My Credentials page of OBS Console.
  • OBS buckets support the following grantee group:
    • All users: ObsClient.enums.GroupAllUsers

Obtaining an Object ACL

You can call ObsClient.getObjectAcl to obtain an object ACL. Sample code is as follows:

// Import the OBS library.
// Use npm to install the client.
var ObsClient = require('esdk-obs-nodejs');
// Use the source code to install the client.
// var ObsClient = require('./lib/obs');

// Create an ObsClient instance.
var obsClient = new ObsClient({
       //Obtain an AK/SK pair using environment variables or import the AK/SK pair in other ways. Using hard coding may result in leakage.
       //Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
       access_key_id: process.env.ACCESS_KEY_ID,
       secret_access_key: process.env.SECRET_ACCESS_KEY,
       server : 'https://your-endpoint'
});

obsClient.getObjectAcl({
       Bucket : 'bucketname',
       Key : 'objectname'
}, (err, result) => {
       if(err){              
          console.error('Error-->' + err);       
       }else{              
          console.log('Status-->' + result.CommonMsg.Status);
                 if(result.CommonMsg.Status < 300 && result.InterfaceResult){
                           console.log('Owner[ID]-->' + result.InterfaceResult.Owner.ID);
                           console.log('Owner[Name]-->' + result.InterfaceResult.Owner.Name);
                           for(let i=0;i<result.InterfaceResult.Grants.length;i++){
                                  console.log('Grant[' + i + ']:');
                                  console.log('Grantee[ID]-->' + result.InterfaceResult.Grants[i]['Grantee']['ID']);
                                  console.log('Grantee[URI]-->' + result.InterfaceResult.Grants[i]['Grantee']['URI']);
                                  console.log('Permission-->' + result.InterfaceResult.Grants[i]['Permission']);
                           }
                 }            
       }
});