Skip to content

Commit 131e1db

Browse files
committed
Handle NotFound S3 Response
After the upgrade to AWS SDK V3 the headObject method needed to be refactored for cases where the key or bucket are missing and S3 returns a 404. In V3 of the SDK this is a thrown error that much be caught.
1 parent 07023b0 commit 131e1db

File tree

2 files changed

+15
-20
lines changed

2 files changed

+15
-20
lines changed

lib/s3.js

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,15 @@ var readFile = RSVP.denodeify(fs.readFile);
99
var mime = require('mime-types');
1010
var joinUriSegments = require('./util/join-uri-segments');
1111

12-
function headObject(client, params) {
13-
return new RSVP.Promise(function(resolve, reject) {
14-
client.headObject(params, function(err, data) {
15-
if (err && err.code === 'NotFound') {
16-
return resolve();
17-
}
18-
else if (err) {
19-
return reject(err);
20-
}
21-
else {
22-
return resolve(data);
23-
}
24-
});
25-
});
12+
async function headObject(client, params) {
13+
try {
14+
return await client.headObject(params);
15+
} catch (err) {
16+
if (err.name === 'NotFound') {
17+
return;
18+
}
19+
throw err;
20+
}
2621
}
2722

2823
module.exports = CoreObject.extend({
@@ -37,26 +32,26 @@ module.exports = CoreObject.extend({
3732
this._plugin = plugin;
3833

3934
var providedS3Client = plugin.readConfig("s3Client");
40-
35+
4136

4237
if (profile && !providedS3Client) {
4338
this._plugin.log("Using AWS profile from config", { verbose: true });
4439
credentials = fromIni({ profile: profile });
4540
}
4641

4742
if (endpoint) {
48-
this._plugin.log('Using endpoint from config', { verbose: true });
43+
this._plugin.log('Using endpoint from config', { verbose: true });
4944
}
5045

5146
this._client = providedS3Client || new S3(config);
52-
47+
5348
if (endpoint) {
5449
this._client.config.endpoint = endpoint;
5550
}
5651
if (credentials) {
5752
this._client.config.credentials = credentials;
5853
}
59-
54+
6055
},
6156

6257
upload: function(options) {

tests/unit/lib/s3-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ describe('s3', function() {
2727
cb(undefined, revisionsData);
2828
},
2929

30-
headObject: function(params, cb) {
30+
headObject: async function(params, cb) {
3131
headParams = params;
32-
cb(undefined, currentData);
32+
return currentData;
3333
},
3434

3535
copyObject: function(params, cb) {

0 commit comments

Comments
 (0)