Skip to content

Commit

Permalink
fix s3 content compare
Browse files Browse the repository at this point in the history
  • Loading branch information
maruware committed Dec 26, 2019
1 parent fca516b commit 567cd40
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
15 changes: 9 additions & 6 deletions src/s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const resolveObjectKey = (cwd: string, base: string, filename: string) => {
return relative(join(cwd, base), filename)
}

type FileDef = { name: string; key: string }
type FileDef = { name: string; key: string; md5: string }

const uploadFile = async (s3: S3, file: FileDef, params: DeployArgsParams) => {
const body = createReadStream(file.name)
Expand All @@ -26,6 +26,9 @@ const uploadFile = async (s3: S3, file: FileDef, params: DeployArgsParams) => {
Key: file.key,
Body: body,
ContentType: contentType,
Metadata: {
md5: file.md5
},
...params
},
service: s3
Expand Down Expand Up @@ -100,14 +103,14 @@ export const deployTask = async ({
const s3 = new S3({ ...config, computeChecksums: true })
const bucket = params.Bucket
const filenames = await globAsync(pattern)
const files = filenames
const files = await Promise.all(filenames
.filter(file => lstatSync(file).isFile())
.map(file => ({
.map(async file => ({
name: file,
key: resolveObjectKey(cwd, base, file),
md5: calcMd5FromStream(file),
md5: await calcMd5FromStream(file),
size: lstatSync(file).size
}))
})))
const tasks: ListrTask<any>[] = files.map(
(file): ListrTask<any> => {
return {
Expand All @@ -119,7 +122,7 @@ export const deployTask = async ({
.promise()
if (r.ETag === `"${file.md5}"`) return true
// Large file can not be compare ETag. So compare file size.
if (r.ContentLength === file.size) return true
if (r.Metadata && r.Metadata.md5 === file.md5) return true
return false
} catch {
return false
Expand Down
6 changes: 3 additions & 3 deletions src/utils/md5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import crypto from 'crypto'
import { createReadStream } from 'fs'

export const calcMd5FromStream = (filePath: string) => {
return new Promise((resolve, reject) => {
var readStream = createReadStream(filePath)
var md5hash = crypto.createHash('md5')
return new Promise<string>((resolve, reject) => {
const readStream = createReadStream(filePath)
const md5hash = crypto.createHash('md5')
md5hash.setEncoding('base64')
readStream.pipe(md5hash)
readStream.on('end', () => {
Expand Down

0 comments on commit 567cd40

Please sign in to comment.