Resource groups (OSS SDK for Go 1.0)

Updated at:

This topic describes how to configure a resource group for a bucket and query the ID of a resource group to which the bucket belongs.

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.

  • This topic demonstrates creating an OSSClient instance with an OSS endpoint. For alternative configurations, such as using a custom domain or authenticating with credentials from Security Token Service (STS), see Configure a client (Go SDK V1).

  • To configure a resource group for a bucket, you must have the oss:PutBucketResourceGroup permission. To query the ID of the resource group to which a bucket belongs, you must have the oss:GetBucketResourceGroup permission. For more information, see Grant a custom policy.

Configure a resource group for a bucket

Important

If you do not specify the ID of a resource group, the bucket belongs to the default resource group. If you want to add the bucket to a specific resource group, make sure that the resource group is created. For more information, see Configure a resource group.

The following code provides an example on how to configure a resource group for a bucket named examplebucket:

package main

import (
	"fmt"
	"os"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

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. 
    // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. Specify the actual region.
    clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
    clientOptions = append(clientOptions, oss.Region("yourRegion"))
    // Specify the version of the signature algorithm.
    clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
    client, err := oss.New("yourEndpoint", "", "", clientOptions...)
    if err != nil {
    	    fmt.Println("Error:", err)
	    os.Exit(-1)
    }
    // Specify the name of the bucket. Example: examplebucket. 
    bucketName := "examplebucket"
    // Configure the resource group to which the bucket belongs. 
    resourceGroup := oss.PutBucketResourceGroup{
                // Specify the ID of the resource group. If you do not specify a resource group ID, the bucket belongs to the default resource group. 
        ResourceGroupId: "rg-aekz****",
    }
    err = client.PutBucketResourceGroup(bucketName, resourceGroup)
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
    fmt.Println("Bucket Resource Group Set Success!")
}

Query the ID of a resource group to which a bucket belongs

The following code provides an example on how to query the ID of a resource group to which a bucket named examplebucket belongs:

package main

import (
	"fmt"
	"os"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

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. 
    // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. Specify the actual region.
    clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
    clientOptions = append(clientOptions, oss.Region("yourRegion"))
    // Specify the version of the signature algorithm.
    clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
    client, err := oss.New("yourEndpoint", "", "", clientOptions...)
    if err != nil {
	    fmt.Println("Error:", err)
	    os.Exit(-1)
    }
    // Specify the name of the bucket. Example: examplebucket. 
    bucketName := "examplebucket"
    // Query the ID of the resource group to which the bucket belongs. 
    result, err := client.GetBucketResourceGroup(bucketName)
    if err != nil {
        fmt.Println("Error:", err)
        os.Exit(-1)
    }
    fmt.Printf("Resource Group Id:%s\n", result.ResourceGroupId)
}

References

  • For more information about the API operation that you can call to configure a resource group for a bucket, see PutBucketResourceGroup.

  • For more information about the API operation that you can call to query the ID of the resource group to which a bucket belongs, see GetBucketResourceGroup.