more functions that help uploading a file listing.
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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}"`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 };
|
||||
|
||||
@@ -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 };
|
||||
|
||||
Reference in New Issue
Block a user