storages.py
1.48 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
from django.conf import settings
from storages.backends.s3boto3 import S3Boto3Storage
from .settings import AWS_STORAGE_BUCKET_NAME, LOCAL_DOWNLOAD_PATH
import boto3
import os
class MediaStorage(S3Boto3Storage):
location = settings.MEDIAFILES_LOCATION
def create_dir(dir_name):
client = boto3.client('s3')
client.put_object(Bucket= AWS_STORAGE_BUCKET_NAME, Key=dir_name+'/')
def upload_file(file, user, dir):
client = boto3.client('s3')
data = file
f = open('tempfile', 'wb')
for chunk in data.chunks():
f.write(chunk)
f.close()
nf = open('tempfile', 'rb')
# client.Bucket(AWS_STORAGE_BUCKET_NAME).put_object(Key=file_name, body= nf)
client.upload_fileobj(nf, AWS_STORAGE_BUCKET_NAME, user.username + '/' + dir + file.name)
os.remove('tempfile')
# putResponse = client.put_object(Bucket=AWS_STORAGE_BUCKET_NAME, Key= file_name)
def download_file(file_name, dir):
client = boto3.resource('s3')
client.Bucket(AWS_STORAGE_BUCKET_NAME).download_file(Key= dir, Filename=LOCAL_DOWNLOAD_PATH+file_name)
def delete_file(file_name, dir):
client = boto3.resource('s3')
client.Bucket(AWS_STORAGE_BUCKET_NAME).objects.filter(Prefix=dir).delete()
def print_dir(dir_name):
s3 = boto3.resource('s3')
mybucket = s3.Bucket('fileshell-test')
return(mybucket.objects.all())