• Object Storage Service

obs
  1. Help Center
  2. Object Storage Service
  3. Developer Guide (Node.js SDK)
  4. Object Management
  5. Managing Object ACLs

Managing Object ACLs

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 three modes:

  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.
var ObsClient = require('./lib/obs');

// Create an instance of ObsClient.
var obsClient = new ObsClient({
    access_key_id: '*** Provide your Access Key ***',       
    secret_access_key: '*** Provide your Secret Key ***',       
    server : 'yourdomainname'
});

obsClient.putObject({
       Bucket : 'bucketname',
       Key : 'objectkey',
       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.
var ObsClient = require('./lib/obs');

// Create an instance of ObsClient.
var obsClient = new ObsClient({
    access_key_id: '*** Provide your Access Key ***',       
    secret_access_key: '*** Provide your Secret Key ***',       
    server : 'yourdomainname'
});

obsClient.setObjectAcl({
       Bucket : 'bucketname',
       Key : 'objectkey',
       // 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);        
       }
});
NOTE:

Use the ACL parameter to specify ACL for an object.

Directly Setting an Object ACL

Sample code:

// Import the OBS library.
var ObsClient = require('./lib/obs');

// Create an instance of ObsClient.
var obsClient = new ObsClient({
    access_key_id: '*** Provide your Access Key ***',       
    secret_access_key: '*** Provide your Secret Key ***',       
    server : 'yourdomainname'
});

obsClient.setObjectAcl({
       Bucket : 'bucketname',
       Key : 'objectkey',
       // Set the object owner.  
       Owner:{'ID':'ownerid','Name':'ownername'},
       Grants:{                         
         Grant:[          
              // 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}, 
               // Grant the WRITE_ACP permission to authorized users.
               { Grantee: {Type : 'Group', URI : obsClient.enums.GroupAuthenticatedUsers}, Permission : obsClient.enums.PermissionWriteAcp}
         ]
       }
}, (err, result) => {
       if(err){              
          console.error('Error-->' + err);       
       }else{              
          console.log('Status-->' + result.CommonMsg.Status);        
       }
});
NOTE:
  • 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 Credential page of OBS Console.
  • OBS objects support two types of grantee groups:
    • Authorized users: ObsClient.enums.GroupAuthenticatedUsers
    • 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.
var ObsClient = require('./lib/obs');

// Create an instance of ObsClient.
var obsClient = new ObsClient({
    access_key_id: '*** Provide your Access Key ***',       
    secret_access_key: '*** Provide your Secret Key ***',       
    server : 'yourdomainname'
});

obsClient.getObjectAcl({
       Bucket : 'bucketname',
       Key : 'objectkey'
}, (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.Grant.length;i++){
                                  console.log('Grant[' + i + ']:');
                                  console.log('Grantee[ID]-->' + result.InterfaceResult.Grants.Grant[i]['Grantee']['ID']);
                                  console.log('Grantee[Name]-->' + result.InterfaceResult.Grants.Grant[i]['Grantee']['Name']);
                                  console.log('Grantee[URI]-->' + result.InterfaceResult.Grants.Grant[i]['Grantee']['URI']);
                                  console.log('Permission-->' + result.InterfaceResult.Grants.Grant[i]['Permission']);
                           }
                 }            
       }
});