在 PHP 中使用 md5 验证 S3 上传是否成功
Verify S3 upload succesfull with md5 in PHP
我正在将文件上传到 S3,没有出现如下错误:
$s3 = new Aws\S3\S3Client([
"version" => "latest",
"region" => $region
]);
$result = $s3->putObject([
"Bucket" => $bucket,
"Key" => $key,
"SourceFile" => $image,
"ACL" => "public-read"
]);
我读过 Etags 和 ContentMD5 headers,但我似乎无法将它们全部放在一起 PHP。
如何知道文件内容是否正确?
将您的文件 md5 添加到元数据中,例如:
$result = $s3->putObject([
"Bucket" => $bucket,
"Key" => $key,
"image" => $image,
"ACL" => "public-read",
"Metadata" => [
"Content-MD5" => base64_encode($fileMd5)
]
]);
见http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html#object-metadata
要获取 PHP 中文件的 md5,您可以使用 md5_file 函数
使用 ContentSHA256 密钥使其正常工作:
$sha256 = hash_file("sha256", $image);
$result = $s3->putObject([
"Bucket" => getenv("S3_BUCKET_NAME"),
"Key" => $key,
"SourceFile" => $image,
"ACL" => "public-read",
"ContentSHA256" => $sha256
]);
如果 SHA 不匹配,我会得到一个 S3 异常,这正是我想要的。
我正在将文件上传到 S3,没有出现如下错误:
$s3 = new Aws\S3\S3Client([
"version" => "latest",
"region" => $region
]);
$result = $s3->putObject([
"Bucket" => $bucket,
"Key" => $key,
"SourceFile" => $image,
"ACL" => "public-read"
]);
我读过 Etags 和 ContentMD5 headers,但我似乎无法将它们全部放在一起 PHP。
如何知道文件内容是否正确?
将您的文件 md5 添加到元数据中,例如:
$result = $s3->putObject([
"Bucket" => $bucket,
"Key" => $key,
"image" => $image,
"ACL" => "public-read",
"Metadata" => [
"Content-MD5" => base64_encode($fileMd5)
]
]);
见http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html#object-metadata
要获取 PHP 中文件的 md5,您可以使用 md5_file 函数
使用 ContentSHA256 密钥使其正常工作:
$sha256 = hash_file("sha256", $image);
$result = $s3->putObject([
"Bucket" => getenv("S3_BUCKET_NAME"),
"Key" => $key,
"SourceFile" => $image,
"ACL" => "public-read",
"ContentSHA256" => $sha256
]);
如果 SHA 不匹配,我会得到一个 S3 异常,这正是我想要的。