Created
May 25, 2025 16:06
-
-
Save yaasita/d61716253d2ea91c7508e3a1ae9391d5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import hashlib | |
import binascii | |
import sys | |
# 設定 | |
if len(sys.argv) <= 1: | |
print("エラー: 最終etagを引数で指定してください", file=sys.stderr) | |
sys.exit(1) | |
PARTS_JSON = "parts.json" # parts.jsonファイル | |
S3_ETAG = sys.argv[1] # S3の最終ETag | |
def read_parts_json(): | |
"""parts.jsonからETagを取得""" | |
try: | |
with open(PARTS_JSON, "r") as f: | |
data = json.load(f) | |
return [(part["PartNumber"], part["ETag"].strip('"')) for part in data["Parts"]] | |
except FileNotFoundError: | |
print(f"{PARTS_JSON} が見つかりません。") | |
return [] | |
except json.JSONDecodeError: | |
print(f"{PARTS_JSON} の形式が無効です。") | |
return [] | |
def calculate_multipart_etag(etags, num_parts): | |
"""ETagリストからマルチパートETagを計算""" | |
concatenated_md5 = b"" | |
for _, etag in etags: | |
# ETagをバイナリ形式に変換 | |
md5_binary = binascii.unhexlify(etag) | |
concatenated_md5 += md5_binary | |
final_md5 = hashlib.md5(concatenated_md5).hexdigest() | |
return f"{final_md5}-{num_parts}" | |
def main(): | |
# parts.jsonからETagを取得 | |
parts = read_parts_json() | |
if not parts: | |
return | |
print("parts.json から取得したETag:") | |
for part_number, etag in parts: | |
print(f"パート{part_number}: ETag = {etag}") | |
# パート数がS3のETag(15)と一致するか確認 | |
expected_parts = int(S3_ETAG.split("-")[-1]) | |
if len(parts) != expected_parts: | |
print(f"エラー: パート数不一致。期待: {expected_parts}, 実際: {len(parts)}") | |
return | |
# マルチパートETagを再計算 | |
calculated_etag = calculate_multipart_etag(parts, len(parts)) | |
print(f"\n計算されたマルチパートETag: {calculated_etag}") | |
print(f"S3の最終ETag: {S3_ETAG}") | |
# 比較 | |
if calculated_etag == S3_ETAG: | |
print("最終ETag一致: parts.jsonのETagはS3の最終ETagと一致します。") | |
else: | |
print("最終ETag不一致: parts.jsonのETagに問題がある可能性があります。") | |
print("parts.jsonの内容またはパートの順序を確認してください。") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment