将内存中的 HTML 保存到 S3 AWS Python Boto3 [英] Saving HTML in memory to S3 AWS Python Boto3

查看:36
本文介绍了将内存中的 HTML 保存到 S3 AWS Python Boto3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import boto3
from io import StringIO
s3 = boto3.client('s3')

display = # Altair Charting

buff = StringIO(display.save(str_obj, 'html'))

s3.upload_fileobj(buff.read(), BUCKET, 'index.html')

我认为我的完整示例会使问题复杂化,因此我将 Altair 图表注释掉了.

I think I full example would complicate the issue, so I left Altair charting commented out.

无论如何,我已尝试实施将对象和文件保存到 AWS S3 存储桶的大量建议,但我不明白.

Anyway, I've tried to implement the vast amount suggestions for saving objects and files to AWS S3 buckets, but I'm not understanding.

保存到磁盘就像:

display.save('index.html')

把它放到 S3 上似乎非常困难.我不确定我在这里缺少什么.也许 upload_fileobj 不是正确的方法,但我已经四处走动,试图使这项工作发挥作用.

Getting this onto S3 seems extremely difficult comparison. I'm not sure what I am missing here. Perhaps the upload_fileobj is not the correct method, but I've gone around and around trying to make this work.

这个方法的具体错误是ValueError: Fileobj must implement read

更新:

buff = StringIO(display.save('str.html'))

s3.put_object(
   Bucket=BUCKET, 
   Key=f'{DASHBOARD}{mkt_type}/{symbol}/index.html',
   Body=buff.read()
   )

结果在我的存储桶上的 0 字节文件 index.html

Results in the 0 Byte file index.html on my Bucket

更新 2:

str_obj = StringIO()
display.save(str_obj, 'html')
buff = str_obj.read()

s3.put_object(
    Bucket=BUCKET, 
    Key=f'{DASHBOARD}{mkt_type}/{symbol}/index.html',
    Body=buff
    )

这也不起作用.我简直不敢相信将文件保存到 S3 如此复杂.事后注意:我没有 buff

This also doesn't work. I just can't believe saving a file to S3 is this complicated. Hindsight note: I didn't have the getvalue() method required for buff

解决方案:这不是我第一次在处理 S3 文件时遇到问题,所以我可能会把它留作以后参考.也就是说,我仍然不清楚为什么我不能以字符串形式保存 '.html' 文件.

SOLUTION: This is not the first time I've struggled with S3 files, so I'm probably leaving this for my own future reference. That said, it is still not clear to me why I couldn't save the '.html' file in string form.

import boto3
from io import StringIO
s3 = boto3.client('s3')

display = # Altair Charting

str_obj = StringIO() # instantiate in-memory string object
display.save(str_obj, 'html') # saving to memory string object
buf = str_obj.getvalue().encode() # convert in-memory string to bytes

# Upload as bytes
s3.put_object(
    Bucket=BUCKET, 
    Key=f'{DASHBOARD}{mkt_type}/{symbol}/index.html', 
    Body=buf
    )

推荐答案

来自 boto3 文档:put_object

From boto3 docs: put_object

body=b'bytes'|file,

Body=b'bytes'|file,

这意味着 Body 应该是文件句柄或字节字符串.所以有(至少)两种可能的上传方式:

Which means that Body should be file handle or byte string. So there are (at least) 2 possible ways to upload:

通过将文件句柄传递给正文:

By passing file handle to Body:

with open('index.hml', 'rb') as f:
    s3.put_object(Bucket=BUCKET, Key=f'{DASHBOARD}{mkt_type}/{symbol}/index.html', Body=f)

通过将字节串传递给正文(假设 display.save() 返回字符串):

By passing bytestring to Body (supposing display.save() return string):

buf = display.save('str.html').encode() # converting str to bytes
s3.put_object(Bucket=BUCKET, Key=f'{DASHBOARD}{mkt_type}/{symbol}/index.html', Body=buf)

这篇关于将内存中的 HTML 保存到 S3 AWS Python Boto3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆