s3: make sure Expires always is a Date.

https://github.com/sitespeedio/sitespeed.io/issues/4501
This commit is contained in:
Peter Hedenskog 2025-04-18 21:32:51 +02:00
parent 24066f6152
commit 6604d6413c
1 changed files with 19 additions and 3 deletions

View File

@ -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}`);