Python With Aws Notes
Python With Aws Notes
• Install:
• Basic Usage:
import boto3
s3 = boto3.client('s3')
s3.upload_file('local_file.txt', 'bucket-name', 'remote_file.txt')
🔹 2. S3 File Upload/Download
s3 = boto3.client('s3')
s3.upload_file('test.png', 'my-bucket', 'images/test.png')
s3.download_file('my-bucket', 'images/test.png', 'downloaded_test.png')
🔹 3. DynamoDB CRUD
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('Users')
# Insert
response = table.put_item(Item={'UserID': '123', 'Name': 'Alice'})
# Read
item = table.get_item(Key={'UserID': '123'})
# Update
table.update_item(
Key={'UserID': '123'},
UpdateExpression='SET Name = :val1',
ExpressionAttributeValues={':val1': 'Bob'}
)
1
# Delete
table.delete_item(Key={'UserID': '123'})
sqs = boto3.client('sqs')
queue_url = 'https://sqs.us-east-1.amazonaws.com/123456789012/my-queue'
# Send
sqs.send_message(QueueUrl=queue_url, MessageBody='Hello from Python')
# Receive
messages = sqs.receive_message(QueueUrl=queue_url, MaxNumberOfMessages=1)
sns = boto3.client('sns')
sns.publish(
TopicArn='arn:aws:sns:us-east-1:123456789012:my-topic',
Message='Hello from Python!',
Subject='Test SNS'
)
2
🔹 7. Read Secrets from AWS Secrets Manager
client = boto3.client('secretsmanager')
secret_value = client.get_secret_value(SecretId='my-secret-id')
print(secret_value['SecretString'])
🔹 8. CloudWatch Logs
logs = boto3.client('logs')
response = logs.create_log_stream(
logGroupName='my-group',
logStreamName='my-stream'
)
sts = boto3.client('sts')
creds = sts.assume_role(RoleArn='arn:aws:iam::123456789012:role/MyRole',
RoleSessionName='MySession')['Credentials']
Let me know if you'd like project-specific Python AWS examples (e.g., ETL with S3 + Lambda + Glue).