-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbucket.py
36 lines (29 loc) · 1.13 KB
/
bucket.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import boto3
from django.conf import settings
import os
class Bucket:
def __init__(self):
session = boto3.session.Session()
self.conn = session.client(
service_name=settings.AWS_SERVICE_NAME,
aws_access_key_id=settings.AWS_S3_ACCESS_KEY_ID,
aws_secret_access_key=settings.AWS_S3_SECRET_ACCESS_KEY,
endpoint_url=settings.AWS_S3_ENDPOINT_URL
)
def get_objects(self):
result = self.conn.list_objects_v2(Bucket=settings.AWS_STORAGE_BUCKET_NAME)
if result['KeyCount']:
return result['Contents']
else:
return None
def delete_object(self, key):
self.conn.delete_object(Bucket=settings.AWS_STORAGE_BUCKET_NAME, Key=key)
return True
def download_object(self, key):
directory = settings.AWS_LOCAL_STORAGE + key
directory = "/".join(directory.split("/")[:-1])
os.makedirs(directory, exist_ok=True)
with open(settings.AWS_LOCAL_STORAGE + key, 'wb') as f:
self.conn.download_fileobj(settings.AWS_STORAGE_BUCKET_NAME, key, f)
return True
bucket = Bucket()