-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublish_message.py
66 lines (66 loc) · 2.11 KB
/
publish_message.py
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
import boto3
import json
import env
import os
from receive_message import receive_message
from delete_message import delete_message
"""
check sqs notification
"""
sqs_url = env.sqs_url
max_number_of_messages = 1
wait_time_seconds = 20
region = env.region
response = receive_message(sqs_url, max_number_of_messages, wait_time_seconds, region)
messages_list = response.get('Messages', [])
print(f"Number of messages received: {len(messages_list)}")
check = False
for message in messages_list:
message_body_str = message["Body"]
message_body_dict = json.loads(message_body_str)
check = message_body_dict['Records'][0]['eventSource']
print(check)
if check=='aws:s3':
"""
connect ro s3 services
"""
client_s3 = boto3.client(
's3',
region,
aws_access_key_id=env.accessKey,
aws_secret_access_key=env.secretKey,
)
data_file_folder = os.path.join(os.getcwd(),'data Files')
"""
download files from bucket_a
"""
s3 = boto3.resource('s3')
for s3_object in s3.Bucket(env.bucket_a).objects.all():
filename = s3_object.key
client_s3.download_file(env.bucket_a,s3_object.key,os.path.join(data_file_folder, filename))
"""
upload files to bucket_b
"""
for file in os.listdir(data_file_folder):
print('Uploading file {0} ...'.format(file))
client_s3.upload_file(
os.path.join(data_file_folder, file),
env.bucket_b,
file
)
"""
Delete Message Example
"""
max_number_of_messages = 5
wait_time_seconds = 20
response = receive_message(sqs_url, max_number_of_messages, wait_time_seconds, region)
messages_list = response.get('Messages', [])
# Deleteing certain message
for message in messages_list:
message_body_str = message["Body"]
message_body_dict = json.loads(message_body_str)
receipt_handle = message['ReceiptHandle']
response = delete_message(sqs_url, receipt_handle, region)
print(response)
else:
print("there is no uploaded files on s3 , invalid resource")