From bde5506b102bc07781d4ff5ae0b450a803886c68 Mon Sep 17 00:00:00 2001 From: Nils Knappmeier Date: Sat, 5 Aug 2023 18:09:54 +0200 Subject: [PATCH] more functions that help uploading a file listing. --- tasks/aws-s3-builds-page/s3client/index.js | 11 ++++++-- tasks/aws-s3-builds-page/s3client/s3-test.js | 28 +++++++++++++++++++ .../aws-s3-builds-page/s3client/uploadFile.js | 23 +++++++++++++-- .../aws-s3-builds-page/test-utils/runTest.js | 4 ++- 4 files changed, 59 insertions(+), 7 deletions(-) diff --git a/tasks/aws-s3-builds-page/s3client/index.js b/tasks/aws-s3-builds-page/s3client/index.js index 62771dff..10e5dfd6 100644 --- a/tasks/aws-s3-builds-page/s3client/index.js +++ b/tasks/aws-s3-builds-page/s3client/index.js @@ -1,5 +1,5 @@ const { listFiles } = require('./listFiles'); -const { uploadFile } = require('./uploadFile'); +const { uploadFile, uploadData } = require('./uploadFile'); const { deleteFile } = require('./deleteFile'); const { S3Client } = require('@aws-sdk/client-s3'); const { requireEnvVar } = require('./requireEnvVar'); @@ -21,8 +21,13 @@ function createS3Client() { async listFiles() { return listFiles(s3Client, bucket); }, - async uploadFile(localName, remoteName) { - await uploadFile(s3Client, bucket, localName, remoteName); + async uploadFile(localName, remoteName, { contentType } = {}) { + await uploadFile(s3Client, bucket, localName, remoteName, { + contentType + }); + }, + async uploadData(data, remoteName, { contentType } = {}) { + await uploadData(s3Client, bucket, data, remoteName, { contentType }); }, async deleteFile(remoteName) { await deleteFile(s3Client, bucket, remoteName); diff --git a/tasks/aws-s3-builds-page/s3client/s3-test.js b/tasks/aws-s3-builds-page/s3client/s3-test.js index b54dc33d..58e5448b 100644 --- a/tasks/aws-s3-builds-page/s3client/s3-test.js +++ b/tasks/aws-s3-builds-page/s3client/s3-test.js @@ -29,6 +29,23 @@ runTest(async ({ log }) => { log(`Check contents of "${filename}"`); const uploadedContents = await client.fetchFile(filename); expectStringContains('"name": "handlebars"', uploadedContents); + await expectContentType(filename, 'application/octet-stream'); + + log(`Uploading with content type "${filename}"`); + await client.uploadFile('package.json', filename, { + contentType: 'text/html' + }); + log('Checking content-type'); + await expectContentType(filename, 'text/html'); + + log('Upload data as text/plain'); + await client.uploadData('Hello world', filename, { + contentType: 'text/plain' + }); + log('Checking content-type'); + await expectContentType(filename, 'text/plain'); + log(`Check contents of "${filename}"`); + expectStringContains('Hello world', await client.fetchFile(filename)); log(`Delete "${filename}"`); await client.deleteFile(filename); @@ -45,3 +62,14 @@ function expectStringContains(needle, haystack) { throw new Error(`Expecting to find "${needle}" in string "${haystack}"`); } } + +async function expectContentType(remoteName, expectedContentType) { + const contentType = (await fetch(client.fileUrl(remoteName))).headers.get( + 'Content-Type' + ); + if (contentType !== expectedContentType) { + throw new Error( + `Expecting to find content-type "${expectedContentType}" but found "${contentType}"` + ); + } +} diff --git a/tasks/aws-s3-builds-page/s3client/uploadFile.js b/tasks/aws-s3-builds-page/s3client/uploadFile.js index 231b8cd3..ea2a9ec0 100644 --- a/tasks/aws-s3-builds-page/s3client/uploadFile.js +++ b/tasks/aws-s3-builds-page/s3client/uploadFile.js @@ -1,14 +1,31 @@ const { PutObjectCommand } = require('@aws-sdk/client-s3'); const fs = require('node:fs/promises'); -async function uploadFile(s3Client, bucket, localName, remoteName) { +async function uploadFile( + s3Client, + bucket, + localName, + remoteName, + { contentType } = {} +) { const fileContents = await fs.readFile(localName); + await uploadData(s3Client, bucket, fileContents, remoteName, { contentType }); +} + +async function uploadData( + s3Client, + bucket, + data, + remoteName, + { contentType } = {} +) { const command = new PutObjectCommand({ Bucket: bucket, Key: remoteName, - Body: fileContents + Body: data, + ContentType: contentType }); await s3Client.send(command); } -module.exports = { uploadFile }; +module.exports = { uploadFile, uploadData }; diff --git a/tasks/aws-s3-builds-page/test-utils/runTest.js b/tasks/aws-s3-builds-page/test-utils/runTest.js index d6cff124..1c94718a 100644 --- a/tasks/aws-s3-builds-page/test-utils/runTest.js +++ b/tasks/aws-s3-builds-page/test-utils/runTest.js @@ -6,6 +6,9 @@ const s3Client = createS3Client(); function runTest(asyncFn) { asyncFn({ log: console.log.bind(console) }) .finally(detectSurplusFiles) + .then(() => { + console.log('DONE'); + }) .catch(error => { console.error(error); process.exit(1); @@ -27,7 +30,6 @@ async function detectSurplusFiles() { if (surplusFileDetected) { console.log(`run with --delete-surplus to delete surplus files`); } - console.log('DONE'); } module.exports = { runTest };