Manage object access permissions (C# SDK V2)

Updated at:

This topic describes how to manage the access control list (ACL) of an object in a versioning-enabled bucket.

Precautions

  • The sample code in this topic uses the public endpoint of the China (Hangzhou) region (cn-hangzhou) as an example. If you want to access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For more information about the mappings between OSS-supported regions and endpoints, see Regions and endpoints.

  • To set the access permissions for an object, you must have the oss:PutObjectAcl permission. To obtain the access permissions for an object, you must have the oss:GetObjectAcl permission. For more information, see Grant custom permissions to a RAM user.

Read/write permission types

An object has the following four types of ACLs:

Access permission

Description

Access permission value

Inherit from bucket

The object inherits the access permissions of the bucket.

default

Private

Only the object owner and authorized users can read and write the object. Other users cannot access the object.

private

Public-read

Only the object owner and authorized users can read and write the object. Other users can only read the object. Use this permission with caution.

public-read

Public-read-write

All users can read and write the object. Use this permission with caution.

public-read-write

The ACL of an object takes precedence over the ACL of the bucket in which the object is stored. For example, if the ACL of the bucket is private and the ACL of the object is public-read-write, all users can read and write the object. If no ACL is configured for an object, the object inherits the ACL of the bucket.

Sample code

Note
  • By default, PutObjectAcl is called to set the ACL of the current version of an object. If the current version of the object is a delete marker, OSS returns a 404 Not Found error. You can specify versionId in the request to set the ACL of a specific version of the object.

  • By default, GetObjectAcl is called to obtain the ACL of the current version of an object. If the current version of the object is a delete marker, OSS returns a 404 Not Found error. You can specify versionId in the request to obtain the ACL of a specific version of the object.

You can use the following sample code to set and obtain the ACL of an object.

using OSS = AlibabaCloud.OSS.V2;  // Create an alias for Alibaba Cloud OSS SDK to simplify subsequent use.

var region = "cn-hangzhou";  // Required. The region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou.
var endpoint = null as string;  // Optional. The endpoint used to access OSS. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
var bucket = "your bucket name";  // Required. The name of the destination bucket.
var key = "your object name";  // Required. The name of the destination object.
var acl = "your object Acl";  // Required. The ACL of the destination object. Valid values: default, private, public-read, and public-read-write.
var versionId = "your object versionId";  // Required. The x-oss-version-id of the destination object.

// Load the default configurations of the OSS SDK. The configurations automatically read credential information (such as AccessKey) from environment variables.
var cfg = OSS.Configuration.LoadDefault();
// Explicitly set to use environment variables to obtain credentials for identity verification (Format: OSS_ACCESS_KEY_ID, OSS_ACCESS_KEY_SECRET).
cfg.CredentialsProvider = new OSS.Credentials.EnvironmentVariableCredentialsProvider();
// Set the region of the bucket.
cfg.Region = region;   
// If an endpoint is specified, it overwrites the default endpoint. 
if(endpoint != null) 
{
    cfg.Endpoint = endpoint;
} 

// Create an OSS client instance using the configuration information.
using var client = new OSS.Client(cfg); 

// Call the PutObjectAclAsync method to set the ACL of the destination object.
var result1 = await client.PutObjectAclAsync(new OSS.Models.PutObjectAclRequest()
{
    Bucket = bucket,
    Key = key,
    Acl = acl,
    VersionId = versionId,
});

// Call the GetObjectAclAsync method to obtain the ACL information of the destination object.
var result2 = await client.GetObjectAclAsync(new OSS.Models.GetObjectAclRequest()
{
    Bucket = bucket,
    Key = key,
    VersionId = versionId,
});

// Print the result.
Console.WriteLine("GetObjectAcl done");  // A message indicating that the operation is complete.
Console.WriteLine($"StatusCode: {result2.StatusCode}");  // The HTTP status code.
Console.WriteLine($"RequestId: {result2.RequestId}");  // The request ID, which is used for troubleshooting in Alibaba Cloud.
Console.WriteLine("Response Headers:");  // The response header.
result2.Headers.ToList().ForEach(x => Console.WriteLine(x.Key + " : " + x.Value));  // Traverse and print all response headers.
Console.WriteLine($"Acl: {result2.Acl}");   // The ACL information of the destination object.