Block Public Access is an account-level security feature that prevents all OSS resources under your Alibaba Cloud account from being publicly accessible. This topic describes how to use the OSS SDK for Go V2 to enable, query, and delete the Block Public Access configuration.
How it works
When enabled, Block Public Access overrides all existing bucket ACLs, bucket policies, and object ACLs that grant public read or public read/write access. No bucket or object under your account can be accessed publicly, regardless of its individual access settings.
After you delete the configuration, bucket-level access policies take effect again. Buckets and objects with public ACLs or policies become publicly accessible.
Before you enable Block Public Access, verify that no applications depend on public access to your OSS resources. Enabling this feature blocks all public access and may disrupt services that rely on it.
Prerequisites
Before you begin, make sure that you have:
The OSS SDK for Go V2 installed (package path:
github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss)Access credentials configured as environment variables. For details, see Configure access credentials
RAM permissions for the operations listed below
Operation Required permission Enable Block Public Access oss:PutPublicAccessBlockQuery Block Public Access oss:GetPublicAccessBlockDelete Block Public Access oss:DeletePublicAccessBlock
Usage notes
These are account-level operations that affect all buckets under your account.
The sample code uses the region ID
cn-hangzhou(China (Hangzhou)) with a public endpoint. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For supported regions and endpoints, see OSS regions and endpoints.All examples load credentials from environment variables through
credentials.NewEnvironmentVariableCredentialsProvider(). For other credential methods, see Configure access credentials.
Sample code
All examples share the same client setup: parse the -region flag, load credentials from environment variables, and create an OSS client. The first example shows the complete program. Subsequent examples show only the operation-specific code -- replace the operation block in the first example to run them.
Error handling
The sample code uses log.Fatalf for simplicity. In production, check for specific error conditions:
| Scenario | Likely cause | Recommendation |
|---|---|---|
AccessDenied | Insufficient RAM permissions | Verify that your RAM user or role has the required oss:PutPublicAccessBlock, oss:GetPublicAccessBlock, or oss:DeletePublicAccessBlock permissions. |
Error on GetPublicAccessBlock | No configuration exists yet | Call PutPublicAccessBlock first, or handle this as a valid "not configured" state. |
| Network timeout | Connectivity issue | Implement retry logic with exponential backoff. |
References
GitHub example for complete sample code
PutPublicAccessBlock API reference
GetPublicAccessBlock API reference
DeletePublicAccessBlock API reference