Commit 6e22506e authored by nanahira's avatar nanahira

reverify md5 in upload

parent 29cb3a85
Pipeline #40954 passed with stages
in 7 minutes and 21 seconds
......@@ -364,11 +364,13 @@ export class PackagerService extends ConsoleLogger {
});
});
const hashObject = createHash('sha256');
const md5Object = createHash('md5');
// let length = 0;
child.stdout.on('data', (chunk) => {
// length += chunk.length;
// this.log(`Received ${length} bytes of archive ${archiveName}.`);
hashObject.update(chunk);
md5Object.update(chunk);
});
const uploadStream = child.stdout;
......@@ -397,6 +399,11 @@ export class PackagerService extends ConsoleLogger {
});
const [, { object }] = await Promise.all([childPromise, uploadPromise]);
archive.hash = hashObject.digest('hex');
const md5 = md5Object.digest('hex');
const md5InS3 = await this.s3.getObjectHash(archiveName);
if (md5InS3 && md5InS3 !== md5) {
throw new Error(`MD5 mismatch for archive ${archiveName}: local ${md5} vs s3 ${md5InS3}`);
}
await this.redis.set(`hash:${archive.path}`, archive.hash, 'EX', 60 * 60 * 24);
archive.size = object.Size;
this.log(`Finished archiving ${archiveName}.`);
......
......@@ -73,18 +73,22 @@ export class S3Service extends ConsoleLogger {
return this.s3.send(command);
}
async hasObject(path: string) {
async getObjectHash(path: string) {
try {
await this.s3.send(
const head = await this.s3.send(
new HeadObjectCommand({
Bucket: this.bucket,
Key: this.getPathWithPrefix(path),
})
);
return true;
const etag = head.ETag;
if (etag) {
return etag.replace(/"/g, '');
}
return undefined;
} catch (e) {
if (e.name === 'NotFound') {
return false;
return undefined;
}
throw e; // rethrow other errors
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment