我正在尝试使用h5从AWS S3读取boto3文件。
client = boto3.client('s3',key ='key')
result = client.get_object(Bucket='bucket', Key='file')
with h5py.File(result['Body'], 'r') as f:
data = fTypeError:预期的str、字节或os.PathLike对象,而不是StreamingBody
有什么想法吗?
h5py版本为2.10,boto3版本为1.7.58
同样的问题是here,但没有答案.
发布于 2020-04-25 06:49:05
h5py.File()命令期望磁盘上的本地文件有一个路径。但是,您要将数据传递到内存中。
您可以通过以下方式下载文件:
import boto3
s3_client = boto3.client('s3')
s3_client.download_file('bucket', 'key', 'filename')
with h5py.File('filename', 'r') as f:
data = f发布于 2022-04-28 19:30:39
使用tempfile作为临时存储的工作解决方案。这会将模型数据从s3桶中流到临时存储中,并将其设置为变量。
import tempfile
from keras import models
import boto3
# Creating the low level functional client
client = boto3.client(
's3',
aws_access_key_id = 'ACCESS_KEY_ID',
aws_secret_access_key = 'ACCESS_SECRET_KEY',
region_name = 'us-east-1'
)
# Create the S3 object
response_data = client.get_object(
Bucket = 'bucket-name',
Key = 'model/model.h5'
)
model_name='model.h5'
response_data=response_data['Body']
response_data=response_data.read()
#save byte file to temp storage
with tempfile.TemporaryDirectory() as tempdir:
with open(f"{tempdir}/{model_name}", 'wb') as my_data_file:
my_data_file.write(response_data)
#load byte file from temp storage into variable
gotten_model=models.load_model(f"{tempdir}/{model_name}")
print(gotten_model.summary())https://stackoverflow.com/questions/61421528
复制相似问题