我有一个运行的python脚本,每小时将图像上传到s3存储桶。 传入的图像分为三种类型,脚本会根据图像名称创建一个特定的文件夹,然后根据图像名称将图像上传到S3中的该文件夹。
现在正在发生的事情是,存储桶中每小时都会覆盖相同的图像,只有在不存在图像的情况下,我才需要上传图像 我该如何实现。 请帮忙
import os.path, shutil
import os, time
import socket
import boto3
from botocore.exceptions import NoCredentialsError
import glob
import json
from apscheduler.schedulers.blocking import BlockingScheduler
id = id_of_file
def my_schedule():
s3 = boto3.client('s3', aws_access_key_id="Access_key",
aws_secret_access_key="Secret_key")
folder_path = "path"
images = [f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))]
for image in images:
print(image)
folder_name = image.split('-')[0]
print(folder_name)
print("folder created**********************************")
key = "%s/%s" % (id+ '/' + folder_name, os.path.basename(image))
objs = list(bucket.objects.filter(Prefix=key))
print("Putting %s as %s" % (image, key))
final_file = folder_path + image
s3.upload_file(final_file, Bucket, key)
print("ALL Images uploaded successfully to s3 bucket")
time.sleep(5)
scheduler = BlockingScheduler()
scheduler.add_job(my_schedule, 'interval', hours=1)
scheduler.start()