Use the OSS Go SDK V2 to create, query, list, and delete bucket inventory configurations.
Prerequisites
Before you begin, make sure that you have:
Configured access credentials as environment variables
Permissions to create, query, list, and delete inventories for the bucket. By default, the bucket owner has these permissions. If not, ask the bucket owner to grant them.
A destination bucket in the same region as the source bucket
Usage notes
A bucket can have up to 1,000 inventory configurations.
The source bucket and the destination bucket must be in the same region.
Each list request returns up to 100 inventory configurations. If there are more than 100, the response includes a token — pass it as a parameter in the next request to paginate through more results.
The code samples use the
cn-hangzhouregion. Replace it with your actual region. For available regions and endpoints, see OSS regions and endpoints. If you access OSS from other Alibaba Cloud products in the same region, use an internal endpoint.
Query an inventory
Use GetBucketInventoryRequest to retrieve the configuration of a specific inventory by name.
package main
import (
"context"
"flag"
"log"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)
var (
region string
bucketName string
)
func init() {
flag.StringVar(®ion, "region", "", "The region where the bucket is located.")
flag.StringVar(&bucketName, "bucket", "", "The bucket name.")
}
func main() {
flag.Parse()
var inventoryId = "inventory id"
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region)
client := oss.NewClient(cfg)
request := &oss.GetBucketInventoryRequest{
Bucket: oss.Ptr(bucketName),
InventoryId: oss.Ptr(inventoryId),
}
result, err := client.GetBucketInventory(context.TODO(), request)
if err != nil {
log.Fatalf("failed to get bucket inventory %v", err)
}
log.Printf("get bucket inventory result: %#v\n", result)
}List inventories
Each request returns up to 100 inventory configurations. If a bucket has more than 100, the response includes a token — pass it as a parameter in the next request to retrieve the next page.
package main
import (
"context"
"flag"
"log"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)
var (
region string
bucketName string
)
func init() {
flag.StringVar(®ion, "region", "", "The region where the bucket is located.")
flag.StringVar(&bucketName, "bucket", "", "The bucket name.")
}
func main() {
flag.Parse()
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region)
client := oss.NewClient(cfg)
request := &oss.ListBucketInventoryRequest{
Bucket: oss.Ptr(bucketName),
}
result, err := client.ListBucketInventory(context.TODO(), request)
if err != nil {
log.Fatalf("failed to list bucket inventory %v", err)
}
log.Printf("list bucket inventory result: %#v\n", result)
}API reference
| Operation | Go method | Reference |
|---|---|---|
| Create an inventory | client.PutBucketInventory() | PutBucketInventory |
| Query an inventory | client.GetBucketInventory() | GetBucketInventory |
| List inventories | client.ListBucketInventory() | ListBucketInventory |
| Delete an inventory | client.DeleteBucketInventory() | DeleteBucketInventory |