Skip to content

Commit

Permalink
Merge pull request #83 from stevestencil/metadata-and-tags
Browse files Browse the repository at this point in the history
added support for metadata and tagging files
  • Loading branch information
davimacedo authored Jan 16, 2020
2 parents 2ddbfd6 + 8fa44fb commit 5bbc7fa
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
19 changes: 18 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ const awsCredentialsDeprecationNotice = function awsCredentialsDeprecationNotice
'See: https://github.com/parse-server-modules/parse-server-s3-adapter#aws-credentials for details');
};

const serialize = (obj) => {
const str = [];
Object.keys(obj).forEach((key) => {
if (obj[key]) {
str.push(`${encodeURIComponent(key)}=${encodeURIComponent(obj[key])}`);
}
});
return str.join('&');
};

class S3Adapter {
// Creates an S3 session.
// Providing AWS access, secret keys and bucket are mandatory
Expand Down Expand Up @@ -66,7 +76,7 @@ class S3Adapter {

// For a given config object, filename, and data, store a file in S3
// Returns a promise containing the S3 object creation response
createFile(filename, data, contentType) {
createFile(filename, data, contentType, options = {}) {
const params = {
Key: this._bucketPrefix + filename,
Body: data,
Expand All @@ -88,6 +98,13 @@ class S3Adapter {
if (this._encryption === 'AES256' || this._encryption === 'aws:kms') {
params.ServerSideEncryption = this._encryption;
}
if (options.metadata && typeof options.metadata === 'object') {
params.Metadata = options.metadata;
}
if (options.tags && typeof options.tags === 'object') {
const serializedTags = serialize(options.tags);
params.Tagging = serializedTags;
}
return this.createBucket().then(() => new Promise((resolve, reject) => {
this._s3Client.upload(params, (err, response) => {
if (err !== null) {
Expand Down
39 changes: 39 additions & 0 deletions spec/test.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,5 +441,44 @@ describe('S3Adapter tests', () => {
afterAll(() => Promise.all(promises));
});

describe('createFile', () => {
let options;
beforeEach(() => {
options = {
bucketPrefix: 'test/',
};
});

it('should save a file with metadata added', async () => {
const s3 = makeS3Adapter(options);
s3._s3Client.upload = (params, callback) => {
const { Metadata } = params;
expect(Metadata).toEqual({ foo: 'bar' });
const data = {
Body: Buffer.from('hello world', 'utf8'),
};
callback(null, data);
};
const fileName = 'randomFileName.txt';
const metadata = { foo: 'bar' };
await s3.createFile(fileName, 'hello world', 'text/utf8', { metadata });
});

it('should save a file with tags added', async () => {
const s3 = makeS3Adapter(options);
s3._s3Client.upload = (params, callback) => {
const { Tagging } = params;
expect(Tagging).toEqual('foo=bar&baz=bin');
const data = {
Body: Buffer.from('hello world', 'utf8'),
};
callback(null, data);
};
const fileName = 'randomFileName.txt';
const tags = { foo: 'bar', baz: 'bin' };
await s3.createFile(fileName, 'hello world', 'text/utf8', { tags });
});
});

filesAdapterTests.testAdapter('S3Adapter', makeS3Adapter({}));
});

0 comments on commit 5bbc7fa

Please sign in to comment.