【问题标题】:How to move files into another directory django-storage s3如何将文件移动到另一个目录 django-storage s3
【发布时间】:2021-10-23 04:00:44
【问题描述】:

我正在开发一个新闻博客,您可以在其中向新闻添加任意数量的文件。对于存储属性的文件,我使用的是 amazon s3 和 django-strorage。但是在我添加了新闻更新视图之后,我在文件管理方面遇到了一些问题。

如你所见,这里是我的文件模型

class FileStorage(models.Model):
    file        = models.FileField(upload_to=uploadFile)
    upload_path = models.TextField(blank=True, default='files/')

    def __str__(self):
        return f'Файл: {self.file.name.split("/")[-1]}'

主要问题是文件移到另一个目录后如何更新FileField?

这是我的文件移动脚本。

bucket = S3Boto3Storage()
from_path = bucket._normalize_name(bucket._clean_name(self.instance.file.name))
to_path = bucket._normalize_name(bucket._clean_name(self.cleaned_data['upload_path']))
    
result = bucket.connection.meta.client.copy_object(
    Bucket=bucket.bucket_name,
    CopySource=bucket.bucket_name + "/" + from_path,
    Key=to_path)
bucket.delete(from_path)

一切都很好,但只有路径。 FileField 中的文件存储旧路径。 我该如何更新它?

有问题的屏幕

【问题讨论】:

    标签: python django amazon-s3 django-storage


    【解决方案1】:

    如果你想要这个,只需像这样更改你的文件“名称”参数:

    file.name = "new file path"
    

    【讨论】:

      猜你喜欢
      • 2019-09-26
      • 1970-01-01
      • 2017-07-20
      • 1970-01-01
      • 2014-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-16
      相关资源
      最近更新 更多