Skip to content

Commit 5bbc7fa

Browse files
authored
Merge pull request #83 from stevestencil/metadata-and-tags
added support for metadata and tagging files
2 parents 2ddbfd6 + 8fa44fb commit 5bbc7fa

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

index.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ const awsCredentialsDeprecationNotice = function awsCredentialsDeprecationNotice
1111
'See: https://github.com/parse-server-modules/parse-server-s3-adapter#aws-credentials for details');
1212
};
1313

14+
const serialize = (obj) => {
15+
const str = [];
16+
Object.keys(obj).forEach((key) => {
17+
if (obj[key]) {
18+
str.push(`${encodeURIComponent(key)}=${encodeURIComponent(obj[key])}`);
19+
}
20+
});
21+
return str.join('&');
22+
};
23+
1424
class S3Adapter {
1525
// Creates an S3 session.
1626
// Providing AWS access, secret keys and bucket are mandatory
@@ -66,7 +76,7 @@ class S3Adapter {
6676

6777
// For a given config object, filename, and data, store a file in S3
6878
// Returns a promise containing the S3 object creation response
69-
createFile(filename, data, contentType) {
79+
createFile(filename, data, contentType, options = {}) {
7080
const params = {
7181
Key: this._bucketPrefix + filename,
7282
Body: data,
@@ -88,6 +98,13 @@ class S3Adapter {
8898
if (this._encryption === 'AES256' || this._encryption === 'aws:kms') {
8999
params.ServerSideEncryption = this._encryption;
90100
}
101+
if (options.metadata && typeof options.metadata === 'object') {
102+
params.Metadata = options.metadata;
103+
}
104+
if (options.tags && typeof options.tags === 'object') {
105+
const serializedTags = serialize(options.tags);
106+
params.Tagging = serializedTags;
107+
}
91108
return this.createBucket().then(() => new Promise((resolve, reject) => {
92109
this._s3Client.upload(params, (err, response) => {
93110
if (err !== null) {

spec/test.spec.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,5 +441,44 @@ describe('S3Adapter tests', () => {
441441
afterAll(() => Promise.all(promises));
442442
});
443443

444+
describe('createFile', () => {
445+
let options;
446+
beforeEach(() => {
447+
options = {
448+
bucketPrefix: 'test/',
449+
};
450+
});
451+
452+
it('should save a file with metadata added', async () => {
453+
const s3 = makeS3Adapter(options);
454+
s3._s3Client.upload = (params, callback) => {
455+
const { Metadata } = params;
456+
expect(Metadata).toEqual({ foo: 'bar' });
457+
const data = {
458+
Body: Buffer.from('hello world', 'utf8'),
459+
};
460+
callback(null, data);
461+
};
462+
const fileName = 'randomFileName.txt';
463+
const metadata = { foo: 'bar' };
464+
await s3.createFile(fileName, 'hello world', 'text/utf8', { metadata });
465+
});
466+
467+
it('should save a file with tags added', async () => {
468+
const s3 = makeS3Adapter(options);
469+
s3._s3Client.upload = (params, callback) => {
470+
const { Tagging } = params;
471+
expect(Tagging).toEqual('foo=bar&baz=bin');
472+
const data = {
473+
Body: Buffer.from('hello world', 'utf8'),
474+
};
475+
callback(null, data);
476+
};
477+
const fileName = 'randomFileName.txt';
478+
const tags = { foo: 'bar', baz: 'bin' };
479+
await s3.createFile(fileName, 'hello world', 'text/utf8', { tags });
480+
});
481+
});
482+
444483
filesAdapterTests.testAdapter('S3Adapter', makeS3Adapter({}));
445484
});

0 commit comments

Comments
 (0)