開始使用免費開始

多個 client

Sam 知道自己常常需要同時使用多個服務。她想練習在 boto3 中為兩個不同的服務各自建立一個 client。

在建立工作流程時,她會讓多個 Amazon Web Services 彼此互動,並由她電腦上的指令碼來執行。

她的 AWS key id 和 AWS secret 已分別儲存在 AWS_KEY_IDAWS_SECRET 中。

你將協助 Sam 初始化一個用於 S3 的 boto3 client,以及另一個用於 SNS 的 client。

她會使用 S3 client 來列出 S3 中的 bucket。 她會使用 SNS client 來列出她可以發佈的 topics(你會在第 3 章學到 SNS topics)。

本練習屬於課程

Python 中的 AWS Boto 入門

檢視課程

練習說明

  • 產生可與 S3 和 SNS 互動的 boto3 client。
  • 對兩個 client 的 region_name 都指定為 'us-east-1'
  • 使用 AWS_KEY_IDAWS_SECRET 設定認證。
  • 列出並列印 SNS topics。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Generate the boto3 client for interacting with S3 and SNS
s3 = boto3.____('____', region_name=____, 
                         aws_access_key_id=____, 
                         ____=____)

sns = boto3.____('____', region_name=____, 
                         aws_access_key_id=____, 
                         ____=____)

# List S3 buckets and SNS topics
buckets = s3.list_buckets()
topics = sns.list_topics()

# Print out the list of SNS topics
print(topics)
編輯並執行程式碼