[英]django-storages upload to S3 closes server with no error
我正在使用Django 2.x和django-storages將媒體文件上傳到S3 Bucket 。
我的模型就像
class Media(models.Model):
user = models.ForeignKey(User, on_delete=models.SET_NULL, blank=True, null=True, default=None)
file = models.FileField(upload_to=get_media_upload_path)
Django 設置有
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
AWS_STORAGE_BUCKET_NAME = os.environ.get('S3_STORAGE', 'test-bucket')
AWS_DEFAULT_ACL = 'public-read'
並且環境變量設置為
AWS_ACCESS_KEY_ID=my-key
AWS_SECRET_ACCESS_KEY=my-secret
當我從郵遞員上傳文件時,它會關閉服務器而沒有任何錯誤。
我嘗試調試 DRF Serializer 的保存方法
def save(self, **kwargs):
log.info('Saving with kwargs: {}'.format(kwargs))
new = super().save(**kwargs)
log.info('Saved: {}'.format(new))
return new
它打印第一行,但在super().save()
行之后沒有輸出。
從設置文件中刪除django-storages
配置工作正常,並將文件上傳到本地目錄。
編輯 2:從 Django shell 上傳
使用以下命令從 Django shell 上傳
file = File(open('/path/to/file'))
m = Media(user=user, file=file)
m.save()
給出錯誤
File "/home/scanova/.virtualenvs/qcg-TqOLHEIu/lib/python3.7/site-packages/s3transfer/upload.py", line 86, in read
return self._fileobj.read(amount)
File "/home/scanova/.virtualenvs/qcg-TqOLHEIu/lib/python3.7/codecs.py", line 322, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc7 in position 10: invalid continuation byte
在我也為此苦苦掙扎之后,我終於發現添加boto3
調試日志非常有用。 在Django Shell 中,在執行m.save()
,請啟用boto3
日志:
import boto3
boto3.set_stream_logger('')
這將為您提供django-storages嘗試做的大量信息。 就我而言,它連接了靜態和媒體文件的存儲桶。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.