Todos os produtos
Search
Central de documentação

Object Storage Service:Acesso em tempo real a objetos Archive com o OSS SDK for Go 2.0

Última atualização: Jul 03, 2026

Este tópico descreve como ativar ou desativar o acesso em tempo real a objetos Archive para buckets usando o OSS SDK for Go V2.

Observações

  • O código de exemplo neste tópico usa o ID da região cn-hangzhou, correspondente à região China (Hangzhou). Por padrão, o endpoint público acessa os recursos do bucket. Para acessar esses recursos por meio de outros serviços da Alibaba Cloud na mesma região do bucket, use um endpoint interno. Para mais informações sobre as regiões e endpoints compatíveis com o Object Storage Service (OSS), consulte Regiões e endpoints do OSS.

  • Neste tópico, as credenciais de acesso são obtidas de variáveis de ambiente. Para saber mais sobre como configurar credenciais de acesso, consulte Configurar credenciais de acesso (Go SDK V1).

Código de exemplo

Ativar acesso em tempo real a objetos Archive

O código a seguir exemplifica como ativar o acesso em tempo real a objetos Archive para um bucket.

package main

import (
	"context" 
	"flag"  
	"log"   

	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"           // Import the OSS SDK package.
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials" // Import the package for processing authentication information.
)

var (
	region     string // Define a variable to store the region information obtained from the command line。
	bucketName string // Define a variable to store the bucket name obtained from the command line.
)

// The init function is executed before the main function to initialize the program.
func init() {
	// Set command line parameters to specify the region. This parameter is left empty by default.
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	// Set command line parameters to specify the bucket name. This parameter is left empty by default.
	flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}

func main() {
	flag.Parse() // Parse command line parameters.

	// Check if the bucket name is specified. If not, the program outputs default parameters and terminates.
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required") // Log the error message and terminate the program.
	}

	// Check if the region information is specified. If not, the program outputs default parameters and terminates.
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required") // Log the error message and terminate the program.
	}

	// Create a configuration object, load the credential provider from environment variables and specify the region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	client := oss.NewClient(cfg) // Create a new OSS client using the configurations.

	// Create a PutBucketArchiveDirectRead request to enable real-time access for Archive objects in a specific bucket.
	request := &oss.PutBucketArchiveDirectReadRequest{
		Bucket: oss.Ptr(bucketName), // Specify the name of the bucket.
		ArchiveDirectReadConfiguration: &oss.ArchiveDirectReadConfiguration{
			Enabled: oss.Ptr(true), // Enable real-time access. If this parameter is set to true, real-time access is enabled. If it is set to false, this feature is disabled.
		},
	}
	result, err := client.PutBucketArchiveDirectRead(context.TODO(), request) // Send the request to configure real-time access.
	if err != nil {
		log.Fatalf("failed to put bucket archive direct read %v", err) // If an error occurs, record the error message and terminate the program.
	}

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

Consultar se o acesso em tempo real a objetos Archive está ativado

O código a seguir demonstra como consultar se o acesso em tempo real a objetos Archive está ativado.

package main

import (
	"context" 
	"flag"   
	"log"   

	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"           // Import the OSS SDK package.
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials" // Import the package for processing authentication information.
)

var (
	region     string // Define a variable to store the region information obtained from the command line.
	bucketName string // Define a variable to store the bucket name obtained from the command line.
)

// The init function is executed before the main function to initialize the program.
func init() {
	// Set command line parameters to specify the region. This parameter is left empty by default.
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	// Set command line parameters to specify the bucket name. This parameter is left empty by default.
	flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}

func main() {
	flag.Parse() // Parse command line parameters.

	// Check if the bucket name is specified. If not, the program outputs default parameters and terminates.
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required") // Log the error message and terminate the program.
	}

	// Check if the region information is specified. If not, the program outputs default parameters and terminates.
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required") // Log the error message and terminate the program.
	}

	// Create a configuration object, load the credential provider from environment variables and specify the region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	client := oss.NewClient(cfg) // Create a new OSS client using the configurations.

	// Create a GetBucketArchiveDirectRead request to query the real-time access configurations for Archive objects in a specific bucket (assuming real-time access is enabled and configured).
	request := &oss.GetBucketArchiveDirectReadRequest{
		Bucket: oss.Ptr(bucketName), // Specify the name of the bucket.
	}
	result, err := client.GetBucketArchiveDirectRead(context.TODO(), request) // Send the request to query the configurations (assuming real-time access is enabled and configured).
	if err != nil {
		log.Fatalf("failed to get bucket archive direct read %v", err) // If an error occurs, record the error message and terminate the program.
	}

	log.Printf("get bucket archive direct read result:%#v\n", result) // Display the configurations (assuming real-time access is enabled and configured).
}

Referências

  • Para obter o código de exemplo completo usado para ativar o acesso em tempo real a objetos Archive, consulte o exemplo no GitHub.

  • Para mais informações sobre a operação de API que ativa o acesso em tempo real a objetos Archive, consulte PutBucketArchiveDirectRead.

  • Para mais informações sobre a operação de API que consulta se o acesso em tempo real a objetos Archive está ativado, consulte GetBucketArchiveDirectRead.