-
Notifications
You must be signed in to change notification settings - Fork 250
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot use aws_sdk_s3::Client::head_bucket()
to get bucket region without already knowing bucket region
#1052
Comments
ah yeah, that's annoying. One option could be to grab the raw response from the error where you set the region and extract the header manually. Since that header isn't in the model currently, it doesn't get surfaced into the error message unfortunately. In parallel, I'll work on our side to see if they can model that field. Then the error can contain it directly, at least. |
This is still easiest to accomplish outside of the SDK. Here's an example that uses use reqwest::{Client, Url};
#[tokio::main]
async fn main() {
const BUCKET_NAME: &str = "zhessler-test-bucket";
let client = Client::new();
let url = Url::parse(format!("https://{}.s3.amazonaws.com", BUCKET_NAME).as_str()).unwrap();
let res = client.head(url).send().await.unwrap();
let bucket_region = res.headers().get("x-amz-bucket-region").unwrap().to_str().unwrap();
println!("Bucket region: {}", bucket_region);
} Which would print |
Comments on closed issues are hard for our team to see. |
Since you can access the response for errors, it's now possible to do this: #[tokio::test]
async fn test_head_bucket() {
// You need to set a region but it can be any region.
// It doesn't have to be the region of the bucket you're looking up.
let config = aws_config::from_env().region("us-east-1").load().await;
let s3 = aws_sdk_s3::Client::new(&config);
let res = s3.head_bucket()
.bucket("zhessler-test-bucket")
.send()
.await;
let bucket_region = match res {
Ok(res) => res.bucket_region().map(str::to_owned),
Err(err) => err.raw_response().and_then(|res| res.headers().get("x-amz-bucket-region")).map(str::to_owned),
}.expect("received a region no matter what");
println!("{bucket_region}");
} |
Describe the bug
Consider the following program:
Dependencies:
When I run this program, passing the name of an S3 general-purpose bucket (which happens to be in
us-east-2
) as the argument, it fails with:If I call
region()
on theConfigLoader
(after the call tono_credentials()
) but use a region that the bucket argument is not in, the program fails with:The only way I can get the program to run successfully is to call
region()
with the name of the region the bucket is in, yet the whole point of the code is to determine what that region is.In contrast, if I just do
curl -fIsSL https://BUCKET_NAME.amazonaws.com
on the command line, the bucket's region is given in thex-amz-bucket-region
header, so it clearly shouldn't be hard for the SDK to get this information without the user having to set a region.Expected Behavior
The program should have successfully retrieved & output the region of the given S3 bucket without having to specify a region in the code.
Current Behavior
See above.
Reproduction Steps
See above.
Possible Solution
No response
Additional Information/Context
No response
Version
Environment details (OS name and version, etc.)
macOS Sonoma 14.2.1, Intel, rustc 1.75
Logs
No response
The text was updated successfully, but these errors were encountered: