From 6604d6413c962a52dca916b2f20f08b2b0d01ee5 Mon Sep 17 00:00:00 2001 From: Peter Hedenskog Date: Fri, 18 Apr 2025 21:32:51 +0200 Subject: [PATCH] s3: make sure Expires always is a Date. https://github.com/sitespeedio/sitespeed.io/issues/4501 --- lib/plugins/s3/index.js | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/plugins/s3/index.js b/lib/plugins/s3/index.js index 528ee718b..42f1fb945 100644 --- a/lib/plugins/s3/index.js +++ b/lib/plugins/s3/index.js @@ -12,19 +12,35 @@ import { runWithConcurrencyLimit } from './limit.js'; const log = getLogger('sitespeedio.plugin.s3'); +const normalizeExpires = maybeSeconds => { + if (typeof maybeSeconds === 'number' && Number.isFinite(maybeSeconds)) { + const seconds = Number.parseInt(maybeSeconds, 10); + const expires = new Date(Date.now() + seconds * 1000); + log.info('Setting s3.expires to ' + expires); + return expires; + } + return maybeSeconds; +}; + async function uploadFile(file, s3Client, s3Options, prefix, baseDir) { const stream = await fsPromises.readFile(file); const contentType = getContentType(file); const subPath = path.relative(baseDir, file); - const parameters = { + const baseParams = { Bucket: s3Options.bucketname, Key: path.join(s3Options.path || prefix, subPath), Body: stream, ContentType: contentType, - ACL: s3Options.acl, - ...s3Options.params + ACL: s3Options.acl }; + const userParams = { ...s3Options.params }; + if ('Expires' in userParams) { + userParams.Expires = normalizeExpires(userParams.Expires); + } + + const parameters = { ...baseParams, ...userParams }; + try { await s3Client.send(new PutObjectCommand(parameters)); log.debug(`Uploaded ${file} to S3 bucket ${s3Options.bucketname}`);