The following code provides an example of how to create an inventory for a 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/credentials"
)
// Define global variables.
var (
region string // The region in which the bucket is located.
bucketName string // The name of the bucket.
)
// Specify the init function used to initialize command line parameters.
func init() {
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}
func main() {
// Parse command line parameters.
flag.Parse()
var (
accountId = "account id of the bucket" // Specify the ID of the Alibaba Cloud account to which the bucket owner grants permissions to perform the operation. Example: 109885487000****.
inventoryId = "inventory id" // The name of the inventory. The name must be globally unique in the bucket.
)
// Check whether the name of the bucket is specified.
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
// Check whether the region is specified.
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
// Load the default configurations and specify the credential provider and region.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region)
// Create an OSS client.
client := oss.NewClient(cfg)
// Create a request to configure an inventory for the bucket.
putRequest := &oss.PutBucketInventoryRequest{
Bucket: oss.Ptr(bucketName), // The name of the bucket.
InventoryId: oss.Ptr(inventoryId), // The name of the inventory specified by the user.
InventoryConfiguration: &oss.InventoryConfiguration{
Id: oss.Ptr(inventoryId), // The name of the inventory specified by the user.
IsEnabled: oss.Ptr(true), // Enable the inventory.
Filter: &oss.InventoryFilter{
Prefix: oss.Ptr("filterPrefix"), // Specify the rule that is used to filter the objects included in inventories.
LastModifyBeginTimeStamp: oss.Ptr(int64(1637883649)), // The timestamp that specifies the start time of the last modification.
LastModifyEndTimeStamp: oss.Ptr(int64(1638347592)), // The timestamp that specifies the end time of the last modification.
LowerSizeBound: oss.Ptr(int64(1024)), // The lower size limit of files (unit: bytes).
UpperSizeBound: oss.Ptr(int64(1048576)), // The upper size limit of files (unit: bytes).
StorageClass: oss.Ptr("Standard,IA"), // The storage class.
},
Destination: &oss.InventoryDestination{
OSSBucketDestination: &oss.InventoryOSSBucketDestination{
Format: oss.InventoryFormatCSV, // The format of the exported inventory lists.
AccountId: oss.Ptr(accountId), // Specify the ID of the account that is granted permissions by the bucket owner to perform the operation. Example: 109885487000****.
RoleArn: oss.Ptr("acs:ram::" + accountId + ":role/AliyunOSSRole"), // Specify the name of the RAM role that is granted permissions by the bucket owner to perform the operation. Example: acs:ram::109885487000****:role/ram-test.
Bucket: oss.Ptr("acs:oss:::" + bucketName), // Specify the name of the bucket in which you want to store the generated inventory lists.
Prefix: oss.Ptr("export/"), // Specify the prefix of the path in which you want to store the generated inventory lists.
},
},
Schedule: &oss.InventorySchedule{
Frequency: oss.InventoryFrequencyDaily, // The frequency at which the inventory list is exported (daily).
},
IncludedObjectVersions: oss.Ptr("All"), // Specify whether to include all versions of objects or only the current versions of objects in the inventory list.
},
}
// Execute the request.
putResult, err := client.PutBucketInventory(context.TODO(), putRequest)
if err != nil {
log.Fatalf("failed to put bucket inventory %v", err)
}
// Display the result.
log.Printf("put bucket inventory result:%#v\n", putResult)
}