Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅
中国的 Amazon Web Services 服务入门
(PDF)。
将 CreateSession 和 Amazon SDK 搭配使用
以下代码示例演示了如何使用 CreateSession。
- Python
-
- 适用于 Python 的 SDK(Boto3)
-
class S3ExpressWrapper:
"""Encapsulates Amazon S3 Express One Zone actions using the client interface."""
def __init__(self, s3_client: Any) -> None:
"""
Initializes the S3ExpressWrapper with an S3 client.
:param s3_client: A Boto3 Amazon S3 client. This client provides low-level
access to AWS S3 services.
"""
self.s3_client = s3_client
@classmethod
def from_client(cls) -> "S3ExpressWrapper":
"""
Creates an S3ExpressWrapper instance with a default s3 client.
:return: An instance of S3ExpressWrapper initialized with the default S3 client.
"""
s3_client = boto3.client("s3")
return cls(s3_client)
def create_session(self, bucket_name: str) -> None:
"""
Creates an express session.
:param bucket_name: The name of the bucket.
"""
try:
self.s3_client.create_session(Bucket=bucket_name)
except ClientError as client_error:
logging.error(
"Couldn't create the express session for bucket %s. Here's why: %s",
bucket_name,
client_error.response["Error"]["Message"],
)
raise
有关 Amazon SDK 开发人员指南和代码示例的完整列表,请参阅 使用 Amazon SDK 通过 Amazon S3 进行开发。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。