Agentic Bucket (OSS SDK for Go 2.0)
This topic describes how to use the OSS SDK for Go 2.0 to create and manage Agentic Buckets, bind and unbind wildcard CNAMEs, and create Bucket Spaces and upload objects.
Usage notes
To use Agentic Bucket features, you must install alibabacloud-oss-go-sdk-v2 V1.6.0 or later.
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
oss/agenticpackage to create an Agentic Bucket client (agentic.NewAgenticBucketClient) or a Bucket Space client (agentic.NewBucketSpaceClient), you must useWithRegionandWithAccountIdto set the region and the account ID in the configuration. 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 the Bucket Space client (
agentic.NewBucketSpaceClient). In the request, theBucketparameter is set to the Bucket Space name prefix, and the SDK automatically resolves it to the full name. Or, useagentic.NewBucketSpaceHelperto create a BucketSpaceHelper, and call itsToBucketNamemethod to convert the name prefix to the full name, and then use the standard clientoss.NewClientto access the Bucket Space. In this case, theBucketparameter in the request must be set to the full name. When you use BucketSpaceHelper, you must also set the region and the account ID in the configuration.CNAME-related operations (creating a CnameToken, getting a CnameToken, binding a CNAME, listing CNAMEs, and deleting a CNAME) are provided by the standard client
oss.NewClient(cfg). In the request, theBucketparameter must be set to the full name of the Agentic Bucket, which is{prefix}-{accountId}-{region}-ab-apsr.In wildcard CNAME requests, the
DomainorCnameparameter must be set to the base domain name (without the*.prefix). The base domain name must contain at least three segments (for example,example.comis invalid, andfoo.example.comis valid). Before you bind a wildcard CNAME, you must apply for a CnameToken for the base domain name and complete domain ownership verification. If you bind a certificate, the certificate must be a wildcard certificate that matches*.base-domain. For more information about the complete rules for wildcard CNAMEs, see PutCname.
Sample code
Create an Agentic Bucket
The following code creates an Agentic Bucket.
package main
import (
"context"
"flag"
"log"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/agentic"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)
// Define command-line arguments, including the required region, Agentic Bucket name prefix, and account ID, and the optional endpoint
var (
region string
bucket string
endpoint string
accountId string
)
func init() {
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
flag.StringVar(&bucket, "bucket", "", "The name of the agentic bucket.")
flag.StringVar(&endpoint, "endpoint", "", "The domain names that other services can use to access OSS.")
flag.StringVar(&accountId, "account-id", "", "The account id.")
}
func main() {
// Parse the command-line arguments and validate the required parameters
flag.Parse()
if len(bucket) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
if len(accountId) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, account id required")
}
// Create a configuration object with the SDK default configuration, and set the credentials provider, region, and account ID.
// The client automatically resolves the bucket name prefix to the full name based on the region and account ID:
// {prefix}-{accountId}-{region}-ab-apsr
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region).
WithAccountId(accountId)
// If a custom endpoint is provided, update the endpoint of the configuration object
if len(endpoint) > 0 {
cfg.WithEndpoint(endpoint)
}
// Initialize the Agentic Bucket client with the preceding configuration
client := agentic.NewAgenticBucketClient(cfg)
// Create an Agentic Bucket with the storage class set to Standard and the data redundancy type set to LRS.
// The Bucket parameter is the Agentic Bucket name prefix
request := &agentic.CreateAgenticBucketRequest{
Bucket: oss.Ptr(bucket),
CreateAgenticBucketConfiguration: &agentic.CreateAgenticBucketConfiguration{
StorageClass: oss.StorageClassStandard,
DataRedundancyType: oss.DataRedundancyLRS,
},
}
result, err := client.CreateAgenticBucket(context.TODO(), request)
if err != nil {
log.Fatalf("failed to create agentic bucket %v", err)
}
log.Printf("create agentic bucket result:%#v\n", result)
}Query Agentic Bucket information
The following code queries the information about a specified Agentic Bucket.
package main
import (
"context"
"flag"
"log"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/agentic"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)
// Define command-line arguments, including the required region, Agentic Bucket name prefix, and account ID, and the optional endpoint
var (
region string
bucket string
endpoint string
accountId string
)
func init() {
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
flag.StringVar(&bucket, "bucket", "", "The name of the agentic bucket.")
flag.StringVar(&endpoint, "endpoint", "", "The domain names that other services can use to access OSS.")
flag.StringVar(&accountId, "account-id", "", "The account id.")
}
func main() {
// Parse the command-line arguments and validate the required parameters
flag.Parse()
if len(bucket) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
if len(accountId) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, account id required")
}
// Create a configuration object with the SDK default configuration, and set the credentials provider, region, and account ID
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region).
WithAccountId(accountId)
// If a custom endpoint is provided, update the endpoint of the configuration object
if len(endpoint) > 0 {
cfg.WithEndpoint(endpoint)
}
// Initialize the Agentic Bucket client with the preceding configuration
client := agentic.NewAgenticBucketClient(cfg)
// Query the information about a specified Agentic Bucket. The Bucket parameter is the name prefix
request := &agentic.GetAgenticBucketRequest{
Bucket: oss.Ptr(bucket),
}
result, err := client.GetAgenticBucket(context.TODO(), request)
if err != nil {
log.Fatalf("failed to get agentic bucket %v", err)
}
// Get and print the details of the Agentic Bucket
if info := result.AgenticBucketInfo; info != nil {
log.Printf("agentic bucket info: name:%v, owner:%v, region:%v, storage class:%v, status:%v, acl:%v, versioning:%v, create time:%v\n",
oss.ToString(info.Name), oss.ToString(info.Owner), oss.ToString(info.Region),
oss.ToString(info.StorageClass), oss.ToString(info.Status), oss.ToString(info.ACL),
oss.ToString(info.Versioning), oss.ToString(info.CreateTime))
}
}List Agentic Buckets
The following code lists all Agentic Buckets of the current account in a specified region.
package main
import (
"context"
"flag"
"log"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/agentic"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)
// Define command-line arguments, including the required region and account ID, and the optional endpoint
var (
region string
endpoint string
accountId string
)
func init() {
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
flag.StringVar(&endpoint, "endpoint", "", "The domain names that other services can use to access OSS.")
flag.StringVar(&accountId, "account-id", "", "The account id.")
}
func main() {
// Parse the command-line arguments and validate the required parameters
flag.Parse()
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
if len(accountId) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, account id required")
}
// Create a configuration object with the SDK default configuration, and set the credentials provider, region, and account ID
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region).
WithAccountId(accountId)
// If a custom endpoint is provided, update the endpoint of the configuration object
if len(endpoint) > 0 {
cfg.WithEndpoint(endpoint)
}
// Initialize the Agentic Bucket client with the preceding configuration
client := agentic.NewAgenticBucketClient(cfg)
// Create a ListAgenticBuckets paginator
request := &agentic.ListAgenticBucketsRequest{}
p := client.NewListAgenticBucketsPaginator(request)
// Iterate page by page through all Agentic Buckets of the current account in the specified region
var i int
log.Println("Agentic Buckets:")
for p.HasNext() {
i++
page, err := p.NextPage(context.TODO())
if err != nil {
log.Fatalf("failed to get page %v, %v", i, err)
}
for _, b := range page.AgenticBuckets {
// Print the full name, storage class, data redundancy type, and creation time of each Agentic Bucket
log.Printf("Agentic Bucket: name:%v, storage class:%v, data redundancy type:%v, create time:%v\n",
oss.ToString(b.Name), oss.ToString(b.StorageClass), oss.ToString(b.DataRedundancyType), oss.ToString(b.CreateTime))
}
}
}Delete an Agentic Bucket
The following code deletes a specified Agentic Bucket.
package main
import (
"context"
"flag"
"log"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/agentic"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)
// Define command-line arguments, including the required region, Agentic Bucket name prefix, and account ID, and the optional endpoint
var (
region string
bucket string
endpoint string
accountId string
)
func init() {
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
flag.StringVar(&bucket, "bucket", "", "The name of the agentic bucket.")
flag.StringVar(&endpoint, "endpoint", "", "The domain names that other services can use to access OSS.")
flag.StringVar(&accountId, "account-id", "", "The account id.")
}
func main() {
// Parse the command-line arguments and validate the required parameters
flag.Parse()
if len(bucket) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
if len(accountId) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, account id required")
}
// Create a configuration object with the SDK default configuration, and set the credentials provider, region, and account ID
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region).
WithAccountId(accountId)
// If a custom endpoint is provided, update the endpoint of the configuration object
if len(endpoint) > 0 {
cfg.WithEndpoint(endpoint)
}
// Initialize the Agentic Bucket client with the preceding configuration
client := agentic.NewAgenticBucketClient(cfg)
// Delete a specified Agentic Bucket. The Bucket parameter is the name prefix
request := &agentic.DeleteAgenticBucketRequest{
Bucket: oss.Ptr(bucket),
}
result, err := client.DeleteAgenticBucket(context.TODO(), request)
if err != nil {
log.Fatalf("failed to delete agentic bucket %v", err)
}
log.Printf("delete agentic bucket result:%#v\n", result)
}Create a CnameToken (wildcard domain name)
The following code applies for a CnameToken for the base domain name of a wildcard CNAME on an Agentic Bucket.
package main
import (
"context"
"flag"
"fmt"
"log"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)
var (
region string
bucket string
endpoint string
accountId string
domain string
)
func init() {
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
flag.StringVar(&bucket, "bucket", "", "The name of the agentic bucket.")
flag.StringVar(&endpoint, "endpoint", "", "The domain names that other services can use to access OSS.")
flag.StringVar(&accountId, "account-id", "", "The account id.")
flag.StringVar(&domain, "domain", "", "The base domain name of the wildcard domain, without the \"*.\" prefix.")
}
func main() {
flag.Parse()
if len(bucket) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
if len(accountId) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, account id required")
}
if len(domain) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, domain name required")
}
// Create a configuration object with the SDK default configuration and load access credentials from environment variables
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region).
WithAccountId(accountId)
if len(endpoint) > 0 {
cfg.WithEndpoint(endpoint)
}
// CNAME-related operations are provided by the standard client, and the Bucket in the request must be set to the full name of the Agentic Bucket
client := oss.NewClient(cfg)
// The full name of the Agentic Bucket is {bucket}-{accountId}-{region}-ab-apsr
agenticBucket := fmt.Sprintf("%s-%s-%s-ab-apsr", bucket, accountId, region)
// Apply for a CnameToken for the base domain name. Set Domain to the base domain name without the *. prefix,
// Set IsWildCard to true to apply for a CnameToken for a wildcard CNAME
request := &oss.CreateCnameTokenRequest{
Bucket: oss.Ptr(agenticBucket),
BucketCnameConfiguration: &oss.BucketCnameConfiguration{
Cname: &oss.Cname{
Domain: oss.Ptr(domain),
IsWildCard: oss.Ptr(true),
},
},
}
result, err := client.CreateCnameToken(context.TODO(), request)
if err != nil {
log.Fatalf("failed to create cname token %v", err)
}
log.Printf("create cname token result:%#v\n", result)
}Get a CnameToken (wildcard domain name)
The following code gets the CnameToken of a wildcard CNAME on an Agentic Bucket.
package main
import (
"context"
"flag"
"fmt"
"log"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)
var (
region string
bucket string
endpoint string
accountId string
domain string
)
func init() {
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
flag.StringVar(&bucket, "bucket", "", "The name of the agentic bucket.")
flag.StringVar(&endpoint, "endpoint", "", "The domain names that other services can use to access OSS.")
flag.StringVar(&accountId, "account-id", "", "The account id.")
flag.StringVar(&domain, "domain", "", "The base domain name of the wildcard domain, without the \"*.\" prefix.")
}
func main() {
flag.Parse()
if len(bucket) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
if len(accountId) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, account id required")
}
if len(domain) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, domain name required")
}
// Create a configuration object with the SDK default configuration and load access credentials from environment variables
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region).
WithAccountId(accountId)
if len(endpoint) > 0 {
cfg.WithEndpoint(endpoint)
}
// CNAME-related operations are provided by the standard client, and the Bucket in the request must be set to the full name of the Agentic Bucket
client := oss.NewClient(cfg)
// The full name of the Agentic Bucket is {bucket}-{accountId}-{region}-ab-apsr
Agentic Bucket := fmt.Sprintf("%s-%s-%s-ab-apsr", bucket, accountId, region)
// Get the CnameToken of the base domain name. Set Cname to the base domain name without the *. prefix,
// Set Wildcard to true to get the CnameToken of a wildcard CNAME
request := &oss.GetCnameTokenRequest{
Bucket: oss.Ptr(agenticBucket),
Cname: oss.Ptr(domain),
Wildcard: oss.Ptr(true),
}
result, err := client.GetCnameToken(context.TODO(), request)
if err != nil {
log.Fatalf("failed to get cname token %v", err)
}
log.Printf("get cname token result:%#v\n", result)
}Bind a wildcard CNAME
The following code binds a wildcard CNAME to an Agentic Bucket.
package main
import (
"context"
"flag"
"fmt"
"log"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)
var (
region string
bucket string
endpoint string
accountId string
domain string
)
func init() {
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
flag.StringVar(&bucket, "bucket", "", "The name of the agentic bucket.")
flag.StringVar(&endpoint, "endpoint", "", "The domain names that other services can use to access OSS.")
flag.StringVar(&accountId, "account-id", "", "The account id.")
flag.StringVar(&domain, "domain", "", "The base domain name of the wildcard domain, without the \"*.\" prefix.")
}
func main() {
flag.Parse()
if len(bucket) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
if len(accountId) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, account id required")
}
if len(domain) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, domain name required")
}
// Create a configuration object with the SDK default configuration and load access credentials from environment variables
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region).
WithAccountId(accountId)
if len(endpoint) > 0 {
cfg.WithEndpoint(endpoint)
}
// CNAME-related operations are provided by the standard client, and the Bucket in the request must be set to the full name of the Agentic Bucket
client := oss.NewClient(cfg)
// The full name of the Agentic Bucket is {bucket}-{accountId}-{region}-ab-apsr
agenticBucket := fmt.Sprintf("%s-%s-%s-ab-apsr", bucket, accountId, region)
// The SSL certificate parameters. A wildcard CNAME must use a wildcard certificate that matches *.base-domain
certId := "493****-cn-hangzhou"
certificate := `-----BEGIN CERTIFICATE-----
MIIDhDCCAmwCCQCFs8ixARsyrDANBgkqhkiG9w0BAQsFADCBgzELMAkGA1UEBhMC
**** -----END CERTIFICATE-----`
privateKey := `-----BEGIN CERTIFICATE-----
MIIDhDCCAmwCCQCFs8ixARsyrDANBgkqhkiG9w0BAQsFADCBgzELMAkGA1UEBhMC
**** -----END CERTIFICATE-----`
// Bind a wildcard CNAME to an Agentic Bucket. Set Domain to the base domain name without the *. prefix,
// Set IsWildCard to true to bind a wildcard CNAME
request := &oss.PutCnameRequest{
Bucket: oss.Ptr(agenticBucket),
BucketCnameConfiguration: &oss.BucketCnameConfiguration{
Cname: &oss.Cname{
Domain: oss.Ptr(domain),
IsWildCard: oss.Ptr(true),
CertificateConfiguration: &oss.CertificateConfiguration{
Certificate: oss.Ptr(certificate),
PrivateKey: oss.Ptr(privateKey),
CertId: oss.Ptr(certId),
},
},
},
}
result, err := client.PutCname(context.TODO(), request)
if err != nil {
log.Fatalf("failed to put cname %v", err)
}
log.Printf("put cname result:%#v\n", result)
}List CNAME records
The following code lists the CNAME records of an Agentic Bucket.
package main
import (
"context"
"flag"
"fmt"
"log"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)
var (
region string
bucket string
endpoint string
accountId string
)
func init() {
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
flag.StringVar(&bucket, "bucket", "", "The name of the agentic bucket.")
flag.StringVar(&endpoint, "endpoint", "", "The domain names that other services can use to access OSS.")
flag.StringVar(&accountId, "account-id", "", "The account id.")
}
func main() {
flag.Parse()
if len(bucket) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
if len(accountId) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, account id required")
}
// Create a configuration object with the SDK default configuration and load access credentials from environment variables
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region).
WithAccountId(accountId)
if len(endpoint) > 0 {
cfg.WithEndpoint(endpoint)
}
// CNAME-related operations are provided by the standard client, and the Bucket in the request must be set to the full name of the Agentic Bucket
client := oss.NewClient(cfg)
// The full name of the Agentic Bucket is {bucket}-{accountId}-{region}-ab-apsr
agenticBucket := fmt.Sprintf("%s-%s-%s-ab-apsr", bucket, accountId, region)
// List the CNAME records of a specified Agentic Bucket
request := &oss.ListCnameRequest{
Bucket: oss.Ptr(agenticBucket),
}
result, err := client.ListCname(context.TODO(), request)
if err != nil {
log.Fatalf("failed to list cname %v", err)
}
// Iterate through and print the details of each CNAME record
for _, r := range result.Cnames {
// A value of true for IsWildCard indicates that the record is a wildcard CNAME
log.Printf("cname: domain:%v, last modified:%v, is wild card:%v, status:%v\n",
oss.ToString(r.Domain), oss.ToString(r.LastModified), oss.ToBool(r.IsWildCard), oss.ToString(r.Status))
}
}Delete a wildcard CNAME
The following code deletes a wildcard CNAME from an Agentic Bucket.
package main
import (
"context"
"flag"
"fmt"
"log"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)
var (
region string
bucket string
endpoint string
accountId string
domain string
)
func init() {
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
flag.StringVar(&bucket, "bucket", "", "The name of the agentic bucket.")
flag.StringVar(&endpoint, "endpoint", "", "The domain names that other services can use to access OSS.")
flag.StringVar(&accountId, "account-id", "", "The account id.")
flag.StringVar(&domain, "domain", "", "The base domain name of the wildcard domain, without the \"*.\" prefix.")
}
func main() {
flag.Parse()
if len(bucket) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
if len(accountId) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, account id required")
}
if len(domain) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, domain name required")
}
// Create a configuration object with the SDK default configuration and load access credentials from environment variables
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region).
WithAccountId(accountId)
if len(endpoint) > 0 {
cfg.WithEndpoint(endpoint)
}
// CNAME-related operations are provided by the standard client, and the Bucket in the request must be set to the full name of the Agentic Bucket
client := oss.NewClient(cfg)
// The full name of the Agentic Bucket is {bucket}-{accountId}-{region}-ab-apsr
agenticBucket := fmt.Sprintf("%s-%s-%s-ab-apsr", bucket, accountId, region)
// Delete a wildcard CNAME from an Agentic Bucket. Set Domain to the base domain name without the *. prefix,
// Set IsWildCard to true to delete a wildcard CNAME
request := &oss.DeleteCnameRequest{
Bucket: oss.Ptr(agenticBucket),
BucketCnameConfiguration: &oss.BucketCnameConfiguration{
Cname: &oss.Cname{
Domain: oss.Ptr(domain),
IsWildCard: oss.Ptr(true),
},
},
}
result, err := client.DeleteCname(context.TODO(), request)
if err != nil {
log.Fatalf("failed to delete cname %v", err)
}
log.Printf("delete cname result:%#v\n", result)
}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 agentic.NewBucketSpaceClient to create a Bucket Space client. In the request, the Bucket parameter is set to the Bucket Space name prefix.
package main
import (
"context"
"flag"
"fmt"
"log"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/agentic"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)
var (
region string
bucket string
endpoint string
accountId string
agenticBucket string
)
func init() {
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
flag.StringVar(&bucket, "bucket", "", "The bucket space prefix name.")
flag.StringVar(&endpoint, "endpoint", "", "The domain names that other services can use to access OSS.")
flag.StringVar(&accountId, "account-id", "", "The account id.")
flag.StringVar(&agenticBucket, "agentic-bucket", "", "The agentic bucket that the bucket space belongs to.")
}
func main() {
flag.Parse()
if len(bucket) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
if len(accountId) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, account id required")
}
if len(agenticBucket) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, agentic bucket name required")
}
// Create a configuration object with the SDK default configuration and load access credentials from environment variables
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region).
WithAccountId(accountId)
if len(endpoint) > 0 {
cfg.WithEndpoint(endpoint)
}
// 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 {bucket}-{accountId}-{region}-bs-apsr
client := agentic.NewBucketSpaceClient(cfg)
// Create a Bucket Space under a specified Agentic Bucket. The Bucket parameter of PutBucketRequest
// is the Bucket Space name prefix, and the Agentic Bucket parameter is the full name of the parent Agentic Bucket
request := &oss.PutBucketRequest{
Bucket: oss.Ptr(bucket),
AgenticBucket: oss.Ptr(fmt.Sprintf("%s-%s-%s-ab-apsr", agenticBucket, accountId, region)),
}
result, err := client.PutBucket(context.TODO(), request)
if err != nil {
log.Fatalf("failed to put bucket space %v", err)
}
log.Printf("put bucket space result:%#v\n", result)
}Method 2: Use agentic.NewBucketSpaceHelper and the standard client oss.NewClient. In the request, the Bucket parameter is set to the converted full name of the Bucket Space.
package main
import (
"context"
"flag"
"fmt"
"log"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/agentic"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)
var (
region string
bucket string
endpoint string
accountId string
agenticBucket string
)
func init() {
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
flag.StringVar(&bucket, "bucket", "", "The bucket space prefix name.")
flag.StringVar(&endpoint, "endpoint", "", "The domain names that other services can use to access OSS.")
flag.StringVar(&accountId, "account-id", "", "The account id.")
flag.StringVar(&agenticBucket, "agentic-bucket", "", "The agentic bucket that the bucket space belongs to.")
}
func main() {
flag.Parse()
if len(bucket) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
if len(accountId) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, account id required")
}
if len(agenticBucket) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, agentic bucket name required")
}
// Create a configuration object with the SDK default configuration and load access credentials from environment variables
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region).
WithAccountId(accountId)
if len(endpoint) > 0 {
cfg.WithEndpoint(endpoint)
}
// Create a standard OSS client
client := oss.NewClient(cfg)
// Create a BucketSpaceHelper. Based on the account ID and region in the configuration, the helper
// Convert the Bucket Space name prefix to the full name {bucket}-{accountId}-{region}-bs-apsr
helper := agentic.NewBucketSpaceHelper(cfg)
// Create a Bucket Space under a specified Agentic Bucket. The Bucket parameter of PutBucketRequest
// is the full Bucket Space name converted by helper.ToBucketName, and the Agentic Bucket parameter is the full name of the parent Agentic Bucket
request := &oss.PutBucketRequest{
Bucket: oss.Ptr(helper.ToBucketName(bucket)),
AgenticBucket: oss.Ptr(fmt.Sprintf("%s-%s-%s-ab-apsr", agenticBucket, accountId, region)),
}
result, err := client.PutBucket(context.TODO(), request)
if err != nil {
log.Fatalf("failed to put bucket space %v", err)
}
log.Printf("put bucket space result:%#v\n", result)
}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 agentic.NewBucketSpaceClient to create a Bucket Space client. In the request, the Bucket parameter is set to the Bucket Space name prefix.
package main
import (
"context"
"flag"
"log"
"strings"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/agentic"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)
var (
region string
bucket string
endpoint string
accountId string
object string
)
func init() {
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
flag.StringVar(&bucket, "bucket", "", "The name of the bucket space.")
flag.StringVar(&endpoint, "endpoint", "", "The domain names that other services can use to access OSS.")
flag.StringVar(&accountId, "account-id", "", "The account id.")
flag.StringVar(&object, "object", "", "The name of the object.")
}
func main() {
flag.Parse()
if len(bucket) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
if len(accountId) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, account id required")
}
if len(object) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, object name required")
}
// Create a configuration object with the SDK default configuration and load access credentials from environment variables
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region).
WithAccountId(accountId)
if len(endpoint) > 0 {
cfg.WithEndpoint(endpoint)
}
// 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 {bucket}-{accountId}-{region}-bs-apsr
client := agentic.NewBucketSpaceClient(cfg)
// Upload an object to a specified Bucket Space
request := &oss.PutObjectRequest{
Bucket: oss.Ptr(bucket),
Key: oss.Ptr(object),
Body: strings.NewReader("hello world"),
}
result, err := client.PutObject(context.TODO(), request)
if err != nil {
log.Fatalf("failed to put object %v", err)
}
log.Printf("put object result:%#v\n", result)
}Method 2: Use agentic.NewBucketSpaceHelper and the standard client oss.NewClient. In the request, the Bucket parameter is set to the converted full name of the Bucket Space.
package main
import (
"context"
"flag"
"log"
"strings"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/agentic"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)
var (
region string
bucket string
endpoint string
accountId string
object string
)
func init() {
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
flag.StringVar(&bucket, "bucket", "", "The name of the bucket space.")
flag.StringVar(&endpoint, "endpoint", "", "The domain names that other services can use to access OSS.")
flag.StringVar(&accountId, "account-id", "", "The account id.")
flag.StringVar(&object, "object", "", "The name of the object.")
}
func main() {
flag.Parse()
if len(bucket) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
if len(accountId) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, account id required")
}
if len(object) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, object name required")
}
// Create a configuration object with the SDK default configuration and load access credentials from environment variables
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region).
WithAccountId(accountId)
if len(endpoint) > 0 {
cfg.WithEndpoint(endpoint)
}
// Create a standard OSS client
client := oss.NewClient(cfg)
// Create a BucketSpaceHelper to convert the Bucket Space name prefix to the full name
// {bucket}-{accountId}-{region}-bs-apsr
helper := agentic.NewBucketSpaceHelper(cfg)
// Upload an object to a specified Bucket Space. The Bucket parameter of PutObjectRequest
// is the full Bucket Space name converted by helper.ToBucketName
request := &oss.PutObjectRequest{
Bucket: oss.Ptr(helper.ToBucketName(bucket)),
Key: oss.Ptr(object),
Body: strings.NewReader("hello world"),
}
result, err := client.PutObject(context.TODO(), request)
if err != nil {
log.Fatalf("failed to put object %v", err)
}
log.Printf("put object result:%#v\n", result)
}References
For the complete sample code for creating an Agentic Bucket, see create_agentic_bucket.go.
For the complete sample code for querying Agentic Bucket information, see get_agentic_bucket.go.
For the complete sample code for listing Agentic Buckets, see list_agentic_buckets.go.
For the complete sample code for deleting an Agentic Bucket, see delete_agentic_bucket.go.
For the complete sample code for creating the CnameToken required for domain ownership verification, see create_cname_token.go.
For the complete sample code for getting a CnameToken, see get_cname_token.go.
For the complete sample code for adding a CNAME record, see put_cname.go.
For the complete sample code for viewing CNAME records, see list_cname.go.
For the complete sample code for deleting a CNAME record, see delete_cname.go.
For the complete sample code for creating a Bucket Space, see bucket_space_put_bucket.go.
For the complete sample code for uploading an object to a Bucket Space, see bucket_space_put_object.go.
For more information about the parameters of CNAME-related operations, see CreateCnameToken, PutCname, ListCname, GetCnameToken and DeleteCname.