I have the downloadable DynamoDB local(currently working). I have read their documents and their example code is working.
I created the Users table with the name "Users"
Here is the UsersCreateTable.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<script src="https://sdk.amazonaws.com/js/aws-sdk-2.7.16.min.js"></script>
<script>
AWS.config.update({
region: "us-west-2",
endpoint: 'http://localhost:8000/',
// accessKeyId default can be used while using the downloadable version of DynamoDB.
// For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead.
accessKeyId: "fakeMyKeyId",
// secretAccessKey default can be used while using the downloadable version of DynamoDB.
// For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead.
secretAccessKey: "fakeSecretAccessKey"
});
var dynamodb = new AWS.DynamoDB();
function createUsers() {
var params = {
TableName : "Users",
KeySchema: [
{ AttributeName: "id", KeyType: "HASH"}
],
AttributeDefinitions: [
{ AttributeName: "id", AttributeType: "N" }
],
ProvisionedThroughput: {
ReadCapacityUnits: 5,
WriteCapacityUnits: 5
}
};
dynamodb.createTable(params, function(err, data) {
if (err) {
document.getElementById('textarea').innerHTML = "Unable to create users table: " + "\n" + JSON.stringify(err, undefined, 2);
} else {
document.getElementById('textarea').innerHTML = "Created users table: " + "\n" + JSON.stringify(data, undefined, 2);
}
});
}
</script>
<title></title>
</head>
<body>
<input id="createTableButton" type="button" value="Create Table" onclick="createUsers();" />
<br><br>
<textarea readonly id="textarea" style="width:400px; height:800px"></textarea>
</body>
</html>
I have prepared the sample JSON data for our Users Table. I have modified their MoviesLoadTable.html file and converted it to UsersLoadTable.html which uploads JSON data to the DynamoDB local, in order to load my JSON Users Data.
When I try to load my JSON data I get these errors on console:
Uncaught SyntaxError: Unexpected token : in JSON at position 497
at JSON.parse (<anonymous>)
at FileReader.r.onload (UsersLoadData.html:31)
r.onload @ UsersLoadData.html:31
FileReader (async)
processFile @ UsersLoadData.html:53
And here is the UsersLoadData.html
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<script src="https://sdk.amazonaws.com/js/aws-sdk-2.7.16.min.js"></script>
<script type="text/javascript">
AWS.config.update({
region: "us-west-2",
endpoint: 'http://localhost:8000/',
// accessKeyId default can be used while using the downloadable version of DynamoDB.
// For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead.
accessKeyId: "fakeMyKeyId",
// secretAccessKey default can be used while using the downloadable version of DynamoDB.
// For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead.
secretAccessKey: "fakeSecretAccessKey"
});
var docClient = new AWS.DynamoDB.DocumentClient();
function processFile(evt) {
document.getElementById('textarea').innerHTML = "";
document.getElementById('textarea').innerHTML += "Importing users into DynamoDB. Please wait..." + "\n";
var file = evt.target.files[0];
if (file) {
var r = new FileReader();
r.onload = function(e) {
var contents = e.target.result;
var allUsers = JSON.parse(contents);
allUsers.forEach(function (user) {
document.getElementById('textarea').innerHTML += "Processing user id: " + user.id + "\n";
var params = {
TableName: "Users",
Item: {
"id": user.id,
"info": user.info
}
};
docClient.put(params, function (err, data) {
if (err) {
document.getElementById('textarea').innerHTML += "Unable to add user: " + count + user.id + "\n";
document.getElementById('textarea').innerHTML += "Error JSON: " + JSON.stringify(err) + "\n";
} else {
document.getElementById('textarea').innerHTML += "Loading succeeded(id): " + user.id + "\n";
textarea.scrollTop = textarea.scrollHeight;
}
});
});
};
r.readAsText(file);
} else {
alert("Could not read users data file");
}
}
</script>
<title></title>
</head>
<body>
<input type="file" id="fileinput" accept='application/json' />
<br><br>
<textarea readonly id="textarea" style="width:400px; height:800px"></textarea>
<script>
document.getElementById('fileinput').addEventListener('change', processFile, false);
</script>
</body>
</html>
I searched for the error and could not find a satisfying solution. Thank you for your help.
