@parse/s3-files-adapter
Version:
AWS S3 adapter for parse-server
62 lines (52 loc) • 1.86 kB
JavaScript
const { Readable } = require('stream');
const S3Adapter = require('../../index.js');
const { GetObjectCommand, PutObjectCommand, DeleteObjectCommand } = require('@aws-sdk/client-s3');
function getMockS3Adapter(options) {
const accessKey = process.env.TEST_S3_ACCESS_KEY || 'ACCESS_KEY';
const secretKey = process.env.TEST_S3_SECRET_KEY || 'SECRET_KEY';
const bucket = process.env.TEST_S3_BUCKET || 'BUCKET';
const region = process.env.TEST_S3_REGION || 'us-east-1';
const s3 = new S3Adapter(Object.assign({
accessKey,
secretKey,
bucket,
}, options));
const objects = {};
s3._s3Client = {
// @ts-ignore
send: command => {
if (command instanceof PutObjectCommand) {
const { Key, Body } = command.input;
objects[Key] = Body;
return Promise.resolve({ Location: `https://${bucket}.s3.${region}.amazonaws.com/${Key}` });
}
if (command instanceof DeleteObjectCommand) {
const { Key } = command.input;
delete objects[Key];
return Promise.resolve({});
}
if (command instanceof GetObjectCommand) {
const { Key } = command.input;
if (objects[Key] !== undefined) {
const stored = objects[Key];
const content = Buffer.isBuffer(stored) ? stored : Buffer.from(String(stored));
const stream = new Readable();
stream.push(content);
stream.push(null);
return {
Body: stream,
AcceptRanges: 'bytes',
ContentLength: content.length,
ContentRange: `bytes 0-${content.length - 1}/${content.length}`,
ContentType: 'text/plain',
};
} else {
return Promise.reject(new Error('Not found'));
}
}
return Promise.resolve();
},
};
return s3;
}
module.exports = { getMockS3Adapter };