Skip to content

Commit d3b76f0

Browse files
author
Will Toozs
committed
fixup: bucketName source + error if no key field
1 parent e9a4300 commit d3b76f0

File tree

3 files changed

+58
-4
lines changed

3 files changed

+58
-4
lines changed

lib/api/api.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,13 @@ const api = {
217217

218218
formDataParser.on('file', (fieldname, file, filename, encoding, mimetype) => {
219219
fileEventData = { fieldname, file, filename, encoding, mimetype };
220-
return next(null);
220+
// no key field == error
221+
if ('key' in request.formData) {
222+
return next(null);
223+
}
224+
return next(errors.InvalidArgument.customizeDescription('Bucket POST must contain'
225+
+ " a field named 'key'. If it is specified, please check the order of the fields."));
221226
});
222-
223227
formDataParser.on('finish', () => {
224228
// No file field == error
225229
if (!fileEventData) {

lib/api/objectPost.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,16 @@ function objectPost(authInfo, request, streamingV4Params, log, callback) {
3737
const {
3838
headers,
3939
method,
40+
formData,
41+
bucketName,
4042
} = request;
4143
let parsedContentLength = 0;
4244
const passThroughStream = new PassThrough();
4345
const requestType = request.apiMethods || 'objectPost';
4446
const valParams = {
4547
authInfo,
46-
bucketName: request.formData.bucket,
47-
objectKey: request.formData.key,
48+
bucketName,
49+
objectKey: formData.key,
4850
requestType,
4951
request,
5052
};

tests/functional/aws-node-sdk/test/object/post.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,54 @@ describe('POST object', () => {
388388
});
389389
});
390390

391+
it('should handle error when key is missing', done => {
392+
const { bucketName, url } = testContext;
393+
// Prep fields then remove the key field
394+
let fields = calculateFields(ak, sk, bucketName);
395+
fields = fields.filter(e => e.name !== 'key');
396+
397+
const formData = new FormData();
398+
399+
fields.forEach(field => {
400+
formData.append(field.name, field.value);
401+
});
402+
403+
formData.append('file', fs.createReadStream(path.join(__dirname, filename)));
404+
405+
formData.getLength((err, length) => {
406+
if (err) {
407+
return done(err);
408+
}
409+
410+
return axios.post(url, formData, {
411+
headers: {
412+
...formData.getHeaders(),
413+
'Content-Length': length,
414+
},
415+
})
416+
.then(() => {
417+
done(new Error('Request should not succeed without key field'));
418+
})
419+
.catch(err => {
420+
assert.ok(err.response, 'Error should be returned by axios');
421+
422+
// Parse the XML error response
423+
xml2js.parseString(err.response.data, (err, result) => {
424+
if (err) {
425+
return done(err);
426+
}
427+
428+
const error = result.Error;
429+
assert.equal(error.Code[0], 'InvalidArgument');
430+
assert.equal(error.Message[0],
431+
'Bucket POST must contain a field named '
432+
+ "'key'. If it is specified, please check the order of the fields.");
433+
return done();
434+
});
435+
});
436+
});
437+
});
438+
391439
it('should upload an object with key slash', done => {
392440
const { bucketName, url } = testContext;
393441
const slashKey = '/';

0 commit comments

Comments
 (0)