From 3a7acef7aac4011b7115c5fd29f88bffc7c92ea9 Mon Sep 17 00:00:00 2001 From: mohemohe Date: Sat, 3 Feb 2024 23:36:23 +0900 Subject: [PATCH] =?UTF-8?q?Lambda=E3=81=A7=E5=AE=9F=E8=A1=8C=E3=81=99?= =?UTF-8?q?=E3=82=8B=E5=A0=B4=E5=90=88=E3=81=ABchainfile=E3=81=8C=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E3=81=95=E3=82=8C=E3=81=AA=E3=81=84=E5=95=8F=E9=A1=8C?= =?UTF-8?q?=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ファイルシステム上の更新日時ではなく、S3上の更新日時を使用する --- src/app.py | 7 +++++-- src/s3.py | 8 +++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/app.py b/src/app.py index 9cde5c3..be58867 100644 --- a/src/app.py +++ b/src/app.py @@ -27,10 +27,13 @@ def worker(): filename = "{}@{}.json".format(account_info["username"], domain).lower() filepath = os.path.join(base_dir, os.path.basename(filename.lower())) + now = datetime.datetime.now().timestamp() if is_lambda: - s3.get_file(filename, filepath) + _, last_modified = s3.get_file(filename, filepath) + else: + last_modified = os.path.getmtime(filepath) if os.path.isfile(filepath) else None - if (os.path.isfile(filepath) and datetime.datetime.now().timestamp() - os.path.getmtime(filepath) < 60 * 60 * 24): + if (os.path.isfile(filepath) and last_modified is not None and now - last_modified < 60 * 60 * 24): print("モデルは再生成されません") else: exportModel.generateAndExport(mastodonTool.loadMastodonAPI(domain, read_access_token, account_info['id'], params), filepath) diff --git a/src/s3.py b/src/s3.py index 3030fe5..3545adb 100644 --- a/src/s3.py +++ b/src/s3.py @@ -9,12 +9,14 @@ def get_file(key, file_path): try: - s3.Bucket(os.environ["S3_BUCKET"]).download_file(key, file_path) + bucketName = os.environ["S3_BUCKET"] + object_summery = s3.ObjectSummary(bucketName, key) + s3.Bucket(bucketName).download_file(key, file_path) print("File downloaded: {} to {}".format(key, file_path)) + return True, object_summery.last_modified.timestamp() except ClientError: print("File not found: {}".format(key)) - return False - return True + return False, None def put_file(key, file_path):