This topic describes how to use Object Storage Service (OSS) SDK for Python 2.0 to manage Block Public Access for an access point.
Usage notes
The sample code in this topic uses the China (Hangzhou) region (
cn-hangzhou) as an example. By default, a public endpoint is used. If you want to access OSS using other Alibaba Cloud services in the same region, use an internal endpoint. For more information about the regions and endpoints supported by OSS, see Regions and endpoints.
Examples
Enable Block Public Access for an access point
The following sample code provides an example on how to enable Block Public Access for an access point:
import argparse
import alibabacloud_oss_v2 as oss
# Create a command line parameter parser and describe the purpose of the script. The example describes how to enable Block Public Access for an access point.
parser = argparse.ArgumentParser(description="put access point public access block sample")
# Specify the command line parameters, including the region, bucket name, access point name, and endpoint, and specify whether to enable Block Public Access. The endpoint parameter is optional.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
parser.add_argument('--access_point_name', help='The name of the access point.', required=True)
parser.add_argument('--block_public_access',
help='Specifies whether to enable Block Public Access. '
'true: enables Block Public Access. '
'false (default): disables Block Public Access.',
default='false')
def main():
# Parse the command line parameters to obtain the values specified by the user.
args = parser.parse_args()
# Obtain access credentials from environment variables for authentication.
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Use the default configurations of the SDK to create a configuration object and specify the credential provider.
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
# Specify the region attribute of the configuration object based on the command line parameters specified by the user.
cfg.region = args.region
# If a custom endpoint is provided, modify the endpoint parameter in the configuration object.
if args.endpoint is not None:
cfg.endpoint = args.endpoint
# Use the preceding configurations to initialize the OSSClient instance and allow the instance to interact with OSS.
client = oss.Client(cfg)
# Send a request to specify Block Public Access for an access point.
result = client.put_access_point_public_access_block(oss.PutAccessPointPublicAccessBlockRequest(
bucket=args.bucket, # The name of the bucket.
access_point_name=args.access_point_name, # The name of the access point.
public_access_block_configuration=oss.PublicAccessBlockConfiguration(
block_public_access=args.block_public_access == 'true', # Enable Block Public Access.
),
))
# Display the HTTP status code and request ID of the request to check the request status.
print(f'status code: {result.status_code},'
f' request id: {result.request_id},'
)
# Call the main function to start the processing logic when the script is directly run.
if __name__ == "__main__":
main() # Specify the entry points in the functions of the script. The control program flow starts here.Queries the Block Public Access configurations of an access point
The following sample code provides an example on how to query the Block Public Access configurations of an access point:
import argparse
import alibabacloud_oss_v2 as oss
# Create a command line parameter parser and describe the purpose of the script. The example describes how to query the Block Public Access configurations of an access point.
parser = argparse.ArgumentParser(description="get access point public access block sample")
# Specify the command line parameters, including the region, bucket name, access point name, and endpoint. The endpoint parameter is optional.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
parser.add_argument('--access_point_name', help='The name of the access point.', required=True)
def main():
# Parse the command line parameters to obtain the values specified by the user.
args = parser.parse_args()
# Obtain access credentials from environment variables for authentication.
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Use the default configurations of the SDK to create a configuration object and specify the credential provider.
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
# Specify the region attribute of the configuration object based on the command line parameters specified by the user.
cfg.region = args.region
# If a custom endpoint is provided, modify the endpoint parameter in the configuration object.
if args.endpoint is not None:
cfg.endpoint = args.endpoint
# Use the preceding configurations to initialize the OSSClient instance and allow the instance to interact with OSS.
client = oss.Client(cfg)
# Send a request to query the Block Public Access configurations of an access point.
result = client.get_access_point_public_access_block(oss.GetAccessPointPublicAccessBlockRequest(
bucket=args.bucket, # The name of the bucket.
access_point_name=args.access_point_name, # The name of the access point.
))
# Display the HTTP status code, request ID, and the Block Public Access status of the request to check the request status and the Block Public Access configurations.
print(f'status code: {result.status_code},'
f' request id: {result.request_id},'
f' block public access: {getattr(result.public_access_block_configuration, "block_public_access", "Not set")},'
)
# Call the main function to start the processing logic when the script is directly run.
if __name__ == "__main__":
main() # Specify the entry points in the functions of the script. The control program flow starts here.Delete the Block Public Access configurations of an access point
The following sample code provides an example on how to delete the Block Public Access configurations of an access point:
import argparse
import alibabacloud_oss_v2 as oss
# Create a command line parameter parser and describe the purpose of the script. The example describes how to query the Block Public Access configurations of an access point.
parser = argparse.ArgumentParser(description="delete access point public access block sample")
# Specify the command line parameters, including the region, bucket name, access point name, and endpoint. The endpoint parameter is optional.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
parser.add_argument('--access_point_name', help='The name of the access point.', required=True)
def main():
# Parse the command line parameters to obtain the values specified by the user.
args = parser.parse_args()
# Obtain access credentials from environment variables for authentication.
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Use the default configurations of the SDK to create a configuration object and specify the credential provider.
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
# Specify the region attribute of the configuration object based on the command line parameters specified by the user.
cfg.region = args.region
# If a custom endpoint is provided, modify the endpoint parameter in the configuration object.
if args.endpoint is not None:
cfg.endpoint = args.endpoint
# Use the preceding configurations to initialize the OSSClient instance and allow the instance to interact with OSS.
client = oss.Client(cfg)
# Send a request to delete the Block Public Access configurations of an access point.
result = client.delete_access_point_public_access_block(oss.DeleteAccessPointPublicAccessBlockRequest(
bucket=args.bucket, # The name of the bucket.
access_point_name=args.access_point_name, # The name of the access point.
))
# Display the HTTP status code and request ID of the request to check the request status.
print(f'status code: {result.status_code},'
f' request id: {result.request_id},'
)
# Call the main function to start the processing logic when the script is directly run.
if __name__ == "__main__":
main() # Specify the entry points in the functions of the script. The control program flow starts here.