Manage file access permissions (Swift SDK)
This topic describes how to use the Swift software development kit (SDK) to set and retrieve the access control list (ACL) of a file.
Notes
-
The sample code in this topic uses the China (Hangzhou) region ID (
cn-hangzhou) and a public endpoint. If you access OSS from another Alibaba Cloud service in the same region, use an internal endpoint. For more information about the regions and endpoints that OSS supports, see Regions and endpoints. -
To set the access permissions of an object, you must have the
oss:PutObjectAclpermission. To retrieve the access permissions of an object, you must have theoss:GetObjectAclpermission. For more information, see Grant custom permissions to a RAM user.
Set file access permissions
You can use the following code to set the access permissions for a file.
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) // Set the region where the bucket is located.
.withCredentialsProvider(credentialsProvider) // Set the access credentials.
// 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)") // Print the error message.
}
}
}
Get file access permissions
You can use the following code to retrieve the access permissions for a file.
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 get. 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) // Set the region where the bucket is located.
.withCredentialsProvider(credentialsProvider) // Set the access credentials.
// Set the endpoint.
if let endpoint = endpoint {
config.withEndpoint(endpoint)
}
// Initialize an OSS client instance.
let client = Client(config)
// Call the operation.
let result = try await client.getObjectAcl(
GetObjectAclRequest(
bucket: bucket,
key: key
)
)
print("result:\n\(result)") // Print the configuration information.
} catch {
print("error: \(error)") // Print the error message.
}
}
}
References
-
For the complete sample code for setting the access control list of a file, see GitHub sample.
-
For more information about the API operation for setting the access control list of a file, see PutObjectACL.
-
For the complete sample code for retrieving the access control list of a file, see GitHub sample.
-
For more information about the API operation for retrieving the access control list of a file, see GetObjectACL.