Search code examples
djangoamazon-web-servicesamazon-s3python-django-storages

Image files were deleted by itself in AWS-S3


I'm using python Django framework for server and AWS-S3 for store uploaded image. Also, i'm using django-storages library for handle S3. But sometimes images were deleted by itself not through django server. Image urls were still exist in DB. So i changed create, put, delete, get action to get in bucket policy But still deleted by itself.

django-storages setting in settings.py is this

AWS_SECRET_ACCESS_KEY = S3['secret_key']
AWS_REGION = 'ap-northeast-2'
AWS_STORAGE_BUCKET_NAME = 'loopusimage'
AWS_S3_CUSTOM_DOMAIN = '%s.s3.%s.amazonaws.com' % (
    AWS_STORAGE_BUCKET_NAME, AWS_REGION)
AWS_DEFAULT_ACL = None
DATA_UPLOAD_MAX_MEMORY_SIZE = 1024000000
FILE_UPLOAD_MAX_MEMORY_SIZE = 1024000000
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage

and changed bucket policy is this

    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "Statement1",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::loopusimage/*"
        }
    ]
}

What should i do to solve my problem?


Solution

  • AWS itself will never delete items in the S3 but there are some scenarios that can be applicable to your problem.

    1. You have set a lifecycle for the items in your bucket. You can check AWS doc to see how to disable/check that.
    2. The second problem you might have is the way you are storing the images in your models. You might have set the names in the wrong way which leads images to override each other in S3. I strongly recommend checking this doc to specify where to save the files.

    To find your exact problem, you can use AWS CloudTrail. This service will help you to see who(or what) has deleted these images from your bucket. You can check this doc for instruction.