首页 > 解决方案 > 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

有谁知道是什么导致了错误?预先感谢您的帮助!

标签: pythonamazon-web-servicesamazon-s3aws-gluebytesio

解决方案


upload_fileobj接受文件对象,而不是字节数组。您需要bytes替换bytes.getvalue().


推荐阅读