Manage bucket ACLs (Swift SDK)
A bucket is a container that stores objects. This topic describes how to use the Swift software development kit (SDK) to set and retrieve bucket access control lists (ACLs).
Notes
-
The sample code in this topic uses the China (Hangzhou) region (
cn-hangzhou) as an example. By default, a public endpoint is used. If you want to access Object Storage Service (OSS) from other Alibaba Cloud services in the same region, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints. -
To set a bucket ACL, you must have the
oss:PutBucketAclpermission. To retrieve a bucket ACL, you must have theoss:GetBucketAclpermission. For more information, see Grant custom permissions to a RAM user.
Set a bucket ACL
Use the following code to set the ACL of a bucket.
import AlibabaCloudOSS
import Foundation
@main
struct Main {
static func main() async {
do {
// Specify the region where the bucket is located. Example: China (Hangzhou) is cn-hangzhou.
let region = "cn-hangzhou"
// Specify the bucket name.
let bucket = "yourBucketName"
// Optional. Specify the domain name used to access OSS. For the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
let endpoint: String? = nil
// The ACL to set. Example: "public-read".
let acl = "public-read"
// Obtain access credentials from environment variables. Before you run the sample code, make sure 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.putBucketAcl( // Asynchronously call the API operation to set the bucket ACL.
PutBucketAclRequest( // Build the request.
bucket: bucket, // The destination bucket name.
acl: acl // The access control policy to set.
)
)
print("result:\n\(result)") // Print the API response.
} catch {
print("error: \(error)") // Print the error message.
}
}
}
Obtaining read/write permissions for a bucket
Use the following code to retrieve the ACL of a bucket.
import AlibabaCloudOSS
import Foundation
@main
struct Main {
static func main() async {
do {
// Specify the region where the bucket is located. Example: China (Hangzhou) is cn-hangzhou.
let region = "cn-hangzhou"
// Specify the bucket name.
let bucket = "yourBucketName"
// Optional. Specify the domain name used to access OSS. For the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
let endpoint: String? = nil
// Obtain access credentials from environment variables. Before you run the sample code, make sure 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 GetBucketAcl API operation.
let result = try await client.getBucketAcl(
GetBucketAclRequest( // Build the request.
bucket: bucket // The destination bucket name.
)
)
print("result:\n\(result)") // Print the ACL configuration.
} catch {
print("error: \(error)") // Print the error message.
}
}
}
References
-
For the complete sample code that shows how to set a bucket ACL, see GitHub sample.
-
For more information about the API operation used to set a bucket ACL, see PutBucketAcl.
-
For the complete sample code that shows how to retrieve a bucket ACL, see GitHub sample.
-
For more information about the API operation used to retrieve a bucket ACL, see GetBucketAcl.