Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get the notification configuration details of a S3 bucket using Boto3 and AWS Client?
Problem Statement − Use boto3 library in Python to get the notification configuration of a S3 bucket. For example, find the notification configuration details of Bucket_1 in S3.
Approach/Algorithm to solve this problem
Step 1 − Import boto3 and botocore exceptions to handle exceptions.
Step 2 − Use bucket_name as the parameter in the function.
Step 3 − Create an AWS session using boto3 library.
Step 4 − Create an AWS client for S3.
Step 5 − Now use the function get_bucket_notification_configuration and pass the bucket name.
Step 6 − It returns the dictionary containing the details about S3. If notification is not set, then it returns NONE
Step 7 − Handle the generic exception if something went wrong while deleting the file.
Example
Use the following code to get the notification configuration details −
import boto3
from botocore.exceptions import ClientError
def get_bucket_notificationconfiguration_of_s3(bucket_name):
session = boto3.session.Session()
s3_client = session.client('s3')
try:
result = s3_client.get_bucket_notification_configuration(Bucket=bucket_name,)
except ClientError as e:
raise Exception( "boto3 client error in get_bucket_notificationconfiguration_of_s3: " + e.__str__())
except Exception as e:
raise Exception( "Unexpected error in get_bucket_notificationconfiguration_of_s3 function: " + e.__str__())
return result
print(get_bucket_notificationconfiguration_of_s3("Bucket_1"))
Output
{
'TopicConfigurations': [
{
'Id': 'string',
'TopicArn': 'string',
'Events': [
's3:ReducedRedundancyLostObject'|'s3:ObjectCreated:*'|'s3:ObjectCreated: Put'|'s3:ObjectCreated:Post'|'s3:ObjectCreated:Copy'|'s3:ObjectCreated:C
ompleteMultipartUpload'|'s3:ObjectRemoved:*'|'s3:ObjectRemoved:Delete'|'
s3:ObjectRemoved:DeleteMarkerCreated'|'s3:ObjectRestore:*'|'s3:ObjectRes
tore:Post'|'s3:ObjectRestore:Completed'|'s3:Replication:*'|'s3:Replicati
on:OperationFailedReplication'|'s3:Replication:OperationNotTracked'|'s3:
Replication:OperationMissedThreshold'|'s3:Replication:OperationReplicate
dAfterThreshold',
],
'Filter': {
'Key': {
'FilterRules': [
{
'Name': 'prefix'|'suffix',
'Value': 'string'
},
]
}
}
},
],
'QueueConfigurations': [
{
'Id': 'string',
'QueueArn': 'string',
'Events': [
's3:ReducedRedundancyLostObject'|'s3:ObjectCreated:*'|'s3:ObjectCreated:
Put'|'s3:ObjectCreated:Post'|'s3:ObjectCreated:Copy'|'s3:ObjectCreated:C
ompleteMultipartUpload'|'s3:ObjectRemoved:*'|'s3:ObjectRemoved:Delete'|'
s3:ObjectRemoved:DeleteMarkerCreated'|'s3:ObjectRestore:*'|'s3:ObjectRes
tore:Post'|'s3:ObjectRestore:Completed'|'s3:Replication:*'|'s3:Replicati
on:OperationFailedReplication'|'s3:Replication:OperationNotTracked'|'s3:
Replication:OperationMissedThreshold'|'s3:Replication:OperationReplicate
dAfterThreshold',
],
'Filter': {
'Key': {
'FilterRules': [
{
'Name': 'prefix'|'suffix',
'Value': 'string'
},
]
}
}
},
],
'LambdaFunctionConfigurations': [
{
'Id': 'string',
'LambdaFunctionArn': 'string',
'Events': [
's3:ReducedRedundancyLostObject'|'s3:ObjectCreated:*'|'s3:ObjectCreated:
Put'|'s3:ObjectCreated:Post'|'s3:ObjectCreated:Copy'|'s3:ObjectCreated:C
ompleteMultipartUpload'|'s3:ObjectRemoved:*'|'s3:ObjectRemoved:Delete'|'
s3:ObjectRemoved:DeleteMarkerCreated'|'s3:ObjectRestore:*'|'s3:ObjectRes
tore:Post'|'s3:ObjectRestore:Completed'|'s3:Replication:*'|'s3:Replicati
on:OperationFailedReplication'|'s3:Replication:OperationNotTracked'|'s3:
Replication:OperationMissedThreshold'|'s3:Replication:OperationReplicate
dAfterThreshold',
],
'Filter': {
'Key': {
'FilterRules': [
{
'Name': 'prefix'|'suffix',
'Value': 'string'
},
]
}
}
},
]
}
Note: This output depends on the settings/permissions of bucket. It omits the result if some settings/permissions are default or not set. Similarly, if notification is not set for bucket then it returns NONE.