Skip to main content

Posts

Showing posts from November, 2020

Use Linux Bash to query or automate listing the contents of an S3 bucket.

Below is a Bash script that will query a bucket with the current date specified on the system. Querying or listing the contents of the S3 bucket is quite a good strategy. Let's say if you need to make sure that important backups are copied or present on S3 bucket.  Or you need to monitor what are the contents being dumped to a specific S3 bucket. Automating querying S3 contents in Linux using Bash scripting is a good idea. Below is a code that works fine on a Linux system. #!/bin/bash varDate=$(date +%Y-%m-%d) aws s3api list-objects --bucket "name_of-the-bucket" --prefix sub_directory_bucket --query 'Contents[?LastModified>=`'${varDate}'`][].{Key: Key, Size: Size, LastModified: LastModified}' >> /tmp/s3_server_files_$varDate.txt The script can be configured on a cron task and runs on a daily basis then the date is automatically supplied by the bash script and data is saved on /tmp/ and file will not be overwritten since filename is based on date.

Python basic if else and or comparison code

Comparing input or variables is a quite a common task to do when creating programs. In Python it can be done in a lot of ways, this example code below shows how it’s being done using “if else” and “or” statement. Here’s the example code below: #Get user input requires Python 3 input1 = input("1. Type something to test this out : ") input2 = input("2. Type something to test this out : ") input3 = input("3. Type something to test this out : ")   #variable initialization is important or else it will result to NameError: name 'variable_name' is not defined checkinput1 = 0 checkinput2 = 0 checkinput3 = 0 noinput = 1   #int(input1) convert the input1 string to an integer if (int(input1) > 40):   checkinput1 = "1" elif ( int(input2) > 40):   checkinput2 = "1" elif ( int(input3) > 40):   checkinput3 = "1" else:   noinput = 0   if (checkinput1 or checkinput2 or checkinput3 ==