Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a file in Amazon S3 using Django-storages

In my Django project I use Django-storageS to save media files in my Amazon S3.

I followed this tutorial (I use also Django-rest-framework). This works well for me: I can upload some images and I can see these on my S3 storage.

But, if I try to remove an instance of my model (that contains an ImageField) this not removes the corresponding file in S3. Is correct this? I need t remove also the resource in S3.

like image 287
Safari Avatar asked Mar 31 '15 16:03

Safari


People also ask

How do I delete files from Amazon S3?

If you no longer need to store the file you've uploaded to your Amazon S3 bucket, you can delete it. Within your S3 bucket, select the file that you want to delete, choose Actions, and then choose Delete. In the confirmation message, choose OK.

Can you delete an S3 bucket with objects in it?

If you are unable to delete an Amazon S3 bucket, consider the following: Make sure the bucket is empty – You can only delete buckets that don't have any objects in them.

How do I delete an S3 bucket containing many objects?

Sign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ . Navigate to the Amazon S3 bucket or folder that contains the objects that you want to delete. Select the check box to the left of the names of the objects that you want to delete.

How do I delete files from my S3 Boto?

You can delete the file from S3 bucket by using object. delete().


3 Answers

Deleting a record will not automatically delete the file in the S3 Bucket. In order to delete the S3 resource you need to call the following method on your file field:

model.filefield.delete(save=False)  # delete file in S3 storage

You can perform this either in

  1. The delete method of your model
  2. A pre_delete signal

Here is an example of how you can achieve this in the delete model method:

def delete(self):
    self.filefield.delete(save=False)
    super().delete()
like image 70
Lance Avatar answered Sep 21 '22 10:09

Lance


You can delete S3 files by offering its id (filename in the S3 storage) using following code:

import boto
from boto.s3.key import Key
from django.conf import settings

def s3_delete(id):
    s3conn = boto.connect_s3(settings.AWS_ACCESS_KEY,
            settings.AWS_SECRET_ACCESS_KEY)
    bucket = s3conn.get_bucket(settings.S3_BUCKET)

    k = Key(bucket)
    k.key = str(id)
    k.delete()

Make sure that you setup S3 variable correctly in settings.py including: AWS_ACCESS_KEY, AWS_SECRET_ACCESS_KEY, and S3_BUCKET.

like image 39
Heron Yang Avatar answered Sep 24 '22 10:09

Heron Yang


This works for me in aws s3,hope it helps

import os


@receiver(models.signals.post_delete, sender=YourModelName)
def auto_delete_file_on_delete(sender, instance, **kwargs):
    if instance.image:
        instance.image.delete(save=False)  ## use for aws s3
        # if os.path.isfile(instance.image.path): ## use this in development
        #     os.remove(instance.image.path)
like image 32
MbeforeL Avatar answered Sep 21 '22 10:09

MbeforeL