Python configparser 从 S3 读取配置而不下载
Python configparser read config from S3 without downloading
有没有办法在不下载的情况下从 s3 读取 .ini 配置文件?
我尝试过的:
config.ini:
[DEFAULT]
test = test1
test1 = test2
[ME]
me = you
you = he
代码:
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()))
它returns[].
我已尝试确保
obj['Body'].read()
很好地返回了一个内容为 config.ini 的二进制文件。这是工作。它在更远的地方中断了。
ConfigParser
的 read
方法需要一个文件名,但您传递给它的是一个文件对象。
您可以改用 read_string
方法,这样您就可以将 StreamingBody
对象的 read
方法返回的内容传递给它:
config.read_string(obj['Body'].read().decode())
有没有办法在不下载的情况下从 s3 读取 .ini 配置文件?
我尝试过的:
config.ini:
[DEFAULT]
test = test1
test1 = test2
[ME]
me = you
you = he
代码:
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()))
它returns[].
我已尝试确保
obj['Body'].read()
很好地返回了一个内容为 config.ini 的二进制文件。这是工作。它在更远的地方中断了。
ConfigParser
的 read
方法需要一个文件名,但您传递给它的是一个文件对象。
您可以改用 read_string
方法,这样您就可以将 StreamingBody
对象的 read
方法返回的内容传递给它:
config.read_string(obj['Body'].read().decode())