This topic describes how to create an inventory for a bucket and how to query, list, and delete the inventories configured for a bucket.
Usage notes
In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.
In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Configure OSSClient instances.
Make sure that you have the permissions to create, view, list, and delete inventories for a bucket. By default, the bucket owner has the permissions to perform the preceding operations. If you do not have the permissions to perform the preceding operations, ask the bucket owner to grant you the permissions.
You can configure up to 1,000 inventories for a bucket.
You must deploy the source bucket for which you want to configure an inventory in the same region as the destination bucket in which the inventory list is stored.
Add an inventory configuration
The following code provides an example on how to create an inventory for a bucket:
package main
import (
"fmt"
"os"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Set yourEndpoint to the endpoint of the bucket. For example, for the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the endpoint as needed.
// Set yourRegion to the region where the bucket is located. For example, for the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, set the region as needed.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Set the signature version.
clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
client, err := oss.New("yourEndpoint", "", "", clientOptions...)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
IsEnabled := true
// To encrypt the inventory using KMS, see the following settings.
//var invEncryption oss.InvEncryption
//var invSseOss oss.InvSseOss
//var invSseKms oss.InvSseKms
//invSseKms.KmsId = "<yourKmsId>" // Specify the KMS key ID.
//invEncryption.SseOss = &invSseOss // Encrypt the inventory using server-side encryption with OSS-managed keys (SSE-OSS).
//invEncryption.SseKms = &invSseKms // Encrypt the inventory using server-side encryption with KMS-managed keys (SSE-KMS).
invConfig := oss.InventoryConfiguration{
// The name of the inventory. The name must be unique within the bucket.
Id: "yourInventoryId2",
// Enable the inventory configuration.
IsEnabled: &IsEnabled,
// Set the inventory filter rule to specify the prefix for filtering objects.
Prefix: "yourFilterPrefix",
OSSBucketDestination: oss.OSSBucketDestination{
// The file format of the exported manifest file.
Format: "CSV",
// The ID of the account granted by the bucket owner, for example, 109885487000****.
AccountId: "yourGrantAccountId",
// The name of the role granted by the bucket owner for the operation, for example, acs:ram::109885487000****:role/ram-test.
RoleArn: "yourRoleArn",
// The name of the bucket where the exported inventory results are stored.
Bucket: "acs:oss:::" + "yourDestBucketName",
// The storage path prefix for the inventory results.
Prefix: "yourDestPrefix",
// If the inventory needs to be encrypted, see the following code.
//Encryption: &invEncryption,
},
// The export frequency of the manifest file.
Frequency: "Daily",
// Specifies whether to include all versions of objects in the inventory.
IncludedObjectVersions: "All",
OptionalFields: oss.OptionalFields{
// The configuration items included in the inventory results.
Field: []string{
"Size", "LastModifiedDate", "ETag", "StorageClass", "IsMultipartUploaded", "EncryptionStatus",
},
},
}
// Set yourBucketName to the name of the bucket for which you want to configure an inventory rule.
err = client.SetBucketInventory("yourBucketName", invConfig)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
}
View an inventory configuration
The following code provides an example on how to query an inventory configured for a bucket:
package main
import (
"encoding/xml"
"fmt"
"os"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Set yourEndpoint to the endpoint of the bucket. For example, for the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the endpoint as needed.
// Set yourRegion to the region where the bucket is located. For example, for the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, set the region as needed.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Set the signature version.
clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
client, err := oss.New("yourEndpoint", "", "", clientOptions...)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Set yourBucketName to the name of the bucket.
// Set yourInventoryId to the name of the inventory rule.
result, err := client.GetBucketInventory("yourBucketName", "yourInventoryId")
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Print the inventory information.
bs, err := xml.MarshalIndent(result, " ", " ")
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
fmt.Println(string(bs))
}
List inventory configurations
You can query up to 100 inventories in a single request. To query more than 100 inventories, you must send multiple requests and use the token returned for each request as the parameter for the next request.
The following code provides an example on how to list inventories configured for a bucket:
package main
import (
"encoding/xml"
"fmt"
"os"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Set yourEndpoint to the endpoint of the bucket. For example, for the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the endpoint as needed.
// Set yourRegion to the region where the bucket is located. For example, for the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, set the region as needed.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Set the signature version.
clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
client, err := oss.New("yourEndpoint", "", "", clientOptions...)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
var sumResult oss.ListInventoryConfigurationsResult
vmarker := ""
for {
// Set yourBucketName to the name of the bucket.
listResult, err := client.ListBucketInventory("yourBucketName", vmarker)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
sumResult.InventoryConfiguration = append(sumResult.InventoryConfiguration, listResult.InventoryConfiguration...)
if listResult.IsTruncated != nil && *listResult.IsTruncated {
vmarker = listResult.NextContinuationToken
} else {
break
}
}
// Print all inventory information.
bs, err := xml.MarshalIndent(sumResult, " ", " ")
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
fmt.Println(string(bs))
}
Delete an inventory configuration
The following code provides an example on how to delete an inventory configured for a bucket:
package main
import (
"fmt"
"os"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Set yourEndpoint to the endpoint of the bucket. For example, for the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the endpoint as needed.
// Set yourRegion to the region where the bucket is located. For example, for the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, set the region as needed.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Set the signature version.
clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
client, err := oss.New("yourEndpoint", "", "", clientOptions...)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Delete the inventory configuration.
// Set yourBucketName to the name of the bucket.
// Set yourInventoryId to the name of the inventory rule.
err = client.DeleteBucketInventory("yourBucketName", "yourInventoryId")
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
}
References
For the complete sample code for bucket inventory, see GitHub.
For more information about the API operation for creating an inventory for a bucket, see PutBucketInventory.
For more information about the API operation for querying an inventory configured for a bucket, see GetBucketInventory.
For more information about the API operation for listing inventories configured for a bucket, see ListBucketInventory.
For more information about the API operation for deleting the inventories configured for a bucket, see DeleteBucketInventory.