• Object Storage Service

obs
  1. Help Center
  2. Object Storage Service
  3. Developer Guide (Node.js SDK)
  4. Versioning Management
  5. Setting or Obtaining a Versioning Object ACL

Setting or Obtaining a Versioning Object ACL

Directly Setting a Versioning Object ACL

You can call ObsClient.setObjectAcl to set the ACL for a versioning object by specifying the version ID (VersionId). 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.setObjectAcl({
       Bucket : 'bucketname',
       Key : 'objectkey',
       VersionId : 'versionid',
       // Set the versioning object ACL to public-read by specifying the pre-defined access control policy.
       ACL : obsClient.enums.AclPublicRead
}, (err, result) => {
       if(err){
              console.log('Error-->' + err);
       }else{
              console.log('Status-->' + result.CommonMsg.Status);
       }
});

obsClient.setObjectAcl({
       Bucket : 'bucketname',
       Key : 'objectkey',
       VersionId : 'versionid',
       // Set the object owner.  
       Owner:{'ID':'ownerid','Name':'ownername'},
       Grants:{                         
         Grant:[                               
               // 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.log('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 a Versioning Object ACL

You can call ObsClient.getObjectAcl to obtain the ACL of a versioning object by specifying the version ID (VersionId). 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',
       VersionId : 'versionid'
}, (err, result) => {
       if(err){
              console.log('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']); 
                  } 
              }
       }
});