Tüm Ürünler
Search
Belge Merkezi

:OSS SDK for Go 2.0 kullanarak OSS kaynakları için Genel Erişimi Engelleme özelliğini yönetin

Son Güncelleme:Oct 01, 2026

Object Storage Service (OSS) kaynakları için Genel Erişimi Engelleme özelliğini etkinleştirmek, ilgili yapılandırmaları sorgulamak veya silmek amacıyla OSS SDK for Go V2'yi kullanabilirsiniz.

Kullanım notları

  • Örnek kod, varsayılan olarak Çin (Hangzhou) bölgesinin bölge kimliği olan cn-hangzhou değerini ve genel uç noktayı kullanır. Paket ile aynı bölgedeki diğer Alibaba Cloud hizmetlerine erişim sağlamak için dahili uç nokta 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ı.

  • 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

Genel Erişimi Engelleme özelliğini etkinleştirin

Aşağıdaki kod, OSS kaynakları için Genel Erişimi Engelleme özelliğinin nasıl etkinleştirileceğini gösterir.

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 string variable to store the region information obtained from the command line.
)

// The init function is executed before the main function to initialize the program.
func init() {
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.") // Specify the region variable by parsing command-line arguments. By default, this parameter is left empty.
}

// The main function serves as the entry point for the program.
func main() {
	flag.Parse() // Parse command line parameters.
	if len(region) == 0 { // If the region parameter is not specified, the program prints an error message indicating that the region parameter is required and terminates.
		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 PutPublicAccessBlock request to enable Block Public Access.
	request := &oss.PutPublicAccessBlockRequest{
		PublicAccessBlockConfiguration: &oss.PublicAccessBlockConfiguration{
			oss.Ptr(true), // Set this parameter to true to enable Block Public Access.
		},
	}
	putResult, err := client.PutPublicAccessBlock(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to put public access block %v", err) // If an error occurs, record the error message and exit.
	}

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

Genel Erişimi Engelleme yapılandırmalarını sorgulayın

Aşağıdaki kod, Genel Erişimi Engelleme yapılandırmalarının nasıl sorgulanacağını gösterir.

package main

import (
	"context" // Used to manage context with features such as deadlines and cancellation signals.
	"flag"    // Used to parse command line parameters.
	"log"     // Used to print log information.

	"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 string variable to store the region information obtained from the command line.
)

// The init function used to set up tasks that are required to be completed before the program starts.
func init() {
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.") // Set command line parameters to specify the region, default is an empty string
}

// Entry point of the program.
func main() {
	flag.Parse() // Parse command line parameters.
	if len(region) == 0 { // If the region parameter is not provided, the program prints an error message indicating that the region parameter is required and terminates.
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required") // Log the error message and terminate the program.
	}

	// Load the default configuration, 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 GetPublicAccessBlock request to query the Block Public Access configurations.
	request := &oss.GetPublicAccessBlockRequest{}
	getResult, err := client.GetPublicAccessBlock(context.TODO(), request) // Execute the request.
	if err != nil {
		log.Fatalf("failed to get public access block %v", err) // If an error occurs, record the error message and exit.
	}

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

Genel Erişimi Engelleme yapılandırmalarını silin

Aşağıdaki kod, Genel Erişimi Engelleme yapılandırmalarının nasıl silineceğini gösterir.

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 string variable to store the region information obtained from the command line.
)

// The init function is executed before the main function to initialize the program.
func init() {
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.") // Set command line parameters to specify the region, default is an empty string
}

// The main function serves as the entry point for the program.
func main() {
	flag.Parse() // Parse command line parameters.
	if len(region) == 0 { // If the region parameter is not provided, the program prints an error message indicating that the region parameter is required and terminates.
		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 configuration.

	// Create a DeletePublicAccessBlock request to delete the Block Public Access configurations.
	request := &oss.DeletePublicAccessBlockRequest{}
	result, err := client.DeletePublicAccessBlock(context.TODO(), request) // Send the request.
	if err != nil {
		log.Fatalf("failed to delete public access block %v", err) // If an error occurs, record the error message and exit.
	}

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

Başvurular