python - Python configparser 从 S3 读取配置而不下载
问题描述
有没有办法从 s3 读取 .ini 配置文件而不下载它?
我试过的:
配置.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()))
它返回 []。
我试图确保
obj['Body'].read()
正在返回一个包含 config.ini 内容的二进制文件。这是有效的。它在更远的地方断裂。
解决方案
的read
方法ConfigParser
接受一个文件名,但你传递给它一个文件对象。
您可以改为使用该read_string
方法,以便您可以将对象的read
方法返回的内容传递给它StreamingBody
:
config.read_string(obj['Body'].read().decode())
推荐阅读
- node.js - Gatsby 的环境变量“未找到 env.cmd”
- css - 如何在导航栏中与单词保持距离?
- html - 将mysql与带有事务查询的nodejs服务器一起使用的问题
- php - Wordpress:如何在自定义帖子类型中生成选择查询?
- c++ - 在另一台电脑上执行程序时加载 SDL2 共享库时出错
- python - pandas:出现次数最多的列
- python - 如何用python计算r平方?
- apache-spark - Pyspark datafame.limit() 和 drop_duplicates() 给出错误的输出
- c# - 连续读取串口缓冲区并将数据解析为C#中的数组
- delphi - 从 32 位或 64 位程序创建快捷方式时,快捷方式文件的文件哈希不同