python - upload_fileobj 的问题 - ValueError:Fileobj 必须实现读取
问题描述
尝试将文件添加到 AWS S3 时,我在使用 upload_fileobj 的 Glue 中收到以下错误:
s3 = boto3.client('s3')
bytes = BytesIO()
zf = zipfile.ZipFile(bytes, "w")
for object_info in bucket.objects.filter(Prefix="stg/transfer/"):
if object_info.key[-1] == "/":
continue
print(object_info.key)
obj = bucket.Object(key=object_info.key)
o = obj.get()
data = o["Body"].read()
name = object_info.key.split('/')[2] + date_str + '.csv'
zf.writestr(name, data)
print(o)
zf.close()
bytes.seek(0)
s3.upload_fileobj(bytes.getvalue(), bucket , key)
s3.upload_fileobj(bytes.getvalue(), bucket , key) 文件“/home/spark/.local/lib/python3.7/site-packages/boto3/s3/inject.py”,第 525 行,在 upload_fileobj 中引发 ValueError ('Fileobj must implement read') ValueError: Fileobj must implement read
有谁知道是什么导致了错误?预先感谢您的帮助!
解决方案
upload_fileobj
接受文件对象,而不是字节数组。您需要bytes
替换bytes.getvalue()
.
推荐阅读
- r - 从一列/行值等于列名的单元格中提取值
- java - 休眠无法找到具有逻辑名称的列
- lync - UCMA 5.0 - 如果会议由 UCMA 应用程序创建,文件共享将不起作用
- android - ConstraintLayout 内容卡在屏幕中间
- css - Safari 和字体功能设置:“lnum”失败
- mysql - 排除相同的值和特定的规则集查询
- javascript - 为什么此代码不显示文本字段中的 expexres 值
- http - Web 浏览器如何支持 HLS?
- odoo-11 - 当我在 odoo 中发送销售订单时,如何替换外发电子邮件中的“Powered by Odoo”(页脚)?
- angular - 这个表达式中的 x 和箭头是什么意思 .subscribe(x=>x);