【发布时间】:2020-02-07 05:17:55
【问题描述】:
我正在将图像上传到内存,然后我想将其保存到 imageField()。
我使用 django-storages 将文件从 image/fileField() 上传到 AWS S3。
我将图像下载到内存中:
r = requests.get(url, headers=header)
image = Image.open(BytesIO(r.content))
image_in_memory = InMemoryUploadedFile(image, None, "foo.png", 'image/png', image.size, None)
image_in_memory_file = image_in_memory.file
一些验证:
print(type(image_in_memory))
---
<class 'django.core.files.uploadedfile.InMemoryUploadedFile'>
当我保存这个时:
my_object.cover.save(name="foo.png", content=image_in_memory)
Django 产生这个错误:
web_1 | link.cover.save(name="foo.png", content=image_in_memory)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/db/models/fields/files.py", line 87, in save
web_1 | self.name = self.storage.save(name, content, max_length=self.field.max_length)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/core/files/storage.py", line 52, in save
web_1 | return self._save(name, content)
web_1 | File "/usr/local/lib/python3.7/site-packages/storages/backends/s3boto3.py", line 491, in _save
web_1 | self._save_content(obj, content, parameters=parameters)
web_1 | File "/usr/local/lib/python3.7/site-packages/storages/backends/s3boto3.py", line 505, in _save_content
web_1 | content.seek(0, os.SEEK_SET)
web_1 | TypeError: seek() takes 2 positional arguments but 3 were given
你发现问题了吗?
【问题讨论】:
-
也许你应该使用
content=image_in_memory_file? -
我也试过了,同样的错误:
seek() takes 2 positional arguments but 3 were given
标签: python django amazon-s3 django-storage