Tüm Ürünler
Search
Belge Merkezi

:OSS SDK for Go 2.0 kullanarak paketler için genel erişim engelleme yapılandırmalarını yönetin

Son Güncelleme:Oct 01, 2026

Bu konuda, OSS SDK for Go V2 kullanarak paketler için genel erişim engelleme yapılandırmalarının nasıl yönetileceği açıklanır.

Notlar

  • Bu konudaki örnek kod, Çin (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. Paketteki kaynaklara, paketle aynı bölgede bulunan diğer Alibaba Cloud hizmetlerini kullanarak 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).

Örnek kod

Bir paket için genel erişim engellemeyi etkinleştirin

Aşağıdaki kodda, bir paket için genel erişim engellemeyi etkinleştirme örneği gösterilmektedir.

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 the command line parameter to specify the region. This parameter is left empty by default.
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	// Set the command line parameter 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 PutBucketPublicAccessBlock request to configure block public access for a bucket.
	request := &oss.PutBucketPublicAccessBlockRequest{
		Bucket: oss.Ptr(bucketName), // Specify the name of the bucket.
		PublicAccessBlockConfiguration: &oss.PublicAccessBlockConfiguration{
			oss.Ptr(true), // Enable block public access.
		},
	}
	putResult, err := client.PutBucketPublicAccessBlock(context.TODO(), request) // Process the request.
	if err != nil {
		log.Fatalf("failed to put bucket public access block %v", err) // If an error occurs, record the error message and terminate the program.
	}

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

Bir paket için genel erişim engelleme yapılandırmalarını sorgulayın

Aşağıdaki kodda, bir paket için genel erişim engelleme yapılandırmalarını sorgulama örneği gösterilmektedir.

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 instance using the configurations.

	// Create a GetBucketPublicAccessBlock request to query block public access configurations.
	request := &oss.GetBucketPublicAccessBlockRequest{
		Bucket: oss.Ptr(bucketName), // Specify the name of the bucket.
	}
	getResult, err := client.GetBucketPublicAccessBlock(context.TODO(), request) // Process the request.
	if err != nil {
		log.Fatalf("failed to get bucket public access block %v", err) // If an error occurs, record the error message and terminate the program.
	}

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

Bir paket için genel erişim engelleme yapılandırmalarını silin

Aşağıdaki kodda, bir paket için genel erişim engelleme yapılandırmalarını silme örneği gösterilmektedir.

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 instance using the configurations.

	// Create a DeleteBucketPublicAccessBlock request to delete the block public access configurations.
	request := &oss.DeleteBucketPublicAccessBlockRequest{
		Bucket: oss.Ptr(bucketName), // Specify the name of the bucket.
	}
	result, err := client.DeleteBucketPublicAccessBlock(context.TODO(), request) // Process the request.
	if err != nil {
		log.Fatalf("failed to delete bucket public access block %v", err) // If an error occurs, record the error message and terminate the program.
	}

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

İlgili Başlıklar

  • Genel erişim engelleme yapılandırmalarını yönetmek için kullanılan tam örnek kodu görmek için bkz. Github örneği.

  • Bir paket için genel erişim engellemeyi etkinleştirmek üzere çağırabileceğiniz API işlemi hakkında daha fazla bilgi için bkz. PutBucketPublicAccessBlock.

  • Bir paket için genel erişim engelleme yapılandırmalarını sorgulamak üzere çağırabileceğiniz API işlemi hakkında daha fazla bilgi için bkz. GetBucketPublicAccessBlock.

  • Bir paket için genel erişim engelleme yapılandırmalarını silmek üzere çağırabileceğiniz API işlemi hakkında daha fazla bilgi için bkz. DeleteBucketPublicAccessBlock.