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-hangzhouas 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.
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(®ion, "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(®ion, "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
For the complete sample code, see the GitHub example.
For the API used to enable Block Public Access, see PutBucketPublicAccessBlock.
For the API used to retrieve Block Public Access settings, see GetBucketPublicAccessBlock.
For the API used to delete Block Public Access settings, see DeleteBucketPublicAccessBlock.