forked from thumbor/thumbor-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixtures.py
77 lines (65 loc) · 2.39 KB
/
fixtures.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
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# thumbor aws extensions
# https://github.com/thumbor/thumbor-aws
# Licensed under the MIT license:
# http://www.opensource.org/licenses/mit-license
# Copyright (c) 2021 Bernardo Heynemann [email protected]
import asyncio
from os import listdir
from os.path import abspath, dirname, isfile, join
from aiobotocore.session import get_session
from thumbor.utils import logger
ROOT_PATH = dirname(__file__)
async def upload():
"""Uploads file to S3"""
images_path = abspath(join(ROOT_PATH, "tests", "fixtures"))
all_images = [
f for f in listdir(images_path) if isfile(join(images_path, f))
]
session = get_session()
async with session.create_client(
"s3",
region_name="local",
endpoint_url="https://localhost:4566",
) as client:
# Ensure Bucket is there
try:
location = {"LocationConstraint": "local"}
await client.create_bucket(
Bucket="fixtures",
CreateBucketConfiguration=location,
)
except client.exceptions.BucketAlreadyOwnedByYou:
pass
# Then upload all images
for image_path in all_images:
with open(join(images_path, image_path), "rb") as image:
content = image.read()
response = None
try:
response = await client.put_object(
Bucket="fixtures",
Key=image_path,
Body=content,
ContentType="image/jpeg",
ACL="public-read",
)
except Exception as err:
msg = (
"Unable to upload image to "
f"{image_path}: {err} ({type(err)})"
)
logger.error(msg)
raise RuntimeError(msg) from err
status_code = response["ResponseMetadata"]["HTTPStatusCode"]
if status_code != 200:
msg = (
"Unable to upload image to "
f"{image_path}: Status Code {status_code}"
)
logger.error(msg)
raise RuntimeError(msg)
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(upload())