s3 の構造はちょっと特殊らしくディレクトリという概念が存在しないらしい つまり /hogehoge/aaa/ttt/ttt みたいなファイル名みたいな感じで入っている( “/” もファイル名の一部という考え) lambda
ランタイム は python 2.7<br> インライン<br>
コード
import boto3
import datetime
from dateutil.relativedelta import relativedelta
bucket_name = 'hogehoge'
path_name = '/tttt/taaaa/aaaa'
today = datetime.datetime.today()
now_year = today.strftime("%Y")
now_month = today.strftime("%m")
last_date = today - relativedelta(months=1)
last_year = last_date.strftime("%Y")
last_month = last_date.strftime("%m")
def lambda_handler(event, context):
s3_client = boto3.resource('s3')
response = s3_client.Bucket(bucket_name).objects.all()
for all_object in response:
if basis_path + now_year in all_object.key or basis_path + last_year in all_object.key:
if basis_path + now_year + "/" + now_month in all_object.key:
print (" keep ")
elif basis_path + last_year + "/" + last_month in all_object.key:
print ("keep ")
else:
print ("fail")
return


