Skip to content

Instantly share code, notes, and snippets.

@LAripping
Created August 19, 2022 21:51
Show Gist options
  • Save LAripping/3119af3513058fb2b0e325b47e6f9d04 to your computer and use it in GitHub Desktop.
Save LAripping/3119af3513058fb2b0e325b47e6f9d04 to your computer and use it in GitHub Desktop.
Quick & Dirty script to migrate entries from the originally-linked, non-SAM table to the fancy new one, marking test entries in the process
import json
import boto3
import botocore
import requests
myIP = requests.get('http://ifconfig.me').text
client = boto3.client('dynamodb', region_name='eu-west-2')
scan_resp = client.scan(TableName='Visitors')
for i in scan_resp['Items']:
try:
putitem_resp = client.put_item(
TableName='VisitorsSam',
Item=i,
ConditionExpression='attribute_not_exists(IP) and attribute_not_exists(UA)'
)
print("Added")
except botocore.exceptions.ClientError as ce:
if ce.response['Error']['Code'] == 'ConditionalCheckFailedException':
print("Visitor details already in the database. Not added")
if i['IP']['S']==myIP: # mark all entries from my IP as "test"
client.update_item(
TableName="VisitorsSam",
Key={
"IP": {
"S": myIP
},
"UA": {
"S" : i['UA']['S']
}
},
UpdateExpression="SET #T = :t",
ExpressionAttributeNames={
'#T': 'test'
},
ExpressionAttributeValues={
':t': {
'BOOL': True
}
}
)
print("Updated item")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment