0

I am using boto 2.45 and Python 2.7. Here is the code:

import time

import boto
from boto.dynamodb2.layer1 import DynamoDBConnection
from boto.dynamodb2.table import Table
from boto.dynamodb2.fields import HashKey, RangeKey, KeysOnlyIndex,GlobalAllIndex

access_key = 'XXX'
secret_key = 'YYY'


conn = boto.dynamodb2.connect_to_region(
'us-east-2',
aws_access_key_id=access_key,         
aws_secret_access_key=secret_key
)

table = Table('Table1', connection=conn);
table.put_item(data={
    'name':'aaa.rar',
    'time':int(time.time()),
    'size':200000000000,
    'title':'from boto'
})

Everytime I run this code, it always creates a new item in the table with name 'aaa.rar'. Table's primary key is name. This is not what I want. I want to raise an exception if an item with the same name already exists in the table.

fhcat
  • 971
  • 2
  • 9
  • 28
  • Update: I think the problem probably is the time column. I made it a primary sort key. I deleted the table and recreated it with no sort key, and now the code works as expected, though I don't quite understand why a sort key can affect the query. – fhcat Feb 08 '17 at 19:33
  • It's more involved than that - when you have a sort-key, it means the Primary-key is a "composite-key". Anyway, the answer is at this other question. https://stackoverflow.com/questions/37053595/how-do-i-conditionally-insert-an-item-into-a-dynamodb-table-using-boto3 – John C Sep 12 '17 at 13:40
  • Possible duplicate of [How do I conditionally insert an item into a dynamodb table using boto3](https://stackoverflow.com/questions/37053595/how-do-i-conditionally-insert-an-item-into-a-dynamodb-table-using-boto3) – John C Sep 12 '17 at 13:40

0 Answers0