-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpytinyfy.py
46 lines (38 loc) · 2.01 KB
/
pytinyfy.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
import os
import boto3
import tinify
from dotenv import load_dotenv
load_dotenv()
# Initialize a session using DigitalOcean Spaces.
session = boto3.session.Session()
client = session.client('s3',
region_name=os.environ.get('REGION'),
endpoint_url=os.environ.get('ENDPOINT'),
aws_access_key_id=os.environ.get('ACCESS_KEY'),
aws_secret_access_key=os.environ.get('SECRET_KEY'))
# Set the name of your DigitalOcean Space and directory path.
bucket_name = os.environ.get('BUCKET_NAME')
directory_path = 'DIRECTORY_PATH'
# Use the list_objects_v2 method of the client to retrieve all objects in the specified directory.
objects = client.list_objects_v2(Bucket=bucket_name, Prefix=directory_path)
# Set your TinyPNG API key.
tinify.key = os.environ.get('TINYPNG_API_KEY')
# Loop through the objects and download any JPEG images.
for obj in objects['Contents']:
# Check if the object is a JPEG image.
if obj['Key'].lower().endswith('.jpg') or obj['Key'].lower().endswith('.jpeg'):
# Create the directories based on the object key's prefix.
prefix = os.path.dirname(obj['Key'])
os.makedirs(prefix, exist_ok=True)
# Download the object to a file with the same name as the object.
filename = os.path.basename(obj['Key'])
client.download_file(bucket_name, obj['Key'], os.path.join(prefix, filename))
print(f"Downloaded {filename} from {bucket_name}/{directory_path} to {prefix}")
# Compress the image using TinyPNG and overwrite the original file.
source = tinify.from_file(os.path.join(prefix, filename))
source.to_file(os.path.join(prefix, filename))
print(f"Compressed {filename} using TinyPNG")
# Upload the compressed image to the same path on the CDN.
cdn_path = obj['Key']
client.upload_file(os.path.join(prefix, filename), bucket_name, cdn_path, ExtraArgs={'ACL': 'public-read'})
print(f"Uploaded {filename} to CDN path {cdn_path}")