All Products
Search
Document Center

Object Storage Service:Manage public access blocking configurations for buckets using OSS SDK for Go 2.0

Last Updated:Mar 20, 2026

Manage Block Public Access configurations for individual buckets using the OSS SDK for Go V2.

Prerequisites

Before you begin, ensure that you have:

  • An Alibaba Cloud account with an OSS bucket

  • The OSS SDK for Go V2 installed

  • Access credentials configured as environment variables. For setup instructions, see Configure access credentials

Usage notes

  • All examples use cn-hangzhou as the region. Replace it with the region where your bucket is located. For a full list of supported regions and endpoints, see OSS regions and endpoints.

  • By default, the SDK connects using the public endpoint. To access OSS from another Alibaba Cloud service in the same region, use the internal endpoint instead.

Enable Block Public Access for a bucket

Call PutBucketPublicAccessBlock to enable Block Public Access for a bucket.

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

var (
	region     string
	bucketName string
)

func init() {
	flag.StringVar(&region, "region", "", "The region where the bucket is located.")
	flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}

func main() {
	flag.Parse()

	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	// Load credentials from environment variables and specify the region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	client := oss.NewClient(cfg)

	request := &oss.PutBucketPublicAccessBlockRequest{
		Bucket: oss.Ptr(bucketName),
		PublicAccessBlockConfiguration: &oss.PublicAccessBlockConfiguration{
			BlockPublicAccess: oss.Ptr(true), // true blocks all public access; false removes the block.
		},
	}

	result, err := client.PutBucketPublicAccessBlock(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to put bucket public access block: %v", err)
	}

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

Get Block Public Access settings for a bucket

Call GetBucketPublicAccessBlock to retrieve the current Block Public Access configuration for a bucket.

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

var (
	region     string
	bucketName string
)

func init() {
	flag.StringVar(&region, "region", "", "The region where the bucket is located.")
	flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}

func main() {
	flag.Parse()

	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	// Load credentials from environment variables and specify the region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	client := oss.NewClient(cfg)

	request := &oss.GetBucketPublicAccessBlockRequest{
		Bucket: oss.Ptr(bucketName),
	}

	result, err := client.GetBucketPublicAccessBlock(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to get bucket public access block: %v", err)
	}

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

Delete Block Public Access settings for a bucket

Call DeleteBucketPublicAccessBlock to remove the Block Public Access configuration from a bucket.

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

var (
	region     string
	bucketName string
)

func init() {
	flag.StringVar(&region, "region", "", "The region where the bucket is located.")
	flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}

func main() {
	flag.Parse()

	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	// Load credentials from environment variables and specify the region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	client := oss.NewClient(cfg)

	request := &oss.DeleteBucketPublicAccessBlockRequest{
		Bucket: oss.Ptr(bucketName),
	}

	result, err := client.DeleteBucketPublicAccessBlock(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to delete bucket public access block: %v", err)
	}

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

References