-
Notifications
You must be signed in to change notification settings - Fork 2
/
services.py
34 lines (31 loc) · 1.01 KB
/
services.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
import string
import random
import boto3
from botocore.client import Config
from settings import AWS_ACCESS_KEY_ID, AWS_SECRET_KEY, BUCKET_NAME, AWS_REGION
def generate_random_string(stringLength=20):
letters = string.ascii_lowercase
return ''.join(random.choice(letters) for i in range(stringLength))
def upload_to_s3(image):
s3 = boto3.resource(
's3',
aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_KEY,
config=Config(signature_version='s3v4')
)
name = generate_random_string()
extension = image.content_type.split('/')[-1]
try:
s3.Bucket(BUCKET_NAME).put_object(Key=f"{name}.{extension}", Body=image)
get_url = f"https://{BUCKET_NAME}.s3.{AWS_REGION}.amazonaws.com/{name}.{extension}"
return_object = {
'status':'SUCCESS',
'get_url':get_url,
}
except Exception as e :
return_object = {
'status': e,
'get_url':'',
}
finally:
return return_object