以下是一个示例代码,演示如何使用Amazon S3传输管理器进行目录上传,并计算哈希值。
import os
import hashlib
import boto3
from botocore.exceptions import NoCredentialsError
def upload_directory_to_s3(local_directory, bucket, s3_folder):
s3 = boto3.client('s3')
for root, dirs, files in os.walk(local_directory):
for file in files:
local_path = os.path.join(root, file)
relative_path = os.path.relpath(local_path, local_directory)
s3_path = os.path.join(s3_folder, relative_path).replace("\\", "/")
try:
s3.upload_file(local_path, bucket, s3_path)
print("Successfully uploaded file to S3: " + s3_path)
except FileNotFoundError:
print("The file was not found")
except NoCredentialsError:
print("Credentials not available")
def calculate_hash(file_path):
with open(file_path, 'rb') as f:
hasher = hashlib.sha256()
buf = f.read()
while len(buf) > 0:
hasher.update(buf)
buf = f.read()
return hasher.hexdigest()
# 设置本地目录,S3存储桶和S3文件夹路径
local_directory = "/path/to/local/directory"
bucket = "your-s3-bucket"
s3_folder = "your-s3-folder"
# 上传目录到S3
upload_directory_to_s3(local_directory, bucket, s3_folder)
# 计算目录中每个文件的哈希值
for root, dirs, files in os.walk(local_directory):
for file in files:
local_path = os.path.join(root, file)
relative_path = os.path.relpath(local_path, local_directory)
s3_path = os.path.join(s3_folder, relative_path).replace("\\", "/")
s3 = boto3.resource('s3')
obj = s3.Object(bucket, s3_path)
print("File: " + s3_path + ", Hash: " + calculate_hash(local_path))
请确保安装了必要的Python库,如boto3和botocore。 另外,还需要配置好AWS凭证,以便进行上传操作。