sha256 is the SHA256 Hash of a file. I use then btoa(256) to base64 it.
The result is always
InvalidRequest: Value for x-amz-checksum-sha256 header is invalid.
AWS 文件说,
ChecksumSHA256 — (String) The base64-encoded, 256-bit SHA-256 digest of the object. This will only be present if it was uploaded with the object. With multipart uploads, this may not be a checksum value of the object. For more information about how checksums are calculated with multipart uploads, see Checking object integrity in the Amazon S3 User Guide. Checking object integrity - Amazon Simple Storage Service Verify the integrity of objects uploaded and downloaded to Amazon S3.
let sha256conv = btoa(sha256);
const params = {
Bucket: process.env.BUCKET,
Key: path.basename(task.file),
Body: fileData,
ContentType: ContentType || application/octet-stream ,
CacheControl: "max-age=172800",
ChecksumAlgorithm: sha256 ,
ChecksumSHA256: sha256conv
};
const upload = new AWS.S3.ManagedUpload({
service: s3,
params
});
第256页就是这样:
export async function getFileSha256(fileName, fileSize, onProgress) {
return new Promise((resolve, reject) => {
const hash = crypto.createHash( sha256 );
// change to binary if you want a binary hash.
hash.setEncoding( hex );
const bar1 = new cliProgress.SingleBar({}, cliProgress.Presets.shades_classic);
bar1.start(fileSize, 0);
const fd = fs.createReadStream(fileName);
fd.on( data , async chunk => {
let processedBytes = bar1.value + chunk.length;
bar1.update(processedBytes);
await onProgress(processedBytes)
})
fd.on( end , function () {
bar1.stop();
hash.end();
return resolve(hash.read())
});
// read all file and pipe it (write it) to the hash object
fd.pipe(hash);
});
}