Agentic Bucket (OSS SDK for Swift)
This topic describes how to use the OSS SDK for Swift to create and manage Agentic Buckets, and create Bucket Spaces and upload objects.
Usage notes
-
To use Agentic Bucket features, you must install
alibabacloud-oss-swift-sdk-v2(OSS SDK for Swift 2.0), and import both of the following modules in Package.swift:AlibabaCloudOSSandAlibabaCloudOSSAgentic. -
The sample code in this topic uses the region ID
cn-hangzhouof the China (Hangzhou) region as an example and uses the public endpoint by default. To access OSS from other Alibaba Cloud services in the same region as OSS, use the internal endpoint. For more information about the mappings between the regions and endpoints supported by OSS, see Regions and endpoints. -
When you use the
AgenticBucketClientorBucketSpaceClient, you must set both of the following in the configuration:regionandaccountId. In the request, thebucketparameter is the bucket name prefix, and the SDK automatically resolves it to the full name. The full name of an Agentic Bucket is{prefix}-{accountId}-{region}-ab-apsr, and the full name of a Bucket Space is{prefix}-{accountId}-{region}-bs-apsr. -
You can access a Bucket Space in either of the following ways: Use
BucketSpaceClient. In the request, thebucketparameter is set to the Bucket Space name prefix, and the SDK automatically resolves it to the full name. Or, useBucketSpaceHelperand the standard clientClient. First, call itstoBucketNamemethod to convert the name prefix to the full name, and then access the Bucket Space through the standard client. In this case, thebucketparameter in the request must be set to the full name. When you useBucketSpaceHelper(accountId:region:), you must also provideaccountIdandregion. -
The current version of the OSS SDK for Swift does not support CNAME-related operations (such as binding or unbinding a wildcard CNAME for an Agentic Bucket). If you have such requirements, use an SDK for another programming language or call the API directly.
Sample code
Create an Agentic Bucket
The following code creates an Agentic Bucket.
import AlibabaCloudOSS
import AlibabaCloudOSSAgentic
import Foundation
import ArgumentParser
@main
struct CreateAgenticBucket: AsyncParsableCommand {
func run() async throws {
do {
// Set the OSS region
let region = "cn-hangzhou"
// Set the Alibaba Cloud account ID
let accountId = "1234567890123456"
// Set the name prefix of the Agentic Bucket to create
let bucket = "my-agentic"
// Create a configuration object:
// - Use the default configuration
// - Set the credentials provider to the environment variable loading mode (EnvironmentCredentialsProvider)
// - Set the region to the specified region
// - Set the account ID to the specified accountId
let config = Configuration.default()
.withRegion(region)
.withCredentialsProvider(EnvironmentCredentialsProvider())
.withAccountId(accountId)
// Initialize the AgenticBucketClient with the preceding configuration
let client = AgenticBucketClient(config)
// Call the createAgenticBucket operation to create an Agentic Bucket,
// with the storage class set to Standard and the data redundancy type set to LRS
let result = try await client.createAgenticBucket(
CreateAgenticBucketRequest(
bucket: bucket, // The Agentic Bucket name prefix
createAgenticBucketConfiguration: CreateAgenticBucketConfiguration(
storageClass: "Standard",
dataRedundancyType: "LRS"
)
)
)
// Print the request result
print("CreateAgenticBucket done, StatusCode:\(result.statusCode), RequestId:\(result.requestId).")
} catch {
CreateAgenticBucket.exit(withError: error)
}
}
}
Query Agentic Bucket information
The following code queries the information about a specified Agentic Bucket.
import AlibabaCloudOSS
import AlibabaCloudOSSAgentic
import Foundation
import ArgumentParser
@main
struct GetAgenticBucket: AsyncParsableCommand {
func run() async throws {
do {
// Set the OSS region
let region = "cn-hangzhou"
// Set the Alibaba Cloud account ID
let accountId = "1234567890123456"
// Set the name prefix of the Agentic Bucket to query
let bucket = "my-agentic"
// Create a configuration object and set the region and account ID
let config = Configuration.default()
.withRegion(region)
.withCredentialsProvider(EnvironmentCredentialsProvider())
.withAccountId(accountId)
// Initialize the AgenticBucketClient with the preceding configuration
let client = AgenticBucketClient(config)
// Call the getAgenticBucket operation to query the information about a specified Agentic Bucket
let result = try await client.getAgenticBucket(
GetAgenticBucketRequest(
bucket: bucket // The Agentic Bucket name prefix
)
)
// Print the request result
print("GetAgenticBucket done, StatusCode:\(result.statusCode), RequestId:\(result.requestId).")
// Print the details of the Agentic Bucket
if let info = result.agenticBucketInfo {
print("Name: \(info.name ?? "")")
print("Region: \(info.region ?? "")")
print("StorageClass: \(info.storageClass ?? "")")
print("DataRedundancyType: \(info.dataRedundancyType ?? "")")
print("Status: \(info.status ?? "")")
print("CreateTime: \(info.createTime ?? "")")
}
} catch {
GetAgenticBucket.exit(withError: error)
}
}
}
List Agentic Buckets
The following code lists all Agentic Buckets of the current account in a specified region.
import AlibabaCloudOSS
import AlibabaCloudOSSAgentic
import Foundation
import ArgumentParser
@main
struct ListAgenticBuckets: AsyncParsableCommand {
func run() async throws {
do {
// Set the OSS region
let region = "cn-hangzhou"
// Set the Alibaba Cloud account ID
let accountId = "1234567890123456"
// Create a configuration object and set the region and account ID
let config = Configuration.default()
.withRegion(region)
.withCredentialsProvider(EnvironmentCredentialsProvider())
.withAccountId(accountId)
// Initialize the AgenticBucketClient with the preceding configuration
let client = AgenticBucketClient(config)
// Create a Paginator to list Agentic Buckets by page, which handles pagination automatically
let paginator = client.listAgenticBucketsPaginator(ListAgenticBucketsRequest())
// Iterate through all pages of Agentic Buckets
for try await page in paginator {
// Iterate through and print the information about each Agentic Bucket
for bucket in page.agenticBuckets ?? [] {
print("AgenticBucket: \(bucket.name ?? "") \(bucket.storageClass ?? "") \(bucket.dataRedundancyType ?? "")")
}
}
} catch {
ListAgenticBuckets.exit(withError: error)
}
}
}
Delete an Agentic Bucket
The following code deletes a specified Agentic Bucket.
import AlibabaCloudOSS
import AlibabaCloudOSSAgentic
import Foundation
import ArgumentParser
@main
struct DeleteAgenticBucket: AsyncParsableCommand {
func run() async throws {
do {
// Set the OSS region
let region = "cn-hangzhou"
// Set the Alibaba Cloud account ID
let accountId = "1234567890123456"
// Set the name prefix of the Agentic Bucket to delete
let bucket = "my-agentic"
// Create a configuration object and set the region and account ID
let config = Configuration.default()
.withRegion(region)
.withCredentialsProvider(EnvironmentCredentialsProvider())
.withAccountId(accountId)
// Initialize the AgenticBucketClient with the preceding configuration
let client = AgenticBucketClient(config)
// Call the deleteAgenticBucket operation to delete a specified Agentic Bucket
let result = try await client.deleteAgenticBucket(
DeleteAgenticBucketRequest(
bucket: bucket // The Agentic Bucket name prefix
)
)
// Print the request result
print("DeleteAgenticBucket done, StatusCode:\(result.statusCode), RequestId:\(result.requestId).")
} catch {
DeleteAgenticBucket.exit(withError: error)
}
}
}
Create a Bucket Space
The following code creates a Bucket Space under a specified Agentic Bucket. The SDK provides two ways to access a Bucket Space. The following samples demonstrate each way, and you can choose one as needed.
Method 1: Use BucketSpaceClient. In the request, the bucket parameter is set to the Bucket Space name prefix.
import AlibabaCloudOSS
import AlibabaCloudOSSAgentic
import Foundation
import ArgumentParser
@main
struct BucketSpacePutBucket: AsyncParsableCommand {
func run() async throws {
do {
// Set the OSS region
let region = "cn-hangzhou"
// Set the Alibaba Cloud account ID
let accountId = "1234567890123456"
// Set the name prefix of the parent Agentic Bucket
let agenticBucketPrefix = "my-agentic"
// Set the name prefix of the Bucket Space to create
let bucketSpacePrefix = "my-space"
// Create a configuration object and set the region and account ID
let config = Configuration.default()
.withRegion(region)
.withCredentialsProvider(EnvironmentCredentialsProvider())
.withAccountId(accountId)
// Create a Bucket Space client. This client reuses standard OSS bucket and object operations
// and automatically resolves the Bucket Space name prefix to the full name:
// {prefix}-{accountId}-{region}-bs-apsr
let client = BucketSpaceClient.make(config)
// Call the putBucket operation to create a Bucket Space under a specified Agentic Bucket.
// The bucket parameter is the Bucket Space name prefix,
// and the agenticBucket parameter is the full name of the parent Agentic Bucket
let result = try await client.putBucket(
PutBucketRequest(
bucket: bucketSpacePrefix,
agenticBucket: "\(agenticBucketPrefix)-\(accountId)-\(region)-ab-apsr"
)
)
// Print the request result
print("PutBucket done, StatusCode:\(result.statusCode), RequestId:\(result.requestId).")
} catch {
BucketSpacePutBucket.exit(withError: error)
}
}
}
Method 2: Use BucketSpaceHelper and the standard client Client. In the request, the bucket parameter is set to the converted full name of the Bucket Space.
import AlibabaCloudOSS
import AlibabaCloudOSSAgentic
import Foundation
import ArgumentParser
@main
struct BucketSpacePutBucketWithHelper: AsyncParsableCommand {
func run() async throws {
do {
// Set the OSS region
let region = "cn-hangzhou"
// Set the Alibaba Cloud account ID
let accountId = "1234567890123456"
// Set the name prefix of the parent Agentic Bucket
let agenticBucketPrefix = "my-agentic"
// Set the name prefix of the Bucket Space to create
let bucketSpacePrefix = "my-space"
// Create a configuration object and set the region
let config = Configuration.default()
.withRegion(region)
.withCredentialsProvider(EnvironmentCredentialsProvider())
// Initialize the OSS client with the standard client
let client = Client(config)
// Create a BucketSpaceHelper. Based on the account ID and region, the helper
// converts the Bucket Space name prefix to the full name:
// {prefix}-{accountId}-{region}-bs-apsr
let helper = BucketSpaceHelper(accountId: accountId, region: region)
// Call the putBucket operation to create a Bucket Space under a specified Agentic Bucket.
// The bucket parameter is the full Bucket Space name converted by the helper,
// and the agenticBucket parameter is the full name of the parent Agentic Bucket
let result = try await client.putBucket(
PutBucketRequest(
bucket: helper.toBucketName(bucketSpacePrefix),
agenticBucket: "\(agenticBucketPrefix)-\(accountId)-\(region)-ab-apsr"
)
)
// Print the request result
print("PutBucket done, StatusCode:\(result.statusCode), RequestId:\(result.requestId).")
} catch {
BucketSpacePutBucketWithHelper.exit(withError: error)
}
}
}
Upload an object to a Bucket Space
The following code uploads an object to a specified Bucket Space. The following samples demonstrate the two access methods.
Method 1: Use BucketSpaceClient. In the request, the bucket parameter is set to the Bucket Space name prefix.
import AlibabaCloudOSS
import AlibabaCloudOSSAgentic
import Foundation
import ArgumentParser
@main
struct BucketSpacePutObject: AsyncParsableCommand {
func run() async throws {
do {
// Set the OSS region
let region = "cn-hangzhou"
// Set the Alibaba Cloud account ID
let accountId = "1234567890123456"
// Set the Bucket Space name prefix
let bucketSpacePrefix = "my-space"
// Set the name of the file (object key) to upload
let key = "exampleobject"
// Create a configuration object and set the region and account ID
let config = Configuration.default()
.withRegion(region)
.withCredentialsProvider(EnvironmentCredentialsProvider())
.withAccountId(accountId)
// Create a Bucket Space client, which automatically resolves the Bucket Space name prefix
// to the full name
let client = BucketSpaceClient.make(config)
// Call the putObject operation to upload an object to a specified Bucket Space
let result = try await client.putObject(
PutObjectRequest(
bucket: bucketSpacePrefix, // The Bucket Space name prefix
key: key, // The object key (file name)
body: .data("Hello OSS".data(using: .utf8)!) // The content data to upload
)
)
// Print the request result
print("PutObject done, StatusCode:\(result.statusCode), RequestId:\(result.requestId), ETag:\(result.etag ?? "").")
} catch {
BucketSpacePutObject.exit(withError: error)
}
}
}
Method 2: Use BucketSpaceHelper and the standard client Client. In the request, the bucket parameter is set to the converted full name of the Bucket Space.
import AlibabaCloudOSS
import AlibabaCloudOSSAgentic
import Foundation
import ArgumentParser
@main
struct BucketSpacePutObjectWithHelper: AsyncParsableCommand {
func run() async throws {
do {
// Set the OSS region
let region = "cn-hangzhou"
// Set the Alibaba Cloud account ID
let accountId = "1234567890123456"
// Set the Bucket Space name prefix
let bucketSpacePrefix = "my-space"
// Set the name of the file (object key) to upload
let key = "exampleobject"
// Create a configuration object and set the region
let config = Configuration.default()
.withRegion(region)
.withCredentialsProvider(EnvironmentCredentialsProvider())
// Initialize the OSS client with the standard client
let client = Client(config)
// Create a BucketSpaceHelper to convert the Bucket Space name prefix
// to the full name: {prefix}-{accountId}-{region}-bs-apsr
let helper = BucketSpaceHelper(accountId: accountId, region: region)
// Call the putObject operation to upload an object to a specified Bucket Space.
// The bucket parameter is the full Bucket Space name converted by the helper
let result = try await client.putObject(
PutObjectRequest(
bucket: helper.toBucketName(bucketSpacePrefix), // The full name of the Bucket Space
key: key, // The object key (file name)
body: .data("Hello OSS".data(using: .utf8)!) // The content data to upload
)
)
// Print the request result
print("PutObject done, StatusCode:\(result.statusCode), RequestId:\(result.requestId), ETag:\(result.etag ?? "").")
} catch {
BucketSpacePutObjectWithHelper.exit(withError: error)
}
}
}
References
-
For the complete sample code for creating an Agentic Bucket, see CreateAgenticBucket.
-
For the complete sample code for querying Agentic Bucket information, see GetAgenticBucket.
-
For the complete sample code for listing Agentic Buckets, see ListAgenticBuckets.
-
For the complete sample code for deleting an Agentic Bucket, see DeleteAgenticBucket.
-
For the complete sample code for creating a Bucket Space, see CreateBucketSpace.
-
For the complete sample code for uploading an object to a Bucket Space, see BucketSpace.
-
To bind or unbind a wildcard CNAME for an Agentic Bucket, see the following topics for the parameter details of the related operations: CreateCnameToken, PutCname, ListCname, GetCnameToken and DeleteCname.