How to use Multi-Region Access Point on BOTO3

Viewed 582

I'm looking to use the multi-region access point on my project. But there is no information about it in boto3. Can someone try to help me please ?

2 Answers

Boto3 S3Control describes how to create multi-region access points.

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3control.html#S3Control.Client.create_multi_region_access_point

Adding more context for using them in boto3 code


BUCKET_NAME = 'my-bucket-name-rqbb7rhaqeqn6rgxka4xk1udcf3wquse1b-s3alias' # replace with your bucket name
KEY = 'my_s3_object_name.jpg' # replace with your object key
profile = "my_profile"
region = "us-east-1"

session = boto3.session.Session(profile_name=profile, region_name=region)
s3 = session.resource('s3')
s3.Bucket(BUCKET_NAME).download_file(KEY, 'my_local_image.jpg')

Based on @Aceliweth's comment, the following will obtain a presigned url for an S3 bucket through a multiregion access point assuming there's a test_file.txt in the bucket(s):

        import boto3
        from botocore.client import Config
        import requests

        s3_client = boto3.client("s3", config=Config(signature_version="s3v4"))
        mrap_arn = "arn:aws:s3::{ACCOUNT_ID}:accesspoint/{MRAP_ALIAS}"
        url = s3_client.generate_presigned_url(
            "get_object",
            Params={"Bucket": mrap_arn, "Key": "test_file.txt"},
            ExpiresIn=int(60 * 60 * 24),  # 24 hours
        )

        r = requests.get(url)

HTTP requests via the Internet to the multiregion access point need to be signed via Signature Version 4.

boto3 (aws-cli as well) will need to be up-to-date. boto3 only supports Multiregion Access Points version [1.18.35] (https://github.com/boto/boto3/blob/develop/CHANGELOG.rst#11835) onwards.

Otherwise, you may experience issues like:

Invalid bucket name "arn:aws:s3::{ACCOUNT_ID}:accesspoint/{MRAPS_ALIAS}": Bucket name must match the regex
Related