删除 Django-Storages S3 中的默认文件名
我将 django-storages 与 amazon S3 一起使用,并使用以下方式上传图像文件:
models.ImageField(upload_to="img=%Y-%m-%d", max_length=256, blank=True, null=True)
但是,当文件上传到 S3 时,它的末尾会附加原始文件名。我如何摆脱它并替换它,用一些随机散列代替?
I'm using django-storages with amazon S3, and uploading image files with:
models.ImageField(upload_to="img=%Y-%m-%d", max_length=256, blank=True, null=True)
When the files are uploaded to S3 however, it has the original file name attached at the end. How do I get rid of that and replace it, with say some random hash instead?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
提供可调用而不是字符串
upload_to
。可调用对象将传递正在保存的实例和文件名,并且必须返回完整路径,包括文件名——因此您可以选择不使用原始文件名。 (但是,您必须使用datetime.date.today()
自己调用strftime
)。Supply a callable instead of a string to
upload_to
. The callable will be passed the instance being saved, and the filename, and will have to return the full path, including the filename -- so you can choose not to use the original filename. (You'll have to callstrftime
yourself withdatetime.date.today()
, however).