Skip to content

Instantly share code, notes, and snippets.

@ShakataGaNai
Last active June 5, 2018 19:56
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save ShakataGaNai/6027b4c684c294f3fcef to your computer and use it in GitHub Desktop.
Save ShakataGaNai/6027b4c684c294f3fcef to your computer and use it in GitHub Desktop.
console.log('Loading event');
var AWS = require('aws-sdk');
var dynamodb = new AWS.DynamoDB();
exports.handler = function(event, context) {
console.log("Request received:\n", JSON.stringify(event));
console.log("Context received:\n", JSON.stringify(context));
var tableName = "OurBlogDemo";
var datetime = new Date().getTime().toString();
dynamodb.putItem({
"TableName": tableName,
"Item": {
"device": {
"S": event.device
},
"timedate": {
"N": datetime
},
"latitude": {
"N": event.lat
},
"longitude": {
"N": event.lon
}
}
}, function(err, data) {
if (err) {
context.fail('ERROR: Dynamo failed: ' + err);
} else {
console.log('Dynamo Success: ' + JSON.stringify(data, null, ' '));
context.succeed('SUCCESS');
}
});
}
@barrettclark
Copy link

Thanks for the example. Here is an updated version. I changed the table name to devices. The DynamoDB setup has changed a little. I just went with a simple key on device.

console.log('Loading event');
var doc = require('dynamodb-doc');
var dynamodb = new doc.DynamoDB();

exports.handler = function(event, context) {
    console.log("Request received:\n", JSON.stringify(event));
    console.log("Context received:\n", JSON.stringify(context));

    var tableName = "devices";
    var datetime = new Date().getTime().toString();
    var item = {
        "device": event.device, 
        "timedate": datetime,
        "latitude": event.lat,
        "longitude": event.lon
    };
    console.log("Item:\n", item);

    dynamodb.putItem({
            "TableName": tableName,
            "Item": item
        }, function(err, data) {
            if (err) {
                context.fail('ERROR: Dynamo failed: ' + err);
            } else {
                console.log('Dynamo Success: ' + JSON.stringify(data, null, '  '));
                context.succeed('SUCCESS');
            }
        });
}

@oraclepeopletools
Copy link

Thank You so much for this-it has enormous value when combined with additional tutorials

@Divyaganapathy
Copy link

Hi, thanks for the code. I want to know how to store and retrieve photo url in dynamodb from s3?

@mi5guided
Copy link

Wow - your blog and this code was crazy helpful. Thank you!

@cameck
Copy link

cameck commented Sep 29, 2016

Thanks for this, great example. I never thought of using API gateway. I'm just curious what you are doing if any records fail for whatever reason to insert into DynamoDB?

@imewish
Copy link

imewish commented Oct 26, 2016

Hello,

Im trying to pass some params like this,

const params = {
        uuid: generateUUID().uuid,
         status: "new",
         owner: r.format.filename.split("/")[3],
         file_size: r.format.size,
         duration: r.format.duration,
         master_file: {
            duration: r.format.duration,
            format: {
                bitrate: r.format.bitrate
            }
         }
}


but when it comes to table,

format: {
bitrate: r.format.bitrate
}

it comes as empty.

Any idea why?

helps would be appreciated.

screen shot 2016-10-26 at 4 18 19 pm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment