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

Add support for ${filename} in POST Object #803

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 6 additions & 2 deletions lib/controllers/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,10 +313,14 @@ exports.postObject = async function postObject(ctx) {
break;
}
};
const fileHandler = (fieldname, file) => {
const fileHandler = (fieldname, file, filename) => {
if (fieldname !== 'file' || fileCount++) {
return file.resume();
}
// S3 strips leading spaces from filename here, even though their docs
// don't mention it. I have raised this as an issue with AWS Enterprise support
// eslint-disable-next-line no-template-curly-in-string
key = key.replace('${filename}', filename.replace(/^ +/, ''));
resolve(new S3Object(ctx.params.bucket, key, file, metadata));
};

Expand Down Expand Up @@ -406,7 +410,7 @@ exports.postObject = async function postObject(ctx) {
if (!location.pathname.endsWith('/')) {
location.pathname += '/';
}
location.pathname += object.key;
location.pathname += encodeURIComponent(object.key);
ctx.set('Location', location.href);
}

Expand Down
4 changes: 2 additions & 2 deletions test/controllers/object.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ describe('Operations on Objects', () => {
describe('POST Object', () => {
it('stores a text object for a multipart/form-data request', async function () {
const form = new FormData();
form.append('key', 'text');
form.append('key', 'my_files/${filename}');
form.append('file', 'Hello!', 'post_file.txt');
const res = await request.post('bucket-a', {
baseUrl: s3Client.endpoint.href,
Expand All @@ -387,7 +387,7 @@ describe('Operations on Objects', () => {
});
expect(res.statusCode).to.equal(204);
const object = await s3Client
.getObject({ Bucket: 'bucket-a', Key: 'text' })
.getObject({ Bucket: 'bucket-a', Key: 'my_files/post_file.txt' })
.promise();
expect(object.ContentType).to.equal('binary/octet-stream');
expect(object.Body).to.deep.equal(Buffer.from('Hello!'));
Expand Down