如何仅使用在 django 模型中过滤的几个模型将文件上传到 AWS?

How upload files to AWS only with a few models filtered in django models?

我的 Django 应用程序的 models.py 中有这段代码。我得到了默认文件存储,将我的文件保存在远程服务器上。但是它将所有文件 objects/models 存储到远程服务器。是否有任何选项可以仅在我想要的模型中设置在远程服务器 AWS 上上传?

class Attachment(models.Model):
    file = models.FileField(upload_to=log_att_path)
    log_sender = models.ForeignKey(
        LogSender,
        related_name='attachments',
        on_delete=models.CASCADE
    )
    timestamp = models.DateTimeField(auto_now_add=True)
    attachment_url = models.TextField(default=False)
```

github 上有一个非常好的和流行的包 django-storages。您可以使用此包将您上传的文件上传到 aws。您可以使用此包中的 S3Boto3Storage 来处理您的文件上传。

Is there any option to set the upload on the remote server AWS only in the models that I want?

如果您只想将 AWS 用于特定 models/fields,则可以指定要在文件字段中使用的存储 class。

from storages.backends.s3boto3 import S3Boto3Storage

class Attachment(models.Model):
    file = models.FileField(upload_to=log_att_path, storage=S3Boto3Storage)
    # other stuff