如何在Django中删除minIO存储中的文件?

6bc51xsx  于 2023-07-01  发布在  Go
关注(0)|答案(2)|浏览(344)

我在我的Django应用中使用了minIO存储。我想从DB和存储中删除对象。我该怎么做?先谢谢你。这是我的模型:

class FileStorage(models.Model):
    team = models.ForeignKey(Team, related_name = 'storage_team', null = True, on_delete = models.CASCADE)
    title = models.CharField(unique=True,max_length=255)
    video = models.FileField(verbose_name="Object Upload",
                            storage=MinioBackend(bucket_name=priv_bucket),
                            upload_to=iso_date_prefix)
                            
    def __str__(self):
        return self.title
vfh0ocws

vfh0ocws1#

你可以在python中创建一个minio客户端并调用remove_object函数。参考here

p3rjfoxz

p3rjfoxz2#

默认情况下,delete将从表中删除行,但不会删除对象。要从minio存储中删除对象,你需要覆盖django的默认delete方法。只需像这样编写delete方法:'''

class FileStorage(models.Model):
        team = models.ForeignKey(Team, related_name = 'storage_team', null = True, on_delete = models.CASCADE)
        title = models.CharField(unique=True,max_length=255)
        video = models.FileField(verbose_name="Object Upload",
                                storage=MinioBackend(bucket_name=priv_bucket),
                                upload_to=iso_date_prefix)   
    
        def delete(self, *args, **kwargs):
            self.video.delete()
            super(FileStorage, self).delete(*args, **kwargs)

相关问题