GetBucketLocation 搭配 AWS SDK或 使用 CLI - AWS SDK 程式碼範例

文件範例儲存庫中有更多 AWS SDK可用的範例。 AWS SDK GitHub

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

GetBucketLocation 搭配 AWS SDK或 使用 CLI

下列程式碼範例示範如何使用 GetBucketLocation

CLI
AWS CLI

如果存在限制my-bucket,下列命令會擷取名為 之儲存貯體的位置限制條件:

aws s3api get-bucket-location --bucket my-bucket

輸出:

{ "LocationConstraint": "us-west-2" }
PowerShell
適用於 的工具 PowerShell

範例 1:如果存在限制,此命令會傳回儲存貯體 's3testbucket' 的位置限制。

Get-S3BucketLocation -BucketName 'amzn-s3-demo-bucket'

輸出:

Value ----- ap-south-1
  • 如需API詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet 參考 GetBucketLocation中的 。

Rust
SDK for Rust
注意

還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

async fn show_buckets( strict: bool, client: &Client, region: BucketLocationConstraint, ) -> Result<(), S3ExampleError> { let mut buckets = client.list_buckets().into_paginator().send(); let mut num_buckets = 0; let mut in_region = 0; while let Some(Ok(output)) = buckets.next().await { for bucket in output.buckets() { num_buckets += 1; if strict { let r = client .get_bucket_location() .bucket(bucket.name().unwrap_or_default()) .send() .await?; if r.location_constraint() == Some(&region) { println!("{}", bucket.name().unwrap_or_default()); in_region += 1; } } else { println!("{}", bucket.name().unwrap_or_default()); } } } println!(); if strict { println!( "Found {} buckets in the {} region out of a total of {} buckets.", in_region, region, num_buckets ); } else { println!("Found {} buckets in all regions.", num_buckets); } Ok(()) }
  • 如需API詳細資訊,請參閱 GetBucketLocation 中的 AWS SDK for Rust API參考