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 two modes:
- Specify a pre-defined access control policy during object upload.
- Call OBSSetObjectACLRequest to set the ACL directly.
Specifying a Pre-defined Access Control Policy During Object Upload
Sample code:
static OBSClient *client;
NSString *endPoint = @"your-endpoint";
NSString *SK = @"*** Provide your Secret Key ***";
NSString *AK = @"*** Provide your Access Key ***";
// 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];
// Upload a file.
OBSPutObjectWithFileRequest *request = [[OBSPutObjectWithFileRequest alloc]initWithBucketName:@"bucketname" objectKey:@"objectname" uploadFilePath:_imagePath];
//Set the access control policy of the object to public-read-write.
request.objectACLPolicy = OBSACLPolicyPublicRead;
request.uploadProgressBlock = ^(int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend) {
NSLog(@"%0.1f%%",(float)floor(totalBytesSent*10000/totalBytesExpectedToSend)/100);
};
[client putObject:request completionHandler:^(OBSPutObjectResponse *response, NSError *error){
NSLog(@"%@",response.etag);
}]; Directly Setting the Object ACL
Sample code:
static OBSClient *client;
NSString *endPoint = @"your-endpoint";
NSString *SK = @"*** Provide your Secret Key ***";
NSString *AK = @"*** Provide your Access Key ***";
// 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 object ACL directly.
// Initialize an instance of OBSUser.
OBSUser *owner = [[OBSUser alloc] initWithID:@"owner id"];
// Grantees
OBSACLGranteeUser *grantee = [[OBSACLGranteeUser alloc]initWithID:@"grantee id"];
//Object ACL
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];
}
// Set the object ACL.
OBSSetObjectACLRequest *request = [[OBSSetObjectACLRequest alloc]initWithBucketName:@"bucketname" objectKey:@"objectname" accessControlPolicy:policy];
[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 the Object ACL
You can call getObjectACL to obtain an object ACL. Sample code is as follows:
static OBSClient *client;
NSString *endPoint = @"your-endpoint";
NSString *SK = @"*** Provide your Secret Key ***";
NSString *AK = @"*** Provide your Access Key ***";
// 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 ACL of an object.
OBSGetObjectACLRequest *request = [[OBSGetObjectACLRequest alloc]initWithBucketName:@"bucketname" objectKey:@"objectname"];
[client getObjectACL:request completionHandler:^(OBSGetObjectACLResponse *response, NSError *error){
NSLog(@"%@",response);
}]; Last Article: Obtaining Object Properties
Next Article: Listing Objects
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.