Updated on 2026-08-14 GMT+08:00

Configuring a Bucket Inventory Rule

Function

The bucket inventory function periodically generates a list of object metadata information for a bucket, helping you better understand object statuses in the bucket. The bucket owner or users who have the required permission can call this API to configure an inventory rule for a bucket. A maximum of 10 inventory rules can be configured for each bucket. For more information and constraints about bucket inventories, see Bucket Inventories.

To perform this operation, you must have the PutBucketInventoryConfiguration permission. By default, the bucket owner has this permission and can grant it to others.

For fusion buckets, inventory rules can be configured for an active bucket, but cannot be configured for a standby bucket.

Restrictions

Table 1 Constraints on bucket inventories

Item

Description

Bucket version

Inventories can be generated only for OBS 3.0 buckets, but the bucket that stores the inventory files can be any OBS version.

Maximum number of bucket inventory rules

A bucket can have a maximum of 10 inventory rules.

Source and destination buckets

  • The source bucket (for which a bucket inventory rule is configured) and the destination bucket (where the generated inventory files are stored) must belong to the same tenant.
  • The source bucket and the destination bucket must be in the same region.
  • The destination bucket cannot have default encryptionserver-side encryption enabled.

Functions

  • Inventory files can only be saved in CSV format.
  • Inventory rules can apply to all objects in a bucket or to a set of objects with the same name prefix.
  • The filter criteria of multiple inventory rules within the same bucket must not overlap.
    • If there is already an inventory rule for all objects in the bucket, any other inventory rule with an object name prefix specified cannot be created. To create a rule for only a set of objects, delete the inventory rule that applies to all objects first.
    • If there is already an inventory rule for a set of objects, a rule for all objects in the bucket cannot be created. To create a rule for all objects, delete all inventory rules that use a prefix filter.
    • Assume there is an inventory rule that uses the ab prefix as a filter. Any new inventory rule whose prefix overlaps with ab, such as a or abc, cannot be created. To create a rule with prefix a or abc, delete the overlapping rule first.
  • Only SSE-KMS can be used to encrypt bucket inventories.

Permissions

OBS must be authorized to upload inventory files to the destination bucket. The destination bucket needs a policy that grants OBS write permissions, with the Principal set to {"Service": "obs"}. For details, see Adding a Bucket Policy for the Destination Bucket.

Others

  • The bucket inventory function is not available to federated users.

Authorization Information

To call this API, you must be the bucket owner or have the permissions to configure bucket inventory rules. You are advised to use IAM or bucket policies for authorization. For details about OBS authorization methods, see Differences Between OBS Permissions Control Methods.

  • If you use IAM for authorization, you need to use either role/policy-based authorization or identity policy-based authorization and configure the required permission:
    • If you use role/policy-based authorization (IAM v3 APIs in the old IAM version), the obs:bucket:PutBucketInventoryConfiguration permission is required. For details, see Creating a Custom IAM Policy.
    • If you use identity policy-based authorization (IAM v5 APIs in the new IAM version), as shown in the following table, the obs:bucket:putBucketInventoryConfiguration permission is required. For details, see Creating a Custom IAM Identity Policy.

      Action

      Access Level

      Resource Type (*: required)

      Condition Key

      AliasAlias

      Dependency

      obs:bucket:putBucketInventoryConfiguration

      Write

      bucket *

      -

      -

      -

      • obs:EpochTime
      • obs:SourceIp
      • obs:TlsVersion
      • obs:CustomDomain
  • If you use bucket policies for authorization, the obs:bucket:PutBucketInventoryConfiguration permission is required. For details, see Creating a Custom Bucket Policy.

Method

func (obsClient ObsClient) SetBucketInventory(input *SetBucketInventoryInput, extensions ...extensionOptions) (output *BaseModel, err error)

Request Parameters

Table 2 SetBucketInventoryInput request parameters

Parameter

Type

Mandatory (Yes/No)

Description

Bucket

string

Yes

Bucket name.

Id

string

Yes

Inventory rule ID, which uniquely identifies an inventory rule.

Specifications: The value can contain a maximum of 64 characters.

Default value: none

Valid characters: letters, digits, hyphens (-), underscores (_), and periods (.)

IsEnabled

bool

Yes

Whether the inventory rule is enabled. If this parameter is set to true, inventory files will be generated. If not, inventory files will not be generated.

Destination

InventoryDestination

Yes

Storage location and format of the inventory files.

Schedule

InventorySchedule

Yes

Inventory generation frequency.

Filter

*InventoryFilter

No

Inventory filter configuration. The inventory contains only objects that meet the filter criteria (filtering by object name prefix). If no filter criteria are configured, all objects are included.

IncludedObjectVersions

string

No

Whether versions of objects are included in an inventory.

Valid values:

  • All: Versioning related fields including VersionId, IsLatest, and DeleteMarker will appear in inventory files, and information about all object versions will be included.
  • Current: Versioning related fields including VersionId, IsLatest, and DeleteMarker will not appear in inventory files, and only information about the current object version will be included.

OptionalFields

*InventoryOptionalFields

No

Optional fields included in the inventories.

Table 3 InventoryDestination parameters

Parameter

Type

Mandatory (Yes/No)

Description

Format

string

Yes

Inventory file format. Only the CSV format is supported.

Bucket

string

Yes

Name of the bucket for storing inventories.

Prefix

string

Yes

Name prefix for inventory files. If no prefix is configured, the inventory file names will start with the BucketInventory prefix by default.

Table 4 InventorySchedule parameters

Parameter

Type

Mandatory (Yes/No)

Description

Frequency

string

Yes

The intervals at which inventories are generated. You can set it to Daily or Weekly. An inventory is generated within one hour after it is configured for the first time, and subsequently at the specified intervals.

Valid values: Daily, Weekly

Table 5 InventoryFilter parameters

Parameter

Type

Mandatory (Yes/No)

Description

Prefix

string

No

Prefix for filtering objects. Only objects with the specified name prefix are included in the inventory.

Table 6 InventoryOptionalFields parameters

Parameter

Type

Mandatory (Yes/No)

Description

Fields

[]string

No

List of optional fields. Valid values are as follows:

Size, LastModifiedDate, StorageClass, ETag, IsMultipartUploaded, ReplicationStatus, EncryptionStatus

Sample Code

This example configures an inventory rule with ID inventory-rule-1 for bucket examplebucket.

package main

import (
	"fmt"
	"log"

        obs "github.com/huaweicloud/huaweicloud-sdk-go-obs/obs"
)

func main() {
    // Obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways. Using hard coding may result in leakage.
    // Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
        ak := os.Getenv("AccessKeyID")
        sk := os.Getenv("SecretAccessKey")
    // (Optional) If you use a temporary AK/SK pair and a security token to access OBS, you are not advised to use hard coding, which may result in information leakage. You can obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways.
    securityToken := os.Getenv("SecurityToken")


    // Enter the endpoint corresponding to the bucket. CN-Hong Kong is used here as an example. Replace it with the one currently in use.
    endPoint := "https://obs.ap-southeast-1.myhuaweicloud.com"




	// Create a client.
    obsClient, err := obs.New(ak, sk, endPoint, obs.WithSecurityToken(securityToken))
    if err != nil {
        fmt.Printf("Create obsClient error, errMsg: %s", err.Error())
    }

	// Configure an inventory rule.
	input := &obs.SetBucketInventoryInput{
		Bucket: "my-bucket",
		InventoryConfiguration: obs.InventoryConfiguration{
			Id:        "inventory-rule-1",
			IsEnabled: true,
			Destination: obs.InventoryDestination{
				Format: "CSV",
				Bucket: "report-bucket",
				Prefix: "inventory-reports/",
			},
			Schedule: obs.InventorySchedule{
				Frequency: "Daily", // Inventories are generated once a day.
			},
			IncludedObjectVersions: "All", // All object versions are included.
			OptionalFields: &obs.InventoryOptionalFields{
				Fields: []string{
					"Size",
					"LastModifiedDate",
					"StorageClass",
					"ETag",
					"IsMultipartUploaded",
				},
			},
		},
	}

	output, err := client.SetBucketInventory(input)
	if err != nil {
		log.Fatalf("Failed to set bucket inventory: %v", err)
	}

	fmt.Printf("Request ID: %s\n", output.RequestId)
	fmt.Println("Inventory configuration set successfully")
}