Metadata management (Go SDK V2)

Updated at:

This topic describes how to create, view, update, and delete the metadata table configurations of a bucket by using Go SDK V2, including the journal table configuration and the inventory table configuration.

Overview

Metadata management provides metadata table configuration capabilities for a bucket, including the following two types of configurations:

  • Journal table configuration (JournalTableConfiguration): Use RecordExpiration to control whether journal records are enabled (Expiration is set to ENABLED or DISABLED) and the record retention period (Days).

  • Inventory table configuration (InventoryTableConfiguration): Use ConfigurationState to control whether the inventory table is enabled (set to ENABLED or DISABLED), and optionally use EncryptionConfiguration to specify the encryption algorithm (SseAlgorithm).

Notes

  • The sample code in this topic uses the region ID cn-hangzhou of the China (Hangzhou) region. Replace it with the actual region as needed.

  • In this topic, access credentials are obtained from environment variables. Make sure that you have configured access credentials (AccessKey) before use.

  • Make sure that you have the permissions to perform metadata table configuration operations on the target bucket. By default, the bucket owner has these permissions. If you do not have the required permissions, contact the bucket owner to grant you the permissions.

  • You can also enable, modify, or disable metadata management in the OSS console.

Sample code

Create the metadata table configuration

The following code provides an example of how to create the metadata table configuration for a bucket (setting both the journal table and the inventory table).

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"
)

var (
	region     string
	bucketName string
)

func init() {
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}

func main() {
	flag.Parse()
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	client := oss.NewClient(cfg)

	request := &oss.CreateBucketMetadataConfigurationRequest{
		Bucket: oss.Ptr(bucketName),
		MetadataConfiguration: &oss.MetadataConfiguration{
			JournalTableConfiguration: &oss.JournalTableConfiguration{
				RecordExpiration: &oss.MetadataTableRecordExpiration{
					Expiration: oss.Ptr("ENABLED"),
					Days:       oss.Ptr(7),
				},
			},
			InventoryTableConfiguration: &oss.InventoryTableConfiguration{
				ConfigurationState: oss.Ptr("ENABLED"),
				EncryptionConfiguration: &oss.MetadataTableEncryptionConfiguration{
					SseAlgorithm: oss.Ptr("AES256"),
				},
			},
		},
	}
	result, err := client.CreateBucketMetadataConfiguration(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to create bucket metadata table configuration %v", err)
	}

	log.Printf("create bucket metadata table configuration result:%#v\n", result)
}

View the metadata table configuration

The following code provides an example of how to view the metadata table configuration of 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"
)

var (
	region     string
	bucketName string
)

func init() {
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}

func main() {
	flag.Parse()
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	client := oss.NewClient(cfg)

	request := &oss.GetBucketMetadataConfigurationRequest{
		Bucket: oss.Ptr(bucketName),
	}
	result, err := client.GetBucketMetadataConfiguration(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to get bucket metadata table configuration %v", err)
	}

	log.Printf("get bucket metadata table configuration result:%#v\n", result)
}

Update the inventory table configuration

The following code provides an example of how to update the inventory table configuration of 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"
)

var (
	region     string
	bucketName string
)

func init() {
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}

func main() {
	flag.Parse()
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	client := oss.NewClient(cfg)

	request := &oss.UpdateBucketMetadataInventoryTableConfigurationRequest{
		Bucket: oss.Ptr(bucketName),
		InventoryTableConfiguration: &oss.InventoryTableConfiguration{
			ConfigurationState: oss.Ptr("DISABLED"),
		},
	}
	result, err := client.UpdateBucketMetadataInventoryTableConfiguration(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to update bucket metadata inventory table configuration %v", err)
	}

	log.Printf("update bucket metadata inventory table configuration result:%#v\n", result)
}

Update the journal table configuration

The following code provides an example of how to update the journal table configuration of 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"
)

var (
	region     string
	bucketName string
)

func init() {
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}

func main() {
	flag.Parse()
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	client := oss.NewClient(cfg)

	request := &oss.UpdateBucketMetadataJournalTableConfigurationRequest{
		Bucket: oss.Ptr(bucketName),
		JournalTableConfiguration: &oss.JournalTableConfiguration{
			RecordExpiration: &oss.MetadataTableRecordExpiration{
				Expiration: oss.Ptr("DISABLED"),
			},
		},
	}
	result, err := client.UpdateBucketMetadataJournalTableConfiguration(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to update bucket metadata journal table configuration %v", err)
	}

	log.Printf("update bucket metadata journal table configuration result:%#v\n", result)
}

Delete the metadata table configuration

The following code provides an example of how to delete the metadata table configuration of 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"
)

var (
	region     string
	bucketName string
)

func init() {
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}

func main() {
	flag.Parse()
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	client := oss.NewClient(cfg)

	request := &oss.DeleteBucketMetadataConfigurationRequest{
		Bucket: oss.Ptr(bucketName),
	}
	result, err := client.DeleteBucketMetadataConfiguration(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to delete bucket metadata table configuration %v", err)
	}

	log.Printf("delete bucket metadata table configuration result:%#v\n", result)
}

References