Skip to content

Instantly share code, notes, and snippets.

@ahmed-abdelazim
Last active January 14, 2022 13:13
Show Gist options
  • Save ahmed-abdelazim/c42a81f2e0c63aac7882c4c28fd1bee3 to your computer and use it in GitHub Desktop.
Save ahmed-abdelazim/c42a81f2e0c63aac7882c4c28fd1bee3 to your computer and use it in GitHub Desktop.

Revisions

  1. ahmed-abdelazim revised this gist Mar 8, 2020. 1 changed file with 5 additions and 30 deletions.
    35 changes: 5 additions & 30 deletions import.py
    Original file line number Diff line number Diff line change
    @@ -33,34 +33,6 @@
    """
    # https://boto3.amazonaws.com/v1/documentation/api/latest/_modules/boto3/dynamodb/types.html


    def corrcet_type(post):
    for field in post:
    innerdict = post[field]
    dtype = ''.join(innerdict.keys())
    if dtype == 'NULL':
    post[field] = None
    if dtype == 'BOOL':
    if ''.join(innerdict.values()) == 'True':
    post[field] = True
    else:
    post[field] = False
    if dtype == 'N':
    post[field] = Decimal(''.join(innerdict.values()))
    if dtype == 'S':
    post[field] = ''.join(innerdict.values())
    if dtype == 'L':
    post[field] = innerdict.values()
    if dtype == 'M':
    post[field] = innerdict.values()
    if dtype == 'NS':
    post[field] = set([Decimal(str(''.join(innerdict.values())))])
    if dtype == 'SS':
    post[field] = set(''.join(innerdict.values()))
    if dtype == 'BS':
    post[field] = set(''.join(innerdict.values()))


    with open(TABLE_NAME+'.json', 'r') as f:
    distros_dict = json.load(f)

    @@ -73,5 +45,8 @@ def corrcet_type(post):
    table = dynamodb.Table(TABLE_NAME)
    mylist = distros_dict['Items']
    for mydict in mylist:
    corrcet_type(mydict)
    response = table.put_item(Item=mydict)
    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)
  2. ahmed-abdelazim revised this gist Mar 8, 2020. No changes.
  3. ahmed-abdelazim created this gist Mar 8, 2020.
    23 changes: 23 additions & 0 deletions export.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    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)
    77 changes: 77 additions & 0 deletions import.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,77 @@
    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


    def corrcet_type(post):
    for field in post:
    innerdict = post[field]
    dtype = ''.join(innerdict.keys())
    if dtype == 'NULL':
    post[field] = None
    if dtype == 'BOOL':
    if ''.join(innerdict.values()) == 'True':
    post[field] = True
    else:
    post[field] = False
    if dtype == 'N':
    post[field] = Decimal(''.join(innerdict.values()))
    if dtype == 'S':
    post[field] = ''.join(innerdict.values())
    if dtype == 'L':
    post[field] = innerdict.values()
    if dtype == 'M':
    post[field] = innerdict.values()
    if dtype == 'NS':
    post[field] = set([Decimal(str(''.join(innerdict.values())))])
    if dtype == 'SS':
    post[field] = set(''.join(innerdict.values()))
    if dtype == 'BS':
    post[field] = set(''.join(innerdict.values()))


    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:
    corrcet_type(mydict)
    response = table.put_item(Item=mydict)