I have walked through the steps of importing a S3 csv to a new dynamodb table in the AWS console successfully. But, when trying to with boto3 I'm getting the error 'DynamoDB' object has no attribute 'import_table'
. Typically you would just need to set the table but since the import_table
creates a new table I'm not sure what is needed.
I use the self.db_client
to delete the old table prior to importing as this import will happen on a reoccurring schedule but that shouldn't affect anything to my knowledge.
self.db_client = self.session.client("dynamodb")
def create_table_from_csv(self) -> dict:
"""Function to import csv from s3 to dynamodb
Args:
none
Returns:
"""
try:
self.logger.info(
"Import report from S3 to new dynamodb table: {}".format(
self.table_name
)
)
response = self.db_client.import_table(
S3BucketSource={
"S3Bucket": self.bucket_name,
"S3KeyPrefix": self.report_csv_key,
},
InputFormat="CSV",
TableCreationParameters={
"TableName": self.table_name,
"AttributeDefinitions": [
{"AttributeName": "instanceArn", "AttributeType": "S"},
{"AttributeName": "instanceName", "AttributeType": "S"},
],
"KeySchema": [
{"AttributeName": "instanceArn", "KeyType": "HASH"},
{"AttributeName": "instanceName", "KeyType": "Range"},
],
"BillingMode": "PROVISIONED",
"ProvisionedThroughput": {
"ReadCapacityUnits": 5,
"WriteCapacityUnits": 5,
},
"SSESpecification": {
"Enabled": True,
"SSEType": "KMS",
"KMSMasterKeyId": self.table_key_arn,
},
},
)
except ClientError as error:
self.logger.error(
"Error: failed to upload report({}) from s3 to new dynamodb table".format(
self.report_csv_key
)
)
self.logger.error("Error message:" + str(error))
return response
You don't state which version of boto3 you are using. Import table functionality is approx 1 year old,so you should ensure you are using one of the latest boto3 packages.
You must be on at least version 1.24.55
https://github.com/boto/boto3/blob/develop/CHANGELOG.rst#12455
import boto3
self.db_client = boto3.client('dynamodb')