so you could try something like this like mariogonzalez said. this will pull from one bucket and resize them locally, and upload them to another bucket, renaming it in the process. If you want to convert the images to a few dimensions, you can just expand the script to do so.
hth
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import boto3
import os
import subprocess
import time
session = boto3.session.Session()
####################################################################
client = session.client('s3',
region_name='sfo2',
endpoint_url='https://sfo2.digitaloceanspaces.com',
aws_access_key_id='32gfeGRITgtfrYOURKEYIDGTRHtrh45gdHG',
aws_secret_access_key='FwrTUCDbQG8/QMbYOUACCESSKEYRwZg3etrHtrjrJTRuREnl84')
bucket1 = ("sourcebucket") #the bucket holding original images
bucket2 = ("emptybucket") #an empty bucket you can send the converted images to
workdirectory = ("/home/username/pics/")
resizeDimension = ("512x512")
####################################################################
uploaded = []
filelist = []
failed = []
#os.mkdir(workdirectory)
def GetNames(s3, **base_kwargs): #gets the list of images in the bucket
continuation_token = None
while True:
list_kwargs = dict(MaxKeys=1000, **base_kwargs)
if continuation_token:
list_kwargs['Marker'] = continuation_token
response = client.list_objects(**list_kwargs)
yield from response.get('Contents', [])
time.sleep(1)
if not response.get('IsTruncated'): # At the end of the list?
break
else:
continuation_token = response.get('NextMarker')
def convert(filename):
try:
print("CV",filename)
subprocess.run('convert ' + workdirectory + filename+'[0]' +' -resize ' + resizeDimension +" "+ workdirectory + resizeDimension + filename, shell=True)
except Exception as e:
failed.append(filename)
def uploadImage(filename):
if os.path.exists(workdirectory+resizeDimension+filename):
try:
print("UPLOADING :",resizeDimension+filename)
client.upload_file(workdirectory+resizeDimension+filename, # Path to local file
bucket2, # Name of Space
resizeDimension+filename)
except Exception as e:
print(e)
s = str(e)
return False
return True
try:
for file in GetNames(client, Bucket=bucket1):
filelist.append(file['Key'])
print("DL",file['Key'])
try:
with open(workdirectory+file['Key'], 'wb') as f:
client.download_fileobj(bucket1, file['Key'], f)
convert(file['Key'])
except Exception as e:
s = str(e)
print(e)
except Exception as e:
s = str(e)
print(e)
for x in filelist:
try:
uploadImage(x)
except Exception as e:
s = str(e)
print(e)