簡體   English   中英

django-storages 上傳到 S3 關閉服務器,沒有錯誤

[英]django-storages upload to S3 closes server with no error

我正在使用Django 2.xdjango-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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM