Skip to content

Commit b4a1c8d

Browse files
committed
Consolidating from the boto3-examples repo
1 parent 958561a commit b4a1c8d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+221
-78
lines changed

Pipfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ botocore = "*"
1010
[dev-packages]
1111

1212
[requires]
13-
python_version = "3.8"
13+
python_version = "3.12"

Pipfile.lock

-77
This file was deleted.

ec2/python3/describe-instances.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import boto3
2+
3+
ec2 = boto3.client('ec2')
4+
5+
response = ec2.describe_instances()
6+
7+
for r in response['Reservations']:
8+
for i in r['Instances']:
9+
print(i['InstanceId'])

ec2/python3/launch-ec2.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import boto3
2+
3+
client = boto3.client('ec2')
4+
5+
response = client.run_instances(
6+
ImageId='ami-02354e95b39ca8dec',
7+
InstanceType='t2.micro',
8+
KeyName='nem2p-cs4740',
9+
SecurityGroupIds=[
10+
'sg-0c5cf2cca6833f090',
11+
],
12+
# SubnetId='subnet-b39b21c5',
13+
DryRun=False,
14+
MinCount=1,
15+
MaxCount=1,
16+
InstanceInitiatedShutdownBehavior='terminate',
17+
TagSpecifications=[
18+
{
19+
'ResourceType': 'instance',
20+
'Tags': [
21+
{
22+
'Key': 'Name',
23+
'Value': 'boto3-created-instance'
24+
},
25+
]
26+
},
27+
]
28+
)
29+
30+
print(response)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

s3/python3/README.md

+2

s3/python3/download.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import boto3
2+
3+
s3 = boto3.client('s3')
4+
s3.download_file('my-bucket', 'OBJECT_NAME', 'FILE_NAME')

s3/python3/list_buckets.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import boto3
2+
import json
3+
4+
s3 = boto3.client('s3')
5+
6+
response = s3.list_buckets()
7+
8+
for x in response['Buckets']:
9+
print(x['Name'])
10+

s3/python3/list_objects.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import boto3
2+
s3 = boto3.resource('s3')
3+
4+
my_bucket = s3.Bucket('bucket_name')
5+
6+
for file in my_bucket.objects.all():
7+
print(file.key)

s3/python3/upload.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import logging
2+
import boto3
3+
from botocore.exceptions import ClientError
4+
import os
5+
6+
7+
def upload_file(file_name, bucket, object_name=None):
8+
"""Upload a file to an S3 bucket
9+
10+
:param file_name: File to upload
11+
:param bucket: Bucket to upload to
12+
:param object_name: S3 object name. If not specified then file_name is used
13+
:return: True if file was uploaded, else False
14+
"""
15+
16+
# If S3 object_name was not specified, use file_name
17+
if object_name is None:
18+
object_name = os.path.basename(file_name)
19+
20+
# Upload the file
21+
s3_client = boto3.client('s3')
22+
try:
23+
response = s3_client.upload_file(file_name, bucket, object_name)
24+
except ClientError as e:
25+
logging.error(e)
26+
return False
27+
return True

sns/python3/publish-sns.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env python3
2+
3+
import boto3
4+
5+
sns = boto3.client('sns')
6+
7+
response = sns.publish(
8+
TopicArn="arn:aws:sns:us-east-1:440848399208:cs4740-notification",
9+
Message="Hey there everybody, how's it going today?",
10+
Subject="Testing testing"
11+
)
12+
print(response)
File renamed without changes.
File renamed without changes.

sqs/python3/create_queue.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env python3
2+
3+
import boto3
4+
5+
sqs = boto3.client('sqs')
6+
7+
response = sqs.create_queue(
8+
QueueName='demo-queue',
9+
)
10+
print(response)

sqs/python3/delete_queue.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env python3
2+
3+
import boto3
4+
5+
sqs = boto3.client('sqs')
6+
7+
response = sqs.delete_queue(
8+
QueueUrl='https://queue.amazonaws.com/440848399208/demo-queue'
9+
)
10+
print(response)

sqs/python3/get_queue_attributes.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env python3
2+
3+
import boto3
4+
5+
sqs = boto3.client('sqs')
6+
7+
def check_message_count():
8+
response = sqs.get_queue_attributes(
9+
QueueUrl='https://queue.amazonaws.com/440848399208/demo-queue',
10+
AttributeNames=[
11+
'All',
12+
]
13+
)
14+
anom = response['Attributes']['ApproximateNumberOfMessages']
15+
if ( int(anom) >=1 ):
16+
print("Count: " + anom + " Greater than or = to 1!")
17+
else:
18+
print("No messages available.")
19+
print(response)
20+
21+
check_message_count()

sqs/python3/purge_queue.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env python3
2+
3+
import boto3
4+
5+
sqs = boto3.client('sqs')
6+
7+
response = sqs.purge_queue(
8+
QueueUrl='https://queue.amazonaws.com/440848399208/demo-queue'
9+
)
10+
print(response)
File renamed without changes.

sqs/python3/receive_messages.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env python3
2+
3+
import boto3
4+
5+
sqs = boto3.client('sqs')
6+
7+
def check_message_count():
8+
response = sqs.get_queue_attributes(
9+
QueueUrl='https://queue.amazonaws.com/440848399208/demo-queue',
10+
AttributeNames=[
11+
'All',
12+
]
13+
)
14+
anom = response['Attributes']['ApproximateNumberOfMessages']
15+
if ( int(anom) >=1 ):
16+
print("Count: " + anom + " Greater than or = to 1!")
17+
get_message()
18+
else:
19+
print("No messages available.")
20+
21+
22+
def delete_message(handle):
23+
response = sqs.delete_message(
24+
QueueUrl='https://queue.amazonaws.com/440848399208/demo-queue',
25+
ReceiptHandle=handle
26+
)
27+
28+
def get_message():
29+
try:
30+
response = sqs.receive_message(
31+
QueueUrl='https://queue.amazonaws.com/440848399208/demo-queue',
32+
AttributeNames=['All'],
33+
MessageAttributeNames=[
34+
'project',
35+
],
36+
MaxNumberOfMessages=1,
37+
VisibilityTimeout=30,
38+
WaitTimeSeconds=20,
39+
)['Messages'][0]
40+
messageid = response['MessageId']
41+
handle = response['ReceiptHandle']
42+
project = response['MessageAttributes']['project']['StringValue']
43+
print("MessageID: " + messageid)
44+
# print("Handle: " + handle)
45+
print("Project: " + project)
46+
print(response)
47+
delete_message(handle)
48+
except:
49+
print("Nothing to see here")
50+
51+
check_message_count()
File renamed without changes.

sqs/python3/send_message.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env python3
2+
3+
import boto3
4+
5+
sqs = boto3.client('sqs')
6+
7+
response = sqs.send_message(
8+
QueueUrl='https://queue.amazonaws.com/440848399208/demo-queue',
9+
MessageBody='body goes here 12345',
10+
MessageAttributes={
11+
'project': {
12+
'StringValue': 'research',
13+
'DataType': 'String'
14+
}
15+
}
16+
)
17+
print(response)

0 commit comments

Comments
 (0)