storages.py
1.87 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
103
104
105
106
107
108
109
110
111
112
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
import boto3.session
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(file.name, 'wb')
for chunk in data.chunks():
f.write(chunk)
f.close()
nf = open(file.name, '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(file.name)
# putResponse = client.put_object(Bucket=AWS_STORAGE_BUCKET_NAME, Key= file_name)
def down(filename, bucketPath):
session = boto3.session.Session(region_name='us-west-2')
s3 = session.client('s3')
filepath = settings.LOCAL_DOWNLOAD_PATH + filename
response_headers = {
'response-content-type': 'application/force-download',
'response-content-disposition': 'attachment;filename="%s"' % filename
}
url = s3.generate_presigned_url('get_object', Params={'Bucket': AWS_STORAGE_BUCKET_NAME, 'Key': bucketPath},
ExpiresIn=100)
return url
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())