Use the Boto3 `create_bucket` method to create an S3 bucket.
import boto3 s3 = boto3.client('s3') bucket_name = "my-new-bucket" s3.create_bucket(Bucket=bucket_name)
You can use the `upload_file` method to upload files to S3.
import boto3 s3 = boto3.client('s3') bucket_name = "my-existing-bucket" file_path = "path/to/file.txt" s3.upload_file(file_path, bucket_name, 'file.txt')
The `download_file` method can be used to download files from S3.
import boto3 s3 = boto3.client('s3') bucket_name = "my-existing-bucket" file_key = "file.txt" output_file = "downloaded_file.txt" s3.download_file(bucket_name, file_key, output_file)
Use the `list_buckets` method to retrieve a list of all S3 buckets.
import boto3 s3 = boto3.client('s3') buckets = s3.list_buckets() for bucket in buckets['Buckets']: print(bucket['Name'])
Use the `copy` method to copy objects from one bucket to another.
import boto3 s3 = boto3.client('s3') copy_source = { 'Bucket': 'source-bucket', 'Key': 'source-file.txt' } s3.copy(copy_source, 'destination-bucket', 'destination-file.txt')
Use the `generate_presigned_url` method to generate a temporary URL for accessing an S3 object.
import boto3 s3 = boto3.client('s3') url = s3.generate_presigned_url('get_object', Params={'Bucket': 'my-bucket', 'Key': 'file.txt'}, ExpiresIn=3600) print(url)
Use the `delete_object` method to remove an object from an S3 bucket.
import boto3 s3 = boto3.client('s3') s3.delete_object(Bucket='my-bucket', Key='file.txt')
Use the `list_objects_v2` method to list objects within an S3 bucket.
import boto3 s3 = boto3.client('s3') response = s3.list_objects_v2(Bucket='my-bucket') for content in response.get('Contents', []): print(content['Key'])
You can create an SNS topic using the `create_topic` method.
import boto3 sns = boto3.client('sns') response = sns.create_topic(Name='my-new-topic') print(response['TopicArn'])
Use the `publish` method to send a message to an SNS topic.
import boto3 sns = boto3.client('sns') sns.publish( TopicArn='arn:aws:sns:us-east-1:123456789012:my-new-topic', Message='Hello from Boto3!' )
Use the `subscribe` method to add a subscription to an SNS topic.
import boto3 sns = boto3.client('sns') sns.subscribe( TopicArn='arn:aws:sns:us-east-1:123456789012:my-new-topic', Protocol='email', Endpoint='example@example.com' )
Use the `list_subscriptions_by_topic` method to list subscriptions for a specific topic.
import boto3 sns = boto3.client('sns') response = sns.list_subscriptions_by_topic(TopicArn='arn:aws:sns:us-east-1:123456789012:my-new-topic') for subscription in response['Subscriptions']: print(subscription['SubscriptionArn'])
Use the `delete_topic` method to delete an SNS topic.
import boto3 sns = boto3.client('sns') sns.delete_topic(TopicArn='arn:aws:sns:us-east-1:123456789012:my-new-topic')
Use the `create_queue` method to create an SQS queue.
import boto3 sqs = boto3.client('sqs') response = sqs.create_queue(QueueName='my-new-queue') print(response['QueueUrl'])
You can use the `send_message` method to send a message to an SQS queue.
import boto3 sqs = boto3.client('sqs') queue_url = 'https://sqs.us-east-1.amazonaws.com/123456789012/my-new-queue' sqs.send_message(QueueUrl=queue_url, MessageBody='Hello from Boto3!')
Use the `receive_message` method to get messages from an SQS queue.
<