Manage file access permissions (Swift SDK)
Use the OSS Swift SDK to set and retrieve the access control list (ACL) of an object.
Prerequisites
Before you begin, ensure that you have:
The
oss:PutObjectAclpermission to set an object's ACLThe
oss:GetObjectAclpermission to retrieve an object's ACLThe
OSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRETenvironment variables set with your Alibaba Cloud credentials
For details on granting these permissions, see Grant custom permissions to a RAM user.
Set object ACL
The sample code uses the cn-hangzhou region ID and a public endpoint. If you access OSS from another Alibaba Cloud service in the same region, use an internal endpoint instead. For supported regions and endpoints, see Regions and endpoints.
import AlibabaCloudOSS
import Foundation
@main
struct Main {
static func main() async {
do {
// Specify the region where the bucket is located. Example: cn-hangzhou for China (Hangzhou).
let region = "cn-hangzhou"
// Specify the bucket name.
let bucket = "yourBucketName"
// Optional. Specify the domain name used to access OSS. Example: https://oss-cn-hangzhou.aliyuncs.com for China (Hangzhou).
let endpoint: String? = nil
// Specify the name of the object for which you want to set the ACL. Example: document.txt.
let key = "yourObjectName"
// Specify the ACL type.
let objectAcl = "yourObjectAcl"
// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
let credentialsProvider = EnvironmentCredentialsProvider()
// Configure the OSS client.
let config = Configuration.default()
.withRegion(region)
.withCredentialsProvider(credentialsProvider)
// Set the endpoint.
if let endpoint = endpoint {
config.withEndpoint(endpoint)
}
// Create an OSS client instance.
let client = Client(config)
// Set the ACL.
let result = try await client.putObjectAcl(
PutObjectAclRequest(
bucket: bucket,
key: key,
objectAcl: objectAcl
)
)
print("result:\n\(result)")
} catch {
print("error: \(error)")
}
}
}Get object ACL
The sample code uses the cn-hangzhou region ID and a public endpoint. If you access OSS from another Alibaba Cloud service in the same region, use an internal endpoint instead. For supported regions and endpoints, see Regions and endpoints.
import AlibabaCloudOSS
import Foundation
@main
struct Main {
static func main() async {
do {
// Specify the region where the bucket is located. Example: cn-hangzhou for China (Hangzhou).
let region = "cn-hangzhou"
// Specify the bucket name.
let bucket = "yourBucketName"
// Optional. Specify the domain name used to access OSS. Example: https://oss-cn-hangzhou.aliyuncs.com for China (Hangzhou).
let endpoint: String? = nil
// Specify the name of the object whose ACL you want to retrieve. Example: document.txt.
let key = "yourObjectName"
// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
let credentialsProvider = EnvironmentCredentialsProvider()
// Configure the OSS client.
let config = Configuration.default()
.withRegion(region)
.withCredentialsProvider(credentialsProvider)
// Set the endpoint.
if let endpoint = endpoint {
config.withEndpoint(endpoint)
}
// Create an OSS client instance.
let client = Client(config)
// Retrieve the ACL.
let result = try await client.getObjectAcl(
GetObjectAclRequest(
bucket: bucket,
key: key
)
)
print("result:\n\(result)")
} catch {
print("error: \(error)")
}
}
}References
Complete sample code for setting the ACL of a file: GitHub sample
API reference for setting the ACL of a file: PutObjectACL
Complete sample code for retrieving the ACL of a file: GitHub sample
API reference for retrieving the ACL of a file: GetObjectACL