All Products
Search
Document Center

Object Storage Service:Bucket inventory

Last Updated:Aug 23, 2023

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 by using other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about the regions and endpoints supported by OSS, 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 Initialization.

  • 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, apply for the permissions from the bucket owner.
  • 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.

Create an inventory for a bucket

The following code provides an example on how to create an inventory for a bucket:

package main

import (
	"fmt"
	"github.com/aliyun/aliyun-oss-go-sdk/oss"
	"os"
)

func main() {
	/// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// Create an OSSClient instance. 
	// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. 
	client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	IsEnabled := true

	// The following code provides an example on how to encrypt the inventory lists by using customer master keys (CMKs) managed by Key Management Service (KMS). 
	//var invEncryption oss.InvEncryption
	//var invSseOss oss.InvSseOss
	//var invSseKms oss.InvSseKms
	//invSseKms.KmsId = "<yourKmsId>" // Specify the ID of CMK that is managed by KMS. 
	//invEncryption.SseOss = &invSseOss // Use OSS-managed keys (SSE-OSS) to encrypt the inventory lists. 
	//invEncryption.SseKms = &invSseKms // Use CMKs managed by KMS (SSE-KMS) to encrypt inventory lists. 

	invConfig := oss.InventoryConfiguration{
		// Specify the name of the inventory. The name must be globally unique in the current bucket. 
		Id: "yourInventoryId2",
		// Enable the inventory. 
		IsEnabled: &IsEnabled,

		// Specify the rule that is used to filter the objects included in inventories. The following code provides an example on how to filter the objects by prefix. 
		Prefix: "yourFilterPrefix",
		OSSBucketDestination: oss.OSSBucketDestination{
			// Specify the format of the exported inventory lists. 
			Format: "CSV",

			// Specify the ID of the account that is granted permissions by the bucket owner to perform the operation. Example: 109885487000****. 
			AccountId: "yourGrantAccountId",

			// 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. 
			RoleArn: "yourRoleArn",

			// Specify the name of the bucket in which you want to store the generated inventory lists. 
			Bucket: "acs:oss:::" + "yourDestBucketName",

			// Specify the prefix of the path in which you want to store the generated inventory lists. 
			Prefix: "yourDestPrefix",

			// The following sample code provides an example on how to encrypt inventory lists. 
			//Encryption:     &invEncryption,
		},

		// Specify the frequency at which inventory lists are exported. 
		Frequency: "Daily",

		// Specify whether to include all versions of objects or only the current versions of objects in the inventory lists. 
		IncludedObjectVersions: "All",

		OptionalFields: oss.OptionalFields{
			// Specify the fields that are included in inventory lists. 
			Field: []string{
				"Size", "LastModifiedDate", "ETag", "StorageClass", "IsMultipartUploaded", "EncryptionStatus",
			},
		},
	}
	// Specify the name of the bucket for which you want to configure the inventory. 
	err = client.SetBucketInventory("yourBucketName", invConfig)
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
}

Query the inventories of a bucket

The following code provides an example on how to query an inventory configured for a bucket:

package main

import (
	"encoding/xml"
	"fmt"
	"github.com/aliyun/aliyun-oss-go-sdk/oss"
	"os"
)

func main() {
	/// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// Create an OSSClient instance. 
	// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. 
	client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	// Specify the name of the bucket 
	// Specify the name of the inventory. 
	result, err := client.GetBucketInventory("yourBucketName", "yourInventoryId")
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// Display the inventory. 
	bs, err := xml.MarshalIndent(result, "  ", "    ")
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	fmt.Println(string(bs))
}

List multiple inventories at a time

Note You can query up to 100 inventories in a single request. If you want to query more than 100 inventories, send multiple requests and use the token returned for each request as a 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"
	"github.com/aliyun/aliyun-oss-go-sdk/oss"
	"os"
)

func main() {
	/// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// Create an OSSClient instance. 
	// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. 
	client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	var sumResult oss.ListInventoryConfigurationsResult
	vmarker := ""
	for {
		// Specify 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
		}
	}

	// Display the inventories of the bucket. 
	bs, err := xml.MarshalIndent(sumResult, "  ", "    ")
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	fmt.Println(string(bs))
}

Delete an inventory of a bucket

The following code provides an example on how to delete an inventory configured for a bucket:

package main

import (
	"fmt"
	"github.com/aliyun/aliyun-oss-go-sdk/oss"
	"os"
)

func main() {
	/// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// Create an OSSClient instance. 
	// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. 
	client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	// Delete the inventory. 
	// Specify the name of the bucket 
	// Specify the name of the inventory. 
	err = client.DeleteBucketInventory("yourBucketName", "yourInventoryId")
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
}

References

  • For the complete sample code for managing bucket inventories, visit 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.