Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python configparser read config from S3 without downloading

is there a way to read a .ini configuration file from s3 without downloading it?

What I have tried:

config.ini:

[DEFAULT]
test = test1
test1 = test2

[ME]
me = you
you = he

code:

import boto3
import io
import configparser

s3_boto = boto3.client('s3')
configuration_file_bucket = "mybucket"
configuration_file_key = "config.ini"
obj = s3_boto.get_object(Bucket=configuration_file_bucket, Key=configuration_file_key)

config = configparser.ConfigParser()
config.read(io.BytesIO(obj['Body'].read()))

It returns [].

I have tried to make sure that

obj['Body'].read()

is returning well a binary with content of config.ini. This is working. It breaks somewhere further.

like image 748
Arhiliuc Cristina Avatar asked Aug 12 '26 06:08

Arhiliuc Cristina


1 Answers

The read method of ConfigParser takes a file name, and yet you're passing it a file object.

You can instead use the read_string method so that you can pass to it the content returned by the read method of the StreamingBody object:

config.read_string(obj['Body'].read().decode())
like image 158
blhsing Avatar answered Aug 14 '26 21:08

blhsing



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!