import boto3 import os import time from pathlib import Path os environ

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import boto3
import os
import time
from pathlib import Path
os.environ['AWS_PROFILE'] = 'research'
sqs_client = boto3.client('sqs', 'us-east-1')
QueueUrl='https://sqs.us-east-1.amazonaws.com/641113671333/lesson1_sergii_barabash'
sqs_client.send_message(
QueueUrl=QueueUrl,
MessageBody=f"message_1-1-1-1-1-1-1-1")
# TASK1: Read and print message from it
response = sqs_client.receive_message(
QueueUrl=QueueUrl,
AttributeNames=[
'SentTimestamp'
],
MaxNumberOfMessages=1,
MessageAttributeNames=[
'All'
],
VisibilityTimeout=0,
WaitTimeSeconds=0
)
message = response['Messages'][0]
receipt_handle = message['ReceiptHandle']
body = message['Body']
print(body)
# TASK2: Remove message from queue after you printed content
# Delete received message from queue
sqs_client.delete_message(
QueueUrl=QueueUrl,
ReceiptHandle=receipt_handle
)
print('Received and deleted message: %s' % message)
# TASK3: Read batch of 10 messages, put their content into a file
for i in range(10):
sqs_client.send_message(
QueueUrl='https://sqs.us-east-1.amazonaws.com/641113671333/lesson1_sergii_barabash',
MessageBody=f"message_{i}")
time.sleep(2)
message = sqs_client.receive_message(
QueueUrl='https://sqs.us-east-1.amazonaws.com/641113671333/lesson1_sergii_barabash',
MaxNumberOfMessages=10,
MessageAttributeNames=[
'All'
],
VisibilityTimeout=30
)
# print(json.dumps(message, indent=4))
# print(len(message['Messages']))
list_messages = []
list_messages = message["Messages"]
print(list_messages)
print(type(list_messages))
length = len(list_messages)
print(length)
with open("10_messages.txt", 'w') as f1:
for item in range(length):
f1.write("%s\n" % list_messages[item]["Body"])
# TASK4: Read a message with json {“name”: “file_name.txt”, “data”: “some data”} and create a file base on message
json_for_file_create = {"name": "file_name.txt", "data": "some data"}
with open(str(json_for_file_create['name']), 'w') as f2:
f2.write(str(json_for_file_create['data']))
# TASK5: Read a message with json {“s3path”: “s3://edge-research-tmp/sval/file1.txt”,
# “data”: “another text”} - upload file on s3 with defined content
json_for_file_upload_s3 = {"s3path": "s3://edge-research-tmp/sval/sergio_file.txt", "data": "another text111"}
file_name = str(json_for_file_upload_s3['s3path']).split('/')[-1:][0]
bucket_name = str(json_for_file_upload_s3['s3path']).split('/')[2]
sub_folder = str(json_for_file_upload_s3['s3path']).split('/')[3]
with open(file_name, 'w') as f3:
f3.write(str(json_for_file_upload_s3['data']))
s3_client = boto3.client('s3', 'us-east-1')
s3_client.upload_file(file_name, bucket_name, f"{sub_folder}/{file_name}")
print(f"file {file_name} was uploaded in bucket: {bucket_name}/{sub_folder}")