![](/img/trans.png)
[英]Django S3BotoStorage __init__ override error , “has no attribute 'rsplit'”
[英]Move Files with S3BotoStorage
我正在使用来自django- storages的S3BotoStorage在Django后端的S3存储桶上处理媒体和静态文件。
一切都按预期工作,但后端引发一个not ImplementedError,因为当我在Image是models.Image字段的地方调用self.image.path时,它不是本地文件系统。 这是预期的行为 。
但是,我使用的路径是使用os.rename移动图像(它也无法在我的存储桶中运行)。 将文件移到存储桶中的方法是什么?
与其使用os
来移动文件, os
使用存储
要移动文件,可以在FileField
(或在本例中为ImageField
)上使用read()
方法(并最终使用storage
删除旧文件)。
file_content = my_model.my_image.read()
file_name = my_model.my_image.name
# Renaming the file
my_model.my_image = my_model.my_image.save('new_file_name', ContentFile(file_content))
# If you want to delete the original file afterwards:
from django.core.files.storage import default_storage
default_storage.delete(file_name)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.