Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for metadata and tagging files #83

Merged
merged 3 commits into from
Jan 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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({}));
});