Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override S3 endpoint using Boto3 configuration file

OVERVIEW:

I'm trying to override certain variables in boto3 using the configuration file (~/aws/confg). In my use case I want to use fakes3 service and send S3 requests to the localhost.

EXAMPLE:

In boto (not boto3), I can create a config in ~/.boto similar to this one:

[s3] host = localhost calling_format = boto.s3.connection.OrdinaryCallingFormat  [Boto] is_secure = False 

And the client can successfully pick up desired changes and instead of sending traffic to the real S3 service, it will send it to the localhost.

>>> import boto >>> boto.connect_s3() S3Connection:localhost >>>  

WHAT I TRIED:

I'm trying to achieve a similar result using boto3 library. By looking at the source code I found that I can use ~/aws/config location. I've also found an example config in unittests folder of botocore.

I tried to modify the config to achieve the desired behaviour. But unfortunately, it doesn't work.

Here is the config:

[default] aws_access_key_id = XXXXXXXXX aws_secret_access_key = YYYYYYYYYYYYYY region = us-east-1 is_secure = False s3 =     host = localhost 

QUESTION:

  1. How to overwrite clients variables using config file?
  2. Where can I find a complete list of allowed variables for the configuration?
like image 891
Vor Avatar asked Sep 16 '15 20:09

Vor


People also ask

How do I connect my S3 to Boto3?

Setting up a client To access any AWS service with Boto3, we have to connect to it with a client. Here, we create an S3 client. We specify the region in which our data lives. We also have to pass the access key and the password, which we can generate in the AWS console, as described here.

Where is Boto default config file?

The default location for the boto configuration file is in the user home directory, ~/. boto, for Linux and macOS, and in %HOMEDRIVE%%HOMEPATH%, for Windows. You can get the location of the configuration file by running the command gsutil version -l .

What is Boto3 client (' S3 ')?

​Boto3 is the official AWS SDK for Python, used to create, configure, and manage AWS services. The following are examples of defining a resource/client in boto3 for the Weka S3 service, managing credentials, and pre-signed URLs, generating secure temporary tokens, and using those to run S3 API calls.


2 Answers

You cannot set host in config file, however you can override it from your code with boto3.

import boto3  session = boto3.session.Session()  s3_client = session.client(     service_name='s3',     aws_access_key_id='aaa',     aws_secret_access_key='bbb',     endpoint_url='http://localhost', ) 

Then you can interact as usual.

print(s3_client.list_buckets()) 
like image 168
Pandikunta Anand Reddy Avatar answered Sep 20 '22 10:09

Pandikunta Anand Reddy


boto3 only reads the signature version for s3 from that config file. You may want to open a feature request, but for now here is how you can address a custom endpoint:

import boto3 from botocore.utils import fix_s3_host resource = boto3.resource(service_name='s3', endpoint_url='http://localhost') resource.meta.client.meta.events.unregister('before-sign.s3', fix_s3_host) 

That bit about the meta is important because boto3 automatically changes the endpoint to your_bucket_name.s3.amazonaws.com when it sees fit 1. If you'll be working with both your own host and s3, you may wish to override the functionality rather than removing it altogether.

like image 40
Jordon Phillips Avatar answered Sep 22 '22 10:09

Jordon Phillips