S3PutObject Delete must delete folders with content added

This commit is contained in:
Ryan Martin 2016-02-26 10:28:25 -05:00
parent ea90a7c231
commit 982064fa55
6 changed files with 49 additions and 6 deletions

View file

@ -101,7 +101,7 @@ DynamoDBPutItemsFunctionArn
Mirrors the [S3.putObject API method](http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property).
This will delete the objects when the corresponding stack is deleted.
This will delete the objects and any added sub-objects (for "folders") when the corresponding stack is deleted.
#### Parameters

View file

@ -64,7 +64,7 @@ PutItems.prototype.handleDelete = function(referenceData) {
}
});
} else {
return Promise.try(function() {});
return helpers.futureSuccessful();
}
}
exports.putItems = function(event, context) {

View file

@ -1,6 +1,7 @@
var Promise = require('bluebird'),
AWS = require('aws-sdk'),
base = require('lib/base'),
helpers = require('lib/helpers'),
s3 = Promise.promisifyAll(new AWS.S3());
// Exposes the SNS.subscribe API method
@ -15,9 +16,31 @@ PutObject.prototype.handleCreate = function() {
}
PutObject.prototype.handleDelete = function(referenceData) {
var p = this.event.ResourceProperties;
return s3.deleteObjectAsync({
Bucket: p.Bucket,
Key: p.Key
var deleteSubObjects =
(p.Key.endsWith("/"))
? s3.listObjectsAsync({
Bucket: p.Bucket,
Prefix: p.Key
})
.then(function(subObjects) {
return Promise
.map(
subObjects.Contents,
function(item) {
return s3.deleteObjectAsync({
Bucket: p.Bucket,
Key: item.Key
})
}
)
})
: helpers.futureSuccessful();
return deleteSubObjects
.then(function() {
return s3.deleteObjectAsync({
Bucket: p.Bucket,
Key: p.Key
});
});
}
exports.putObject = function(event, context) {

View file

@ -1,6 +1,7 @@
var Promise = require('bluebird'),
AWS = require('aws-sdk'),
base = require('lib/base'),
helpers = require('lib/helpers'),
sns = Promise.promisifyAll(new AWS.SNS());
// Exposes the SNS.subscribe API method
@ -22,7 +23,7 @@ Subscribe.prototype.handleDelete = function(referenceData) {
SubscriptionArn: referenceData.SubscriptionArn
});
} else {
return Promise.try(function() {});
return helpers.futureSuccessful();
}
}
exports.subscribe = function(event, context) {

View file

@ -136,6 +136,7 @@
"Effect": "Allow",
"Action": [
"s3:DeleteObject",
"s3:ListBucket",
"s3:PutObject"
],
"Resource": "*"

View file

@ -1,3 +1,5 @@
var Promise = require('bluebird');
// Translates from raw JSON into DynamoDB-formatted JSON. This is more than a
// convenience thing: the original iteration of this accepted DyanamoDB-JSON Items,
// to avoid complications in translation. But when CloudFormation passes the parameters
@ -83,4 +85,20 @@ exports.formatFromDynamo = function(value) {
throw "Unrecognized type [" + (typeof value) + "]";
}
return result;
}
exports.futureSuccessful = function() {
return Promise.try(function() {});
}
if (!String.prototype.endsWith) {
String.prototype.endsWith = function(searchString, position) {
var subjectString = this.toString();
if (typeof position !== 'number' || !isFinite(position) || Math.floor(position) !== position || position > subjectString.length) {
position = subjectString.length;
}
position -= searchString.length;
var lastIndex = subjectString.indexOf(searchString, position);
return lastIndex !== -1 && lastIndex === position;
};
}