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
| 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 |
|
| Functions |
|
| 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 |
|
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)
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
| 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:
|
| OptionalFields | *InventoryOptionalFields | No | Optional fields included in the inventories. |
| 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. |
| 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 |
| 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. |
| 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")
} Helpful Links
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot