Skip to content

Commit

Permalink
fixup: bucketName source + error if no key field
Browse files Browse the repository at this point in the history
  • Loading branch information
Will Toozs committed Jul 9, 2024
1 parent e9a4300 commit d3b76f0
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
8 changes: 6 additions & 2 deletions lib/api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,13 @@ const api = {

formDataParser.on('file', (fieldname, file, filename, encoding, mimetype) => {
fileEventData = { fieldname, file, filename, encoding, mimetype };
return next(null);
// no key field == error
if ('key' in request.formData) {
return next(null);
}
return next(errors.InvalidArgument.customizeDescription('Bucket POST must contain'
+ " a field named 'key'. If it is specified, please check the order of the fields."));
});

formDataParser.on('finish', () => {
// No file field == error
if (!fileEventData) {
Expand Down
6 changes: 4 additions & 2 deletions lib/api/objectPost.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,16 @@ function objectPost(authInfo, request, streamingV4Params, log, callback) {
const {
headers,
method,
formData,
bucketName,
} = request;
let parsedContentLength = 0;
const passThroughStream = new PassThrough();
const requestType = request.apiMethods || 'objectPost';
const valParams = {
authInfo,
bucketName: request.formData.bucket,
objectKey: request.formData.key,
bucketName,
objectKey: formData.key,
requestType,
request,
};
Expand Down
48 changes: 48 additions & 0 deletions tests/functional/aws-node-sdk/test/object/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,54 @@ describe('POST object', () => {
});
});

it('should handle error when key is missing', done => {
const { bucketName, url } = testContext;
// Prep fields then remove the key field
let fields = calculateFields(ak, sk, bucketName);
fields = fields.filter(e => e.name !== 'key');

const formData = new FormData();

fields.forEach(field => {
formData.append(field.name, field.value);
});

formData.append('file', fs.createReadStream(path.join(__dirname, filename)));

formData.getLength((err, length) => {
if (err) {
return done(err);
}

return axios.post(url, formData, {
headers: {
...formData.getHeaders(),
'Content-Length': length,
},
})
.then(() => {
done(new Error('Request should not succeed without key field'));
})
.catch(err => {
assert.ok(err.response, 'Error should be returned by axios');

// Parse the XML error response
xml2js.parseString(err.response.data, (err, result) => {
if (err) {
return done(err);
}

const error = result.Error;
assert.equal(error.Code[0], 'InvalidArgument');
assert.equal(error.Message[0],
'Bucket POST must contain a field named '
+ "'key'. If it is specified, please check the order of the fields.");
return done();
});
});
});
});

it('should upload an object with key slash', done => {
const { bucketName, url } = testContext;
const slashKey = '/';
Expand Down

0 comments on commit d3b76f0

Please sign in to comment.