Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
154 views
in Technique[技术] by (71.8m points)

python - Load JSON data from CloudTrail into DynamoDB using Boto

I am working on a Boto3 script that can load the attributes from Cloudtrail into Dynamodb. The format of my cloudtrail logs is JSON. I am fairly new to DynamoDB and I am not sure where I am making a mistake. I'm trying to store "S3BucketName" as well as the name of the bucket which is "goodbucket3". Name for the cloudtrail is "GoodTrail".This is what I have come up with so far. I am getting this error "ResourceNotFoundException: An error occurred (ResourceNotFoundException) when calling the PutItem operation: Requested resource not found"

import boto3

dynamodb = boto3.resource('dynamodb')

table =dynamodb.create_table(
    TableName='GoodTable',
    AttributeDefinitions=[
    {
      "AttributeName": "S3BucketName",
      "AttributeType": "S"
    }
    ],
      KeySchema=[
    {
      "AttributeName": "S3BucketName",
      "KeyType": "HASH"
    }
  ],
  ProvisionedThroughput={
    "ReadCapacityUnits": 1,
    "WriteCapacityUnits": 1
  }
)

table = dynamodb.Table('GoodTable')
response = table.put_item(
    Item= {
        'S3BucketName': 'some-bucket-name', 
        'content': f'{path}',
    }
)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your table is just a dictionary. It is not dynamodb's table object.

To rectify the issue:

import boto3

dynamodb_res = boto3.resource('dynamodb')

table = dynamodb_res.Table('GoodTable')

response = table.put_item(
    Item= {
        'S3BucketName': 'some-bucket-name', 
        'content': f'{path}',
    }
)

print(response)

Based on your definition, you don't have any id. Your primary key in your table is S3BucketName, not id.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...