Last active
January 14, 2022 13:13
-
-
Save ahmed-abdelazim/c42a81f2e0c63aac7882c4c28fd1bee3 to your computer and use it in GitHub Desktop.
Export / import AWS dynamodb table from json file with correct data types using python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import print_function # Python 2/3 compatibility | |
import json | |
import boto3 | |
# AWS_ACCESS = "" | |
# AWS_SECRET = "" | |
AWS_REGION = "eu-west-3" | |
TABLE_NAME = "users" | |
client = boto3.client( | |
'dynamodb', | |
# aws_secret_access_key=AWS_SECRET, | |
# aws_access_key_id=AWS_ACCESS, | |
region_name=AWS_REGION) | |
response = client.scan( | |
TableName=TABLE_NAME, | |
Select='ALL_ATTRIBUTES', | |
ReturnConsumedCapacity='TOTAL' | |
) | |
# print(json.dumps(response)) | |
with open(TABLE_NAME+'.json', 'w') as f: | |
print(json.dumps(response), file=f) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import print_function # Python 2/3 compatibility | |
import json | |
import boto3 | |
from pprint import pprint | |
from decimal import Decimal | |
# AWS_ACCESS = "" | |
# AWS_SECRET = "" | |
AWS_REGION = "eu-west-3" | |
TABLE_NAME = "users" | |
# Build New dict with Correct Datatypes | |
"""The method to deserialize the DynamoDB data types. | |
:param value: A DynamoDB value to be deserialized to a pythonic value. | |
Here are the various conversions: | |
DynamoDB Python | |
-------- ------ | |
{'NULL': True} None | |
{'BOOL': True/False} True/False | |
{'N': str(value)} Decimal(str(value)) | |
{'S': string} string | |
{'B': bytes} Binary(bytes) | |
{'NS': [str(value)]} set([Decimal(str(value))]) | |
{'SS': [string]} set([string]) | |
{'BS': [bytes]} set([bytes]) | |
{'L': list} list | |
{'M': dict} dict | |
:returns: The pythonic value of the DynamoDB type. | |
""" | |
# https://boto3.amazonaws.com/v1/documentation/api/latest/_modules/boto3/dynamodb/types.html | |
with open(TABLE_NAME+'.json', 'r') as f: | |
distros_dict = json.load(f) | |
dynamodb = boto3.resource( | |
'dynamodb', | |
# aws_secret_access_key=AWS_SECRET, | |
# aws_access_key_id=AWS_ACCESS, | |
region_name=AWS_REGION) | |
table = dynamodb.Table(TABLE_NAME) | |
mylist = distros_dict['Items'] | |
for mydict in mylist: | |
boto3.resource('dynamodb') | |
deserializer = boto3.dynamodb.types.TypeDeserializer() | |
python_data = {k: deserializer.deserialize(v) for k, v in mydict.items()} | |
pprint(python_data) | |
response = table.put_item(Item=python_data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment