Reputation: 1334
I instantiate a boto3 S3 resource
like this (code simplified):
import os
from pathlib import Path
import boto3
s3_resource = boto3.resource(
"s3",
aws_access_key_id=os.getenv("AWS_ACCESS_KEY_ID"),
aws_secret_access_key=os.getenv("AWS_SECRET_ACCESS_KEY"),
# NOTE: My config is:
# [default]
# region = eu-west-1
region_name=os.getenv("region_name"),
)
How can I get the region to which the S3 resource is associated? Is there an attribute of the class / instance I can query?
I checked the boto3 docs for the S3 Service Resource object but couldn't find such an attribute. I also checked this related question but it relates to the client
not resource
object from boto3.
Context: I want to check the region so that I can ensure that my ~/.aws/config is being read to supply the region, since checking what os.getenv("region_name")
returns None
since I have no such environmental variable. If you can answer this question too, it's a bonus. As cited in the code, my config points to the eu-west-1 region.
I'm new to using S3 programmatically inside Python (I've mostly used the AWS CLI) so please suggest edits to the question if necessary.
Upvotes: 1
Views: 3350
Reputation: 7546
Well, every boto3 resource has a client in meta, so you can do this:
s3_resource.meta.client._client_config.region_name
Upvotes: 4
Reputation: 11337
You can check for the region via AWS Console (UI). Goto: S3 --> --> Properties tab, and you'll see the "AWS Region" of your bucket:
EDIT: following your comment, maybe you're looking for get_bucket_location:
Returns the Region the bucket resides in.
Upvotes: -1