-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add management task for cleaning up stale S3 files
- Loading branch information
1 parent
95b30cb
commit 57469bb
Showing
7 changed files
with
53 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from django.core.management.base import BaseCommand | ||
import datetime | ||
from django.conf import settings | ||
from project_admin.models import S3Upload | ||
from datetime import timedelta | ||
import boto3 | ||
|
||
|
||
class Command(BaseCommand): | ||
help = 'Delete stale zip files' | ||
|
||
def handle(self, *args, **options): | ||
s3_client = boto3.client('s3', aws_access_key_id=settings.AWS_ACCESS_KEY_ID, | ||
aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY) | ||
old_files = S3Upload.objects.filter( | ||
created_at__date__lt=datetime.datetime.now() - timedelta(hours=24)) | ||
for of in old_files: | ||
delete_response = s3_client.delete_object( | ||
Bucket=settings.AWS_STORAGE_BUCKET_NAME, | ||
Key=of.key) | ||
if delete_response.get("ResponseMetadata").get('HTTPStatusCode') != 204: | ||
print("Couldn't delete file {}".format(of.key)) | ||
old_files.delete() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Generated by Django 2.0.7 on 2018-08-20 23:06 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('project_admin', '0012_auto_20180615_0240'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='S3Upload', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('key', models.CharField(max_length=60)), | ||
('created_at', models.DateTimeField(auto_now_add=True)), | ||
], | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters