Move S3PutObject helper to new format
This commit is contained in:
parent
c81f9bca4d
commit
a8f46953d1
5 changed files with 84 additions and 33 deletions
27
aws/s3.js
Normal file
27
aws/s3.js
Normal file
|
@ -0,0 +1,27 @@
|
|||
var Promise = require('bluebird'),
|
||||
AWS = require('aws-sdk'),
|
||||
base = require('lib/base'),
|
||||
dynamoDB = Promise.promisifyAll(new AWS.DynamoDB()),
|
||||
s3 = Promise.promisifyAll(new AWS.S3());
|
||||
|
||||
// Exposes the SNS.subscribe API method
|
||||
function PutObject(event, context, functionIdentifier) {
|
||||
base.Handler.call(this, event, context, functionIdentifier);
|
||||
}
|
||||
PutObject.prototype = Object.create(base.Handler.prototype);
|
||||
PutObject.prototype.handleCreate = function() {
|
||||
var p = this.event.ResourceProperties;
|
||||
delete p.ServiceToken;
|
||||
return s3.putObjectAsync(p);
|
||||
}
|
||||
PutObject.prototype.handleDelete = function(referenceData) {
|
||||
var p = this.event.ResourceProperties;
|
||||
return s3.deleteObjectAsync({
|
||||
Bucket: p.Bucket,
|
||||
Key: p.Key
|
||||
});
|
||||
}
|
||||
exports.putObject = function(event, context) {
|
||||
handler = new PutObject(event, context, "S3PutObjectFunction");
|
||||
handler.handle();
|
||||
}
|
|
@ -15,7 +15,7 @@ Subscribe.prototype.handleCreate = function() {
|
|||
Endpoint: p.Endpoint,
|
||||
Protocol: p.Protocol,
|
||||
TopicArn: p.TopicArn
|
||||
})
|
||||
});
|
||||
}
|
||||
Subscribe.prototype.handleDelete = function(referenceData) {
|
||||
if (referenceData) {
|
||||
|
|
|
@ -2,9 +2,7 @@ var AWS = require('aws-sdk');
|
|||
var Promise = require('bluebird');
|
||||
var apiGateway = Promise.promisifyAll(new AWS.APIGateway()),
|
||||
dynamoDB = new AWS.DynamoDB(),
|
||||
response = require('./lib/cfn-response'),
|
||||
s3 = new AWS.S3(),
|
||||
sns = new AWS.SNS();
|
||||
response = require('./lib/cfn-response');
|
||||
|
||||
exports.apiGatewayCreateRestApi = function(event, context) {
|
||||
var p = event.ResourceProperties;
|
||||
|
@ -42,34 +40,6 @@ exports.dynamoDBPutItems = function(event, context) {
|
|||
}
|
||||
}
|
||||
|
||||
// Puts an object into S3.
|
||||
exports.s3PutObject = function(event, context) {
|
||||
var p = event.ResourceProperties;
|
||||
if (event.RequestType == 'Delete') {
|
||||
s3.deleteObject({
|
||||
Bucket: p.Bucket,
|
||||
Key: p.Key,
|
||||
RequestPayer: p.RequestPayer
|
||||
}, function(err, data) {
|
||||
if (err) {
|
||||
error(err, event, context);
|
||||
} else {
|
||||
response.send(event, context, response.SUCCESS);
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
delete p.ServiceToken;
|
||||
s3.putObject(p, function(err, data) {
|
||||
if (err) {
|
||||
error(err, event, context);
|
||||
} else {
|
||||
response.send(event, context, response.SUCCESS, { "data": data });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Puts items into DynamoDB, iterating over the list recursively.
|
||||
function putItems(items, tableName, event, context, itemsInserted) {
|
||||
if(items.length > 0){
|
||||
|
|
|
@ -152,7 +152,7 @@
|
|||
"S3Key": "lambda_functions/cloudformation-helpers.zip"
|
||||
},
|
||||
"Description": "Used to put objects into S3.",
|
||||
"Handler": "cloudformation_helpers.s3PutObject",
|
||||
"Handler": "aws/s3.putObject",
|
||||
"Role": {"Fn::GetAtt" : [ "S3PutObjectFunctionRole", "Arn" ] },
|
||||
"Runtime": "nodejs",
|
||||
"Timeout": 30
|
||||
|
|
54
test/aws/s3.putObect.template
Normal file
54
test/aws/s3.putObect.template
Normal file
|
@ -0,0 +1,54 @@
|
|||
{
|
||||
"AWSTemplateFormatVersion": "2010-09-09",
|
||||
"Parameters": {
|
||||
"CFHelperStackName": {
|
||||
"Type": "String",
|
||||
"Description": "The name of the stack where you installed the CloudFormation helper functions. See https://github.com/gilt/cloudformation-helpers."
|
||||
},
|
||||
"S3Bucket": {
|
||||
"Type": "String",
|
||||
"Description": "The name of the S3 bucket where to put the object. Must already exist."
|
||||
}
|
||||
},
|
||||
"Resources": {
|
||||
"CFHelperStack": {
|
||||
"Type": "AWS::CloudFormation::Stack",
|
||||
"Properties": {
|
||||
"TemplateURL": "https://s3.amazonaws.com/com.gilt.public.backoffice/cloudformation_templates/lookup_stack_outputs.template"
|
||||
}
|
||||
},
|
||||
"CFHelper": {
|
||||
"Type": "Custom::CFHelper",
|
||||
"Properties": {
|
||||
"ServiceToken": { "Fn::GetAtt" : ["CFHelperStack", "Outputs.LookupStackOutputsArn"] },
|
||||
"StackName": { "Ref": "CFHelperStackName" }
|
||||
},
|
||||
"DependsOn": [
|
||||
"CFHelperStack"
|
||||
]
|
||||
},
|
||||
"S3PutObject1": {
|
||||
"Type": "Custom::S3PutObject",
|
||||
"Properties": {
|
||||
"ServiceToken": { "Fn::GetAtt" : ["CFHelper", "S3PutObjectFunctionArn"] },
|
||||
"Bucket": { "Ref": "S3Bucket" },
|
||||
"Key": "empty-dir/"
|
||||
},
|
||||
"DependsOn": [
|
||||
"CFHelper"
|
||||
]
|
||||
},
|
||||
"S3PutObject2": {
|
||||
"Type": "Custom::S3PutObject",
|
||||
"Properties": {
|
||||
"ServiceToken": { "Fn::GetAtt" : ["CFHelper", "S3PutObjectFunctionArn"] },
|
||||
"Bucket": { "Ref": "S3Bucket" },
|
||||
"Key": "new-dir/test.txt",
|
||||
"Body": "Test data"
|
||||
},
|
||||
"DependsOn": [
|
||||
"CFHelper"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue