我正在尝试下面编写的代码,但它正在抛出一个错误。我不确定我做错了什么。任何帮助表示赞赏
def upload_img(request):
maxsize = (1024, 1024)
im = Image.open(request.FILES['image'])
im.thumbnail(maxsize)
img_name = 'test.jpg'
s3 = boto3.client('s3')
s3.put_object(
Bucket=settings.AWS_S3_DEV_IMG_BUCKET,
Key=img_name,
Body=im.save(img_name, "JPEG"),
ContentType='image/jpeg',
)
错误:
'ParamValidationError: Parameter validation failed:
Invalid type for parameter Body, value: None, type: <type 'NoneType'>, valid types: <type 'str'>, <type 'bytearray'>, file-like object
答案 0 :(得分:2)
Image.save()
方法不会返回图像,saves it to disk (or buffer) and returns None
。这就是为什么boto抱怨NoneType
参数。
您希望将数据保存到某个临时缓冲区,然后将其传递给boto。例如:
buffer = io.BytesIO()
im.save(buffer, "JPEG")
buffer.seek(0) # rewind pointer back to start
s3.put_object(
Bucket=settings.AWS_S3_DEV_IMG_BUCKET,
Key=img_name,
Body=buffer,
ContentType='image/jpeg',
)