すべてのプロダクト
Search
ドキュメントセンター

Object Storage Service:リソースグループ

最終更新日:Nov 11, 2024

このトピックでは、バケットのリソースグループを設定し、バケットが属するリソースグループのIDを照会する方法について説明します。

使用上の注意

  • このトピックでは、中国 (杭州) リージョンのパブリックエンドポイントを使用します。 OSSと同じリージョンにある他のAlibaba CloudサービスからOSSにアクセスする場合は、内部エンドポイントを使用します。 OSSリージョンとエンドポイントの詳細については、「リージョン、エンドポイント、オープンポート」をご参照ください。

  • このトピックでは、アクセス資格情報は環境変数から取得します。 アクセス資格情報の設定方法の詳細については、「アクセス資格情報の設定」をご参照ください。

  • このトピックでは、OSSエンドポイントを使用してOSSClientインスタンスを作成します。 カスタムドメイン名またはSTS (Security Token Service) を使用してOSSClientインスタンスを作成する場合は、「初期化」をご参照ください。

  • バケットのリソースグループを設定するには、oss:PutBucketResourceGroup権限が必要です。 バケットが属するリソースグループのIDを照会するには、oss:GetBucketResourceGroup権限が必要です。 詳細については、「RAMユーザーへのカスタムポリシーのアタッチ」をご参照ください。

バケットのリソースグループの設定

重要

リソースグループのIDを指定しない場合、バケットはデフォルトリソースグループに属します。 バケットを特定のリソースグループに追加する場合は、リソースグループが作成されていることを確認してください。 詳細については、「リソースグループの設定」をご参照ください。

次のコードは、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!")
}

バケットが属するリソースグループのIDを照会する

次のコードでは、examplebucketという名前のバケットが属するリソースグループのIDを照会する方法の例を示します。

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

関連ドキュメント

  • バケットのリソースグループを設定するために呼び出すことができるAPI操作の詳細については、「PutBucketResourceGroup」をご参照ください。

  • バケットが属するリソースグループのIDを照会するために呼び出すことができるAPI操作の詳細については、「GetBucketResourceGroup」をご参照ください。