Help Center/ Object Storage Service/ SDK Reference/ iOS/ Versioning Management/ Setting or Obtaining a Versioning Object ACL
Updated on 2024-05-08 GMT+08:00

Setting or Obtaining a Versioning Object ACL

Directly Setting a Versioning Object ACL

You can call OBSSetObjectACLRequest to input the version ID (versionID) to set the ACL for a versioning object. Sample code is as follows:

static OBSClient *client;
NSString *endPoint = @"your-endpoint";
// Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables AccessKeyID and SecretAccessKey.
// Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/eu/usermanual-ca/ca_01_0003.html.
char* ak_env = getenv("AccessKeyID");
char* sk_env = getenv("SecretAccessKey");
NSString *AK = [NSString stringWithUTF8String:ak_env];
NSString *SK = [NSString stringWithUTF8String:sk_env];
    
// Initialize identity authentication.
OBSStaticCredentialProvider *credentialProvider = [[OBSStaticCredentialProvider alloc] initWithAccessKey:AK secretKey:SK];
    
//Initialize service configuration.
OBSServiceConfiguration *conf = [[OBSServiceConfiguration alloc] initWithURLString:endPoint credentialProvider:credentialProvider];
    
// Initialize an instance of OBSClient.
client = [[OBSClient alloc] initWithConfiguration:conf];
    
// Set the access control policy for a versioning object.
OBSUser *owner = [[OBSUser alloc] initWithID:@"249e6c2bfbc74c928d5893895543029e"];
    
OBSACLGranteeUser *grantee = [[OBSACLGranteeUser alloc]initWithID:@"AKjdsjklSKLL/DSKDSLADLADLjdsjald231124"];
// Grant the FULL_CONTROL permission to authorized users.
OBSACLGrant *grant = [[OBSACLGrant alloc]initWithGrantee:grantee permission:OBSACLFull_Control];
OBSAccessControlPolicy *policy = [OBSAccessControlPolicy new];
policy.owner = owner;
[policy.accessControlList addObject:grant];
for(int i=0;i<=20;i++){
    [policy.accessControlList addObject:grant];
 }
    
OBSSetObjectACLRequest *request = [[OBSSetObjectACLRequest alloc]initWithBucketName:@"bucketname" objectKey:@"objectname" accessControlPolicy:policy];
    
request.versionID = @"Version ID of a versioning object";
    
[client setObjectACL:request completionHandler:^(OBSSetObjectACLResponse *response, NSError *error){
    NSLog(@"%@",response);
}];

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.

Obtaining a Versioning Object ACL

You can call OBSGetObjectACLRequest to pass the version ID (versionID) to obtain the ACL for a versioning object. Sample code is as follows:

static OBSClient *client;
NSString *endPoint = @"your-endpoint";
// Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables AccessKeyID and SecretAccessKey.
// Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/eu/usermanual-ca/ca_01_0003.html.
char* ak_env = getenv("AccessKeyID");
char* sk_env = getenv("SecretAccessKey");
NSString *AK = [NSString stringWithUTF8String:ak_env];
NSString *SK = [NSString stringWithUTF8String:sk_env];
    
// Initialize identity authentication.
OBSStaticCredentialProvider *credentialProvider = [[OBSStaticCredentialProvider alloc] initWithAccessKey:AK secretKey:SK];
    
//Initialize service configuration.
OBSServiceConfiguration *conf = [[OBSServiceConfiguration alloc] initWithURLString:endPoint credentialProvider:credentialProvider];
    
// Initialize an instance of OBSClient.
client = [[OBSClient alloc] initWithConfiguration:conf];
    
// Obtain the access control policy of a versioning object.
OBSGetObjectACLRequest *request = [[OBSGetObjectACLRequest alloc]initWithBucketName:@"bucketname" objectKey:@"objectname"];
// Set the version ID for a versioning object.
request.versionID = @"Version ID of a versioning object";
    
[client getObjectACL:request completionHandler:^(OBSGetObjectACLResponse *response, NSError *error){
    NSLog(@"%@",response);
}];