Tüm Ürünler
Search
Belge Merkezi

:Go 2.0 için OSS SDK ile Veri Çoğaltma

Son Güncelleme:Oct 02, 2026

Veri çoğaltma özelliklerini kullanarak, nesnelerin oluşturulması, üzerine yazılması ve silinmesi gibi nesneler üzerinde gerçekleştirilen işlemlerle birlikte nesneleri kaynak paketten hedef pakete otomatik olarak senkronize edebilirsiniz. Object Storage Service (OSS), iki veri çoğaltma özelliği sunar: bölgeler arası çoğaltma (CRR) ve aynı bölge çoğaltma (SRR).

Notlar

  • Bu konudaki örnek kod, China (Hangzhou) bölgesinin bölge kimliği olan cn-hangzhou değerini kullanır. Bir paketteki kaynaklara erişmek için varsayılan olarak genel uç nokta kullanılır. Paketin bulunduğu aynı bölgedeki diğer Alibaba Cloud hizmetlerini kullanarak paketteki kaynaklara erişmek istiyorsanız dahili uç noktayı kullanın. Object Storage Service (OSS) tarafından desteklenen bölgeler ve uç noktalar hakkında daha fazla bilgi için bkz. OSS bölgeleri ve uç noktaları.

  • Bu konuda, erişim kimlik bilgileri ortam değişkenlerinden alınır. Erişim kimlik bilgilerinin nasıl yapılandırılacağı hakkında daha fazla bilgi için bkz. Erişim kimlik bilgilerini yapılandırma (Go SDK V1).

  • Varsayılan olarak bir Alibaba Cloud hesabına veri çoğaltmayla ilgili izinler verilir. Verileri bir RAM kullanıcısı olarak veya Security Token Service (STS) tarafından sağlanan geçici erişim kimlik bilgilerini kullanarak çoğaltmak istiyorsanız gerekli izinlere sahip olmanız gerekir. Örneğin:

    • Veri çoğaltmayı etkinleştirmek için oss:PutBucketReplication izni gereklidir.

    • Çoğaltma zamanı kontrolü (RTC) özelliğini etkinleştirmek ve devre dışı bırakmak için oss:PutBucketRtc izni gereklidir.

    • Veri çoğaltma kurallarını sorgulamak için oss:GetBucketReplication izni gereklidir.

    • Kullanılabilir hedef bölgeleri sorgulamak için oss:GetBucketReplicationLocation izni gereklidir.

    • Bir veri çoğaltma görevinin ilerleme durumunu sorgulamak için oss:GetBucketReplicationProgress izni gereklidir.

    • Veri çoğaltmayı devre dışı bırakmak için oss:DeleteBucketReplication izni gereklidir.

Örnek kod

Veri çoğaltmayı etkinleştirin

Önemli

Veri çoğaltmayı etkinleştirmeden önce kaynak paketin ve hedef paketin sürüm oluşturulmamış veya sürüm oluşturmanın etkin olduğu paketler olduğundan emin olun.

Aşağıdaki kod, veri çoğaltmanın nasıl etkinleştirileceğine dair bir örnek sağlar. Kaynak ve hedef paketler aynı bölgede veya farklı bölgelerde bulunabilir.

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

// Define global variables.
var (
	region     string // Region in which the bucket is located.
	bucketName string // Name of the bucket.
)

// Specify the init function used to initialize command line parameters.
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() {
	// Parse command line parameters.
	flag.Parse()

	var (
		targetBucket   = "target bucket name" // Name of the destination bucket.
		targetLocation = "oss-cn-beijing"     // Region in which the destination bucket is located.
	)

	// Check whether the name of the bucket is specified.
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	// Check whether the region is specified.
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	// Load the default configurations and specify the credential provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	// Create an OSS client.
	client := oss.NewClient(cfg)

	// Create a request to enable data replication for the bucket.
	request := &oss.PutBucketReplicationRequest{
		Bucket: oss.Ptr(bucketName), // Name of the bucket.
		ReplicationConfiguration: &oss.ReplicationConfiguration{
			Rules: []oss.ReplicationRule{
				{
					RTC: &oss.ReplicationTimeControl{
						Status: oss.Ptr("enabled"), // Enable the RTC feature.
					},
					Destination: &oss.ReplicationDestination{
						Bucket:       oss.Ptr(targetBucket),   // Name of the destination bucket.
						Location:     oss.Ptr(targetLocation), // Region in which the destination bucket is located.
						TransferType: oss.TransferTypeOssAcc,  // Type of transfer.
					},
					HistoricalObjectReplication: oss.HistoricalObjectReplicationEnabled, // Enable the historical data replication feature.
				},
			},
		},
	}

	// Enable data replication.
	result, err := client.PutBucketReplication(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to put bucket replication %v", err)
	}

	// Display the result.
	log.Printf("put bucket replication result:%#v\n", result)
}

Veri çoğaltma kurallarını sorgulayın

Aşağıdaki kod, bir paketin veri çoğaltma kurallarının nasıl sorgulanacağına dair bir örnek sağlar.

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

// Define global variables.
var (
	region     string // Region in which the bucket is located.
	bucketName string // Name of the bucket.
)

// Specify the init function used to initialize command line parameters.
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() {
	// Parse command line parameters.
	flag.Parse()

	// Check whether the name of the bucket is specified.
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	// Check whether the region is specified.
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	// Load the default configurations and specify the credential provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	// Create an OSS client.
	client := oss.NewClient(cfg)

	// Create a request to query the data replication rules of the bucket.
	request := &oss.GetBucketReplicationRequest{
		Bucket: oss.Ptr(bucketName),
	}

	// Perform the query operation.
	getResult, err := client.GetBucketReplication(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to get bucket replication %v", err)
	}

	// Display the result.
	log.Printf("get bucket replication result:%#v\n", getResult)
}

RTC özelliğini etkinleştirin veya devre dışı bırakın

Aşağıdaki kod, bir CRR kuralı için RTC özelliğinin nasıl etkinleştirileceğine veya devre dışı bırakılacağına dair bir örnek sağlar.

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

// Define global variables.
var (
	region     string // Region in which the bucket is located.
	bucketName string // Name of the bucket.
)

// Specify the init function used to initialize command line parameters.
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() {
	// Parse command line parameters.
	flag.Parse()

	var (
		ruleId = "replication id"
	)

	// Check whether the name of the bucket is specified.
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	// Check whether the region is specified.
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	// Load the default configurations and specify the credential provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	// Create an OSS client.
	client := oss.NewClient(cfg)

	// Create a request to enable or disable the RTC feature.
	request := &oss.PutBucketRtcRequest{
		Bucket: oss.Ptr(bucketName),
		RtcConfiguration: &oss.RtcConfiguration{
			RTC: &oss.ReplicationTimeControl{
				Status: oss.Ptr("enabled"),
			},
			ID: oss.Ptr(ruleId), // Specify the ID of the replication rule.
		},
	}

	// Perform the operation to enable or disable the RTC feature.
	result, err := client.PutBucketRtc(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to put bucket rtc %v", err)
	}

	// Display the result.
	log.Printf("put bucket rtc result:%#v\n", result)
}

Verilerin çoğaltılabileceği bölgeleri sorgulayın

Aşağıdaki kod, bir paketten verilerin çoğaltılabileceği bölgelerin nasıl sorgulanacağına dair bir örnek sağlar.

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

// Define global variables.
var (
	region     string // Region in which the bucket is located.
	bucketName string // Name of the bucket.
)

// Specify the init function used to initialize command line parameters.
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() {
	// Parse command line parameters.
	flag.Parse()

	// Check whether the name of the bucket is specified.
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	// Check whether the region is specified.
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	// Load the default configurations and specify the credential provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	// Create an OSS client.
	client := oss.NewClient(cfg)

	// Create a request to query the regions to which data in the source bucket can be replicated.
	request := &oss.GetBucketReplicationLocationRequest{
		Bucket: oss.Ptr(bucketName),
	}

	// Perform the query operation.
	result, err := client.GetBucketReplicationLocation(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to get bucket replication location %v", err)
	}

	// Display information about the regions.
	for _, location := range result.ReplicationLocation.Locations {
		log.Printf("Bucket Replication Location: %s", location)
	}

	// Display the region for which the RTC feature can be enabled.
	for _, rtcLocation := range result.ReplicationLocation.LocationRTCConstraint.Locations {
		log.Printf("Bucket Replication Location RTC Location: %s", rtcLocation)
	}
}

Bir veri çoğaltma görevinin ilerleme durumunu sorgulayın

Not

Geçmiş veri çoğaltma görevlerinin ve artımlı veri çoğaltma görevlerinin ilerleme durumunu sorgulayabilirsiniz.

  • Geçmiş veri çoğaltma görevlerinin ilerleme durumu yüzde olarak ifade edilir. Geçmiş veri çoğaltma görevlerinin ilerleme durumunu yalnızca geçmiş veri çoğaltmanın etkin olduğu paketler için sorgulayabilirsiniz.

  • Artımlı veri çoğaltma görevlerinin ilerleme durumu bir zaman noktası olarak ifade edilir. Bu zaman noktasından önce kaynak pakette depolanan veriler çoğaltılır.

Aşağıdaki kod, bir pakette belirli bir çoğaltma kuralı kimliğine sahip olan veri çoğaltma görevinin ilerleme durumunun nasıl sorgulanacağına dair bir örnek sağlar.

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

// Define global variables.
var (
	region     string // Region in which the bucket is located.
	bucketName string // Name of the bucket.
)

// Specify the init function used to initialize command line parameters.
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() {
	// Parse command line parameters.
	flag.Parse()

	// Specify the ID of the replication rule.
	var ruleId = "replication id"

	// Check whether the name of the bucket is specified.
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	// Check whether the region is specified.
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	// Load the default configurations and specify the credential provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	// Create an OSS client.
	client := oss.NewClient(cfg)

	// Create a request to query the progress of the data replication task.
	request := &oss.GetBucketReplicationProgressRequest{
		Bucket: oss.Ptr(bucketName),
		RuleId: oss.Ptr(ruleId), // Specify the ID of the replication rule.
	}

	// Perform the query operation.
	result, err := client.GetBucketReplicationProgress(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to get bucket replication progress %v", err)
	}

	// Display the progress of the data replication task.
	for _, repProgressRule := range result.ReplicationProgress.Rules {
		log.Printf("Rule Id: %s", repProgressRule.ID)
		log.Printf("Status: %s", repProgressRule.Status)
		log.Printf("Replication Progress Rule Destination Bucket: %s", *repProgressRule.Destination.Bucket)
		log.Printf("Replication Progress Rule Destination Location: %s", *repProgressRule.Destination.Location)
		log.Printf("Replication Progress Rule Destination TransferType: %v", repProgressRule.Destination.TransferType)
		log.Printf("Replication Progress Rule Historical Object Replication: %s", *repProgressRule.HistoricalObjectReplication)
	}
}

Veri çoğaltmayı devre dışı bırakın

Belirtilen kaynak paket için yapılandırılan çoğaltma kuralını silebilir ve böylece paket için veri çoğaltmayı devre dışı bırakabilirsiniz.

Aşağıdaki kod, belirli bir kimliğe sahip çoğaltma kuralının nasıl silineceğine dair bir örnek sağlar.

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

// Define global variables.
var (
	region     string // Region in which the bucket is located.
	bucketName string // Name of the bucket.
)

// Specify the init function used to initialize command line parameters.
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() {
	// Parse command line parameters.
	flag.Parse()

	// Specify the ID of the replication rule.
	var ruleId = "replication id"

	// Check whether the name of the bucket is specified.
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	// Check whether the region is specified.
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	// Load the default configurations and specify the credential provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	// Create an OSS client.
	client := oss.NewClient(cfg)

	// Create a request to delete the data replication rule configured for the bucket.
	request := &oss.DeleteBucketReplicationRequest{
		Bucket: oss.Ptr(bucketName), // Name of the bucket.
		ReplicationRules: &oss.ReplicationRules{
			IDs: []string{ruleId}, // List of replication rule IDs.
		},
	}

	// Perform the operation to delete the data replication rule.
	result, err := client.DeleteBucketReplication(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to delete bucket replication %v", err)
	}

	// Display the result.
	log.Printf("delete bucket replication result:%#v\n", result)
}

Başvurular

  • Veri çoğaltmayı etkinleştirmek için çağırabileceğiniz API işlemi hakkında daha fazla bilgi için bkz. PutBucketReplication.

  • Mevcut CRR kuralları için RTC özelliğini etkinleştirmek veya devre dışı bırakmak üzere kullanılan API işlemi hakkında daha fazla bilgi için bkz. PutBucketRTC.

  • Veri çoğaltma kurallarını sorgulamak üzere kullanılan API işlemi hakkında daha fazla bilgi için bkz. GetBucketReplication.

  • Verilerin çoğaltılabileceği bölgeleri sorgulamak üzere kullanılan API işlemi hakkında daha fazla bilgi için bkz. GetBucketReplicationLocation.

  • Bir veri çoğaltma görevinin ilerleme durumunu sorgulamak üzere kullanılan API işlemi hakkında daha fazla bilgi için bkz. GetBucketReplicationProgress.

  • Veri çoğaltmayı devre dışı bırakmak üzere kullanılan API işlemi hakkında daha fazla bilgi için bkz. DeleteBucketReplication.