簡體   English   中英

Python configparser 無需下載即可從 S3 讀取配置

[英]Python configparser read config from S3 without downloading

有沒有辦法在不下載的情況下從 s3 讀取 .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 內容的二進制文件。 這是有效的。 它在更遠的地方破裂。

ConfigParserread方法接受一個文件名,但你傳遞給它的是一個文件對象。

您可以改為使用read_string方法,以便將StreamingBody對象的read方法返回的內容傳遞給它:

config.read_string(obj['Body'].read().decode())

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM