-
Notifications
You must be signed in to change notification settings - Fork 0
/
Introduction_to_AWS_Boto_in_Python.py
664 lines (477 loc) · 18.7 KB
/
Introduction_to_AWS_Boto_in_Python.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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
##############################
# Putting Files in the Cloud #
##############################
### Boto3 client
# Generate the boto3 client for interacting with S3
s3 = boto3.client('s3', region_name='us-east-1',
# Set up AWS credentials
aws_access_key_id=AWS_KEY_ID,
aws_secret_access_key=AWS_SECRET)
# List the buckets
buckets = s3.list_buckets()
# Print the buckets
print(buckets)
### Multiple clients
# Generate the boto3 client for interacting with S3 and SNS
s3 = boto3.client('s3', region_name='us-east-1',
aws_access_key_id=AWS_KEY_ID,
aws_secret_access_key=AWS_SECRET)
sns = boto3.client('sns', region_name='us-east-1',
aws_access_key_id=AWS_KEY_ID,
aws_secret_access_key=AWS_SECRET)
# List S3 buckets and SNS topics
buckets = s3.list_buckets()
topics = sns.list_topics()
# Print out the list of SNS topics
print(topics)
### Creating a bucket
import boto3
# Create boto3 client to S3
s3 = boto3.client('s3', region_name='us-east-1',
aws_access_key_id=AWS_KEY_ID,
aws_secret_access_key=AWS_SECRET)
# Create the buckets
response_staging = s3.create_bucket(Bucket='gim-staging')
response_processed = s3.create_bucket(Bucket='gim-processed')
response_test = s3.create_bucket(Bucket='gim-test')
# Print out the response
print(response_staging)
### Listing buckets
# Get the list_buckets response
response = s3.list_buckets()
# Iterate over Buckets from .list_buckets() response
for bucket in response['Buckets']:
# Print the Name for each bucket
print(bucket['Name'])
### Deleting a bucket
# Delete the gim-test bucket
s3.delete_bucket(Bucket='gim-test')
# Get the list_buckets response
response = s3.list_buckets()
# Print each Buckets Name
for bucket in response['Buckets']:
print(bucket['Name'])
### Deleting multiple buckets
# Get the list_buckets response
response = s3.list_buckets()
# Delete all the buckets with 'gim', create replacements.
for bucket in response['Buckets']:
if 'gim' in bucket['Name']:
s3.delete_bucket(Bucket=bucket['Name'])
s3.create_bucket(Bucket='gid-staging')
s3.create_bucket(Bucket='gid-processed')
# Print bucket listing after deletion
response = s3.list_buckets()
for bucket in response['Buckets']:
print(bucket['Name'])
### Putting files in the cloud
# Upload final_report.csv to gid-staging
s3.upload_file(Filename='final_report.csv',
# Set filename and key
Bucket='gid-staging',
Key='2019/final_report_01_01.csv')
# Get object metadata and print it
response = s3.head_object(Bucket='gid-staging',
Key='2019/final_report_01_01.csv')
# Print the size of the uploaded object
print(response['ContentLength'])
### Spring cleaning
# List only objects that start with '2018/final_'
response = s3.list_objects(Bucket='gid-staging',
Prefix='2018/final_')
# Iterate over the objects
if 'Contents' in response:
for obj in response['Contents']:
# Delete the object
s3.delete_object(Bucket='gid-staging', Key=obj['Key'])
# Print the remaining objects in the bucket
response = s3.list_objects(Bucket='gid-staging')
for obj in response['Contents']:
print(obj['Key'])
##########################
# Sharing Files Securely #
##########################
### Uploading a public report
# Upload the final_report.csv to gid-staging bucket
s3.upload_file(
# Complete the filename
Filename='./final_report.csv',
# Set the key and bucket
Key='2019/final_report_2019_02_20.csv',
Bucket='gid-staging',
# During upload, set ACL to public-read
ExtraArgs = {
'ACL': 'public-read'})
### Making multiple files public
# List only objects that start with '2019/final_'
response = s3.list_objects(
Bucket='gid-staging', Prefix='2019/final_')
# Iterate over the objects
for obj in response['Contents']:
# Give each object ACL of public-read
s3.put_object_acl(Bucket='gid-staging',
Key=obj['Key'],
ACL='public-read')
# Print the Public Object URL for each object
print("https://{}.s3.amazonaws.com/{}".format( 'gid-staging', obj['Key']))
### Generating a presigned URL
# Generate presigned_url for the uploaded object
share_url = s3.generate_presigned_url(
# Specify allowable operations
ClientMethod='get_object',
# Set the expiration time
ExpiresIn=3600,
# Set bucket and shareable object's name
Params={'Bucket': 'gid-staging','Key': 'final_report.csv'}
)
# Print out the presigned URL
print(share_url)
### Opening a private file
df_list = [ ]
for file in response['Contents']:
# For each file in response load the object from S3
obj = s3.get_object(Bucket='gid-requests', Key=file['Key'])
# Load the object's StreamingBody with pandas
obj_df = pd.read_csv(obj['Body'])
# Append the resulting DataFrame to list
df_list.append(obj_df)
# Concat all the DataFrames with pandas
df = pd.concat(df_list)
# Preview the resulting DataFrame
df.head()
### Generate HTML table from Pandas
# Generate an HTML table with no border and selected columns
services_df.to_html('./services_no_border.html',
# Keep specific columns only
columns=['service_name', 'link'],
# Set border
border=0)
# Generate an html table with border and all columns.
services_df.to_html('./services_border_all_columns.html',
border=1,
render_links=True)
### Upload an HTML file to S3
# Upload the lines.html file to S3
s3.upload_file(Filename='lines.html',
# Set the bucket name
Bucket='datacamp-public', Key='index.html',
# Configure uploaded file
ExtraArgs = {
# Set proper content type
'ContentType':'text/html',
# Set proper ACL
'ACL': 'public-read'})
# Print the S3 Public Object URL for the new file.
print("http://{}.s3.amazonaws.com/{}".format('datacamp-public', 'index.html'))
### Combine daily requests for February
df_list = []
# Load each object from s3
for file in request_files:
s3_day_reqs = s3.get_object(Bucket='gid-requests',
Key=file['Key'])
# Read the DataFrame into pandas, append it to the list
day_reqs = pd.read_csv(s3_day_reqs['Body'])
df_list.append(day_reqs)
# Concatenate all the DataFrames in the list
all_reqs = pd.concat(df_list)
# Preview the DataFrame
all_reqs.head()
### Upload aggregated reports for February
# Write agg_df to a CSV and HTML file with no border
agg_df.to_csv('./feb_final_report.csv')
agg_df.to_html('./feb_final_report.html', border=0)
# Upload the generated CSV to the gid-reports bucket
s3.upload_file(Filename='./feb_final_report.csv',
Key='2019/feb/final_report.html', Bucket='gid-reports',
ExtraArgs = {'ACL': 'public-read'})
# Upload the generated HTML to the gid-reports bucket
s3.upload_file(Filename='./feb_final_report.html',
Key='2019/feb/final_report.html', Bucket='gid-reports',
ExtraArgs = {'ContentType': 'text/html',
'ACL': 'public-read'})
### Update index to include February
# List the gid-reports bucket objects starting with 2019/
objects_list = s3.list_objects(Bucket='gid-reports', Prefix='2019/')
# Convert the response contents to DataFrame
objects_df = pd.DataFrame(objects_list['Contents'])
# Create a column "Link" that contains Public Object URL
base_url = "http://gid-reports.s3.amazonaws.com/"
objects_df['Link'] = base_url + objects_df['Key']
# Preview the resulting DataFrame
objects_df.head()
### Upload the new index
# Write objects_df to an HTML file
objects_df.to_html('report_listing.html',
# Set clickable links
render_links=True,
# Isolate the columns
columns=['Link', 'LastModified', 'Size'])
# Overwrite index.html key by uploading the new file
s3.upload_file(
Filename='./report_listing.html', Key='index.html',
Bucket='gid-reports',
ExtraArgs = {
'ContentType': 'text/html',
'ACL': 'public-read'
})
###########################
# Reporting and Notifying #
###########################
### Creating a Topic
# Initialize boto3 client for SNS
sns = boto3.client('sns',
region_name='us-east-1',
aws_access_key_id=AWS_KEY_ID,
aws_secret_access_key=AWS_SECRET)
# Create the city_alerts topic
response = sns.create_topic(Name="city_alerts")
c_alerts_arn = response['TopicArn']
# Re-create the city_alerts topic using a oneliner
c_alerts_arn_1 = sns.create_topic(Name="city_alerts")['TopicArn']
# Compare the two to make sure they match
print(c_alerts_arn == c_alerts_arn_1)
### Creating multiple topics
# Create list of departments
departments = ['trash', 'streets', 'water']
for dept in departments:
# For every department, create a general topic
sns.create_topic(Name="{}_general".format(dept))
# For every department, create a critical topic
sns.create_topic(Name="{}_critical".format(dept))
# Print all the topics in SNS
response = sns.list_topics()
print(response['Topics'])
### Deleting multiple topics
# Get the current list of topics
topics = sns.list_topics()['Topics']
for topic in topics:
# For each topic, if it is not marked critical, delete it
if "critical" not in topic['TopicArn']:
sns.delete_topic(TopicArn=topic['TopicArn'])
# Print the list of remaining critical topics
print(sns.list_topics()['Topics'])
### Subscribing to topics
# Subscribe Elena's phone number to streets_critical topic
resp_sms = sns.subscribe(
TopicArn = str_critical_arn,
Protocol ='sms', Endpoint ="+16196777733")
# Print the SubscriptionArn
print(resp_sms['SubscriptionArn'])
# Subscribe Elena's email to streets_critical topic.
resp_email = sns.subscribe(
TopicArn = str_critical_arn,
Protocol='email', Endpoint ="eblock@sandiegocity.gov")
# Print the SubscriptionArn
print(resp_email['SubscriptionArn'])
### Creating multiple subscriptions
# For each email in contacts, create subscription to street_critical
for email in contacts['Email']:
sns.subscribe(TopicArn = str_critical_arn,
# Set channel and recipient
Protocol = 'email',
Endpoint = email)
# List subscriptions for streets_critical topic, convert to DataFrame
response = sns.list_subscriptions_by_topic(
TopicArn = str_critical_arn)
subs = pd.DataFrame(response['Subscriptions'])
# Preview the DataFrame
subs.head()
### Deleting multiple subscriptions
# List subscriptions for streets_critical topic.
response = sns.list_subscriptions_by_topic(
TopicArn = str_critical_arn)
# For each subscription, if the protocol is SMS, unsubscribe
for sub in response['Subscriptions']:
if sub['Protocol'] == 'sms':
sns.unsubscribe(SubscriptionArn=sub['SubscriptionArn'])
# List subscriptions for streets_critical topic in one line
subs = sns.list_subscriptions_by_topic(
TopicArn=str_critical_arn)['Subscriptions']
# Print the subscriptions
print(subs)
### Sending an alert
# If there are over 100 potholes, create a message
if streets_v_count > 100:
# The message should contain the number of potholes.
message = "There are {} potholes!".format(streets_v_count)
# The email subject should also contain number of potholes
subject = "Latest pothole count is {}".format(streets_v_count)
# Publish the email to the streets_critical topic
sns.publish(
TopicArn = str_critical_arn,
# Set subject and message
Message = message,
Subject = subject
)
### Sending a single SMS message
# Loop through every row in contacts
for idx, row in contacts.iterrows():
# Publish an ad-hoc sms to the user's phone number
response = sns.publish(
# Set the phone number
PhoneNumber = str(row['Phone']),
# The message should include the user's name
Message = 'Hello {}'.format(row['Name'])
)
print(response)
### Creating multi-level topics
dept_arns = {}
for dept in departments:
# For each deparment, create a critical topic
critical = sns.create_topic(Name="{}_critical".format(dept))
# For each department, create an extreme topic
extreme = sns.create_topic(Name="{}_extreme".format(dept))
# Place the created TopicARNs into a dictionary
dept_arns['{}_critical'.format(dept)] = critical['TopicArn']
dept_arns['{}_extreme'.format(dept)] = extreme['TopicArn']
# Print the filled dictionary.
print(dept_arns)
### Different protocols per topic level
for index, user_row in contacts.iterrows():
# Get topic names for the users's dept
critical_tname = '{}_critical'.format(user_row['Department'])
extreme_tname = '{}_extreme'.format(user_row['Department'])
# Get or create the TopicArns for a user's department.
critical_arn = sns.create_topic(Name=critical_tname)['TopicArn']
extreme_arn = sns.create_topic(Name=extreme_tname)['TopicArn']
# Subscribe each users email to the critical Topic
sns.subscribe(TopicArn = critical_arn,
Protocol ='email', Endpoint =user_row['Email'])
# Subscribe each users phone number for the extreme Topic
sns.subscribe(TopicArn = extreme_arn,
Protocol ='sms', Endpoint =str(user_row['Phone']))
### Sending multi-level alerts
if vcounts['water'] > 100:
# If over 100 water violations, publish to water_critical
sns.publish(
TopicArn = dept_arns['water_critical'],
Message = "{} water issues".format(vcounts['water']),
Subject = "Help fix water violations NOW!")
if vcounts['water'] > 300:
# If over 300 violations, publish to water_extreme
sns.publish(
TopicArn = dept_arns['water_extreme'],
Message = "{} violations! RUN!".format(vcounts['water']),
Subject = "THIS IS BAD. WE ARE FLOODING!")
#######################
# Pattern Rekognition #
#######################
### Cat detector
# Use Rekognition client to detect labels
image1_response = rekog.detect_labels(
# Specify the image as an S3Object; Return one label
Image=image1, MaxLabels=1)
# Print the labels
print(image1_response['Labels'])
# Use Rekognition client to detect labels
image2_response = rekog.detect_labels(
# Specify the image as an S3Object; Return one label
Image=image2, MaxLabels=1)
# Print the labels
print(image2_response['Labels'])
### Multiple cat detector
# Create an empty counter variable
cats_count = 0
# Iterate over the labels in the response
for label in response['Labels']:
# Find the cat label, look over the detected instances
if label['Name'] == 'Cat':
for instance in label['Instances']:
# Only count instances with confidence > 85
if (instance['Confidence'] > 85):
cats_count += 1
# Print count of cats
print(cats_count)
### Parking sign reader
# Create empty list of words
words = []
# Iterate over the TextDetections in the response dictionary
for text_detection in response['TextDetections']:
# If TextDetection type is WORD, append it to words list
if text_detection['Type'] == 'WORD':
# Append the detected text
words.append(text_detection['DetectedText'])
# Print out the words list
print(words)
# Create empty list of lines
lines = []
# Iterate over the TextDetections in the response dictionary
for text_detection in response['TextDetections']:
# If TextDetection type is Line, append it to lines list
if text_detection['Type'] == 'LINE':
# Append the detected text
lines.append(text_detection['DetectedText'])
# Print out the words list
print(lines)
### Detecting language
# For each dataframe row
for index, row in dumping_df.iterrows():
# Get the public description field
description =dumping_df.loc[index, 'public_description']
if description != '':
# Detect language in the field content
resp = comprehend.detect_dominant_language(Text=description)
# Assign the top choice language to the lang column.
dumping_df.loc[index, 'lang'] = resp['Languages'][0]['LanguageCode']
# Count the total number of spanish posts
spanish_post_ct = len(dumping_df[dumping_df.lang == 'es'])
# Print the result
print("{} posts in Spanish".format(spanish_post_ct))
### Translating Get It Done requests
for index, row in dumping_df.iterrows():
# Get the public_description into a variable
description = dumping_df.loc[index, 'public_description']
if description != '':
# Translate the public description
resp = translate.translate_text(
Text=description,
SourceLanguageCode='auto', TargetLanguageCode='en')
# Store original language in original_lang column
dumping_df.loc[index, 'original_lang'] = resp['SourceLanguageCode']
# Store the translation in the translated_desc column
dumping_df.loc[index, 'translated_desc'] = resp['TranslatedText']
# Preview the resulting DataFrame
dumping_df = dumping_df[['service_request_id', 'original_lang', 'translated_desc']]
dumping_df.head()
### Getting request sentiment
for index, row in dumping_df.iterrows():
# Get the translated_desc into a variable
description = dumping_df.loc[index, 'public_description']
if description != '':
# Get the detect_sentiment response
response = comprehend.detect_sentiment(
Text=description,
LanguageCode='en')
# Get the sentiment key value into sentiment column
dumping_df.loc[index, 'sentiment'] = response['Sentiment']
# Preview the dataframe
dumping_df.head()
### Scooter community sentiment
for index, row in scooter_requests.iterrows():
# For every DataFrame row
desc = scooter_requests.loc[index, 'public_description']
if desc != '':
# Detect the dominant language
resp = comprehend.detect_dominant_language(Text=desc)
lang_code = resp['Languages'][0]['LanguageCode']
scooter_requests.loc[index, 'lang'] = lang_code
# Use the detected language to determine sentiment
scooter_requests.loc[index, 'sentiment'] = comprehend.detect_sentiment(
Text=desc,
LanguageCode=lang_code)['Sentiment']
# Perform a count of sentiment by group.
counts = scooter_requests.groupby(['sentiment', 'lang']).count()
counts.head()
### Scooter dispatch
# Get topic ARN for scooter notifications
topic_arn = sns.create_topic(Name='scooter_notifications')['TopicArn']
for index, row in scooter_requests.iterrows():
# Check if notification should be sent
if (row['sentiment'] == 'NEGATIVE') & (row['img_scooter'] == 1):
# Construct a message to publish to the scooter team.
message = "Please remove scooter at {}, {}. Description: {}".format(
row['long'], row['lat'], row['public_description'])
# Publish the message to the topic!
sns.publish(TopicArn = topic_arn,
Message = message,
Subject = "Scooter Alert")