Set up the AWS CLI on Windows

Theoretically you can install the AWS CLI1 to import JSON files to DynamoDB.

Once you install this, you’ll want to add it to your path.

To connect, do this:

aws configure

To fill out the prompts, you’ll need to create a username/password.2. Then find the region name3.

To use DynamoDB, create a table, then create a policy with rights to add/select from the table4.

The policy will look like this:

{
 "Version": "2012-10-17",
 "Statement": [
  {
   "Effect": "Allow",
   "Action": [
    "dynamodb:BatchGetItem",
    "dynamodb:BatchWriteItem",
    "dynamodb:DeleteItem",
    "dynamodb:GetItem",
    "dynamodb:PutItem",
    "dynamodb:Query",
    "dynamodb:UpdateItem"
   ],
   "Resource": [
    "arn:aws:dynamodb:us-east-1:472846177579:table/talks"
   ],
   "Condition": {
    "ForAllValues:StringEquals": {
     "dynamodb:LeadingKeys": [
      "${www.amazon.com:user_id}"
     ]
    }
   }
  }
 ]
}

You can save this in the policy editor5, attach policy to the user you made earlier 6. Alternately, there is a permission called AmazonDynamoDBFullAccess that you can add.

Then, this will work:

aws dynamodb list-tables

Theoretically this should work:

aws dynamodb batch-write-item --request-items file://1.json

However, the AWS tool seems to suffer from the disastrous Python unicode problems:

UnicodeEncodeError: 'ascii' codec can't encode characters in position 3747-3748: ordinal not in range(128)

I went through a few dozen blog posts / Github tickets, and nothing solved it.

Rather, I found that once you get this far, it’s worth just writing Python scripts to upload data:

from __future__ import print_function
import boto3
import json
import decimal

# endpoint_url="http://localhost:8000"
dynamodb = boto3.resource('dynamodb', region_name='us-east-1')

table = dynamodb.Table('talks')

with open("d:\\projects\\image-annotation\\data\\talks\\json\\1\\1.json") as json_file:
  row = json.load(json_file, parse_float = decimal.Decimal)
  table.put_item(
    Item={
      'id_i': str(row['id_i'])
    }
  )
  1. http://docs.aws.amazon.com/cli/latest/userguide/installing.html#install-msi-on-windows []
  2. https://console.aws.amazon.com/iam/home#users []
  3. http://docs.aws.amazon.com/general/latest/gr/rande.html []
  4. https://console.aws.amazon.com/dynamodb/home?region=us-east-1#tables:selected=talks []
  5. https://console.aws.amazon.com/iam/home?#policies []
  6. https://console.aws.amazon.com/iam/home?#users/gary []

Leave a Reply

Your email address will not be published. Required fields are marked *