0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

S3にMD5メタデータ付きjsonうp

Posted at
import boto3
import hashlib


def calculate_md5(file_path):
    """ファイルのMD5ハッシュを計算する"""
    hash_md5 = hashlib.md5()
    with open(file_path, "rb") as f:
        for chunk in iter(lambda: f.read(4096), b""):
            hash_md5.update(chunk)
    return hash_md5.hexdigest()


# AWSのクライアントを作成
s3 = boto3.client('s3')

# バケット名とファイルのパス
bucket_name = 'md5-test-bucket'
file_path = 'afterEscape.json'
file_key = 'md5Meta.json'

# MD5ハッシュ値を計算
md5_value = calculate_md5(file_path)

# ファイルをアップロードし、メタデータとしてMD5ハッシュ値を設定
s3.upload_file(
    Filename=file_path,
    Bucket=bucket_name,
    Key=file_key,
    ExtraArgs={
        "Metadata": {
            "md5": md5_value
        }
    }
)

print(f"File uploaded to S3 with MD5 hash {md5_value} as metadata.")

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?