Saturday, October 3, 2020

How to Upload Excel File to Amazon S3 Bucket in Python

In this tutorial we will learn how to upload excel file to Amazon S3 bucket in python using Amazon's SDK Boto3. To complete this tutorial, we need to perform following steps.

  1. Get you access key and secret key from AWS management console.
  2. Install boto3 module.
  3. Write python code to upload file to Amazon S3 bucket.

1. Get your access key and secret key from AWS management console.


We assume you already have a Amazon Web Services account. If you don't have, create new account.
  1. Login to AWS management console.
  2. Click on your username at the top-right of the page to open the drop-down menu.
  3. Click on My Security Credentials. (Your Security Credential tab will open by default)
  4. Click on the Access keys (access key ID and secret access key)
  5. To create a new access key and secret, click on the Create New Access Key button.
  6. Download the .csv file containing your access key and secret.

2. Install boto3 module


Use pip as your package installer, run the below command in cmd.
pip install boto3
If you are working in virtual environment then make sure to activate your virtual environment first.

3. Write python code to upload file to Amazon S3 bucket.


# import module
import boto3
from botocore.exceptions import ClientError

# declare constants
AWS_ACCESS_KEY =  "xxxxxxxxxx"
AWS_SECRET_KEY =  "xxxxxxxxxxxxxxxxxxxxxxxx"
S3_BUCKET = 'bucket_name'


def upload_file_to_s3(file_name, bucket, object_name):
    """
    :param file_name: file name to upload
    :param bucket: S3 bucket
    :param object_name: S3 object name
    :return: True if successful upload else False
    """
    
    s3 = boto3.client("s3", aws_access_key_id=AWS_ACCESS_KEY,
                      aws_secret_access_key=AWS_SECRET_KEY)
    
    try:
        s3.upload_fileobj(
            file_name,
            bucket,
            object_name,
            ExtraArgs={"ContentType": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", 
                       "ContentDisposition": "attachment"}
        )
    except ClientError as e:
        return False
    return True

upload_file_to_s3(local_file_name, S3_BUCKET, s3_object_name)

Please let me know if you have any question, or have some better way to do this. :)

Tuesday, January 30, 2018

How To: Schedule Cron Job using python-crontab

We have already learnt how to schedule cron job manually. Now we will schedule cron job using python code.

Create an empty python file with .py entension.

Paste following code step by step:

# Create CronTab instance.
cron = CronTab(user=True) 


# Remove all previous jobs having comment id 'My_Job'
cron.remove_all(comment='My_Job')


# Schedule new job having comment id 'My_Job'
job = cron.new(comment='My_Job', command='/usr/bin/python3 /home/root/Schedular/scheduleCronJob.py >> /home/root/Schedular/Logs.txt')

# Set timeslot to run this job
job.setall(str(timeSlot))

# Write to file.
cron.write()



This is all, save file and run/execute this python file.

Install following required packages before execution of file.

pip3 install python-crontab
pip3 install schedule


If you have also installed crontab package then you may see some errors, to uninstall crontab use following command:

pip3 uninstall crontab


Upon successful execution, this will schedule a new cron job in ubuntu.

To list existing cron jobs enter following command in terminal window:

crontab –l


Click here for complete code.

How To: Schedule Cron Job in Ubuntu

Open a Terminal Window (Command Line) in ubuntu.
Type following command and press Enter.


 crontab -e


This will open editor for you. Write cron command at the end of file.

Use sudo if root privileges required. e.g


 sudo crontab -e


Use following pattern to create new cron job syntax:
  1. The number of minutes after the hour (0 to 59) 
  2. The hour in military time (24 hour) format (0 to 23) 
  3. The day of the month (1 to 31) 
  4. The month (1 to 12) 
  5. The day of the week (0 or 7 is Sun, or use name) 
  6. The command to run 

For example


 0 7 * * * /path/to/your/script.sh


This syntax will run script.sh at 7:00 AM daily.

File must have executable permissions. Run following command to make file executable.


  chmod +x /path/to/your/script.sh


If you want to schedule python file to run via cron, use following command instead.


  0 7 * * * /usr/bin/python3 /path/to/your/pythron-file.py


Just make sure you have already installed python3.

To list existing cron jobs enter following command:


 crontab -l


To remove an existing cron job enter following command:


 crontab -e


Delete the line that contains your cron job and save file.

Wednesday, June 28, 2017

How To: Create Thumbnail Image From Base64 Encoded String using Python 3

In this tutorial we 'll create thumbnail image from base64 encoded string received from client/user using python 3.
Our purpose is to create a thumbnail image for each image saved by client/user so that we can send back thumbnail images instead large size images while sending bulk data over the internet.

We are receiving image in form of base64 encoded string, We 'll apply following steps on it for complete result.

1). Decode string using base64 technique.
2). Create temporary image from decoded string.
3). Create thumbnail from this temporary image.
4). Encode thumbnail using base64 technique, so that we can save thumbnail as well in our database.
5). Remove both newly created temporary image and its thumbnail after saving to database.

1). Decode string using base64 technique.

# UserImage holds base64 encoded string. 
TmpUserImage = UserImage.replace("data:image/jpeg;base64,", "") 
ImgDataDecoded = base64.b64decode(TmpUserImage)

2). Create temporary image from decoded string.

# Using uuid for unique file name.
TmpUUID = str(uuid.uuid4()) 
# File name
FileName = TmpUUID + '_image.jpeg' 
# Thumbnail name
FileNameThumb = TmpUUID + '_image_80x80.jpeg' 
# Writing to file.
with open(FileName, 'wb') as f: 
   f.write(ImgDataDecoded)

3). Create thumbnail from this temporary image.

image = Image.open(FileName) 
size = (80, 80) 
thumb = ImageOps.fit(image, size, Image.ANTIALIAS) 
thumb.save(FileNameThumb)

4). Encode thumbnail using base64 technique, so that we can save thumbnail as well in our database.

with open(FileNameThumb, "rb") as thmbn: 
    TmpStr = base64.b64encode(thmbn.read()) 
TmpStr = "data:image/jpeg;base64," + str(TmpStr)[2:-1] 
# Encode to utf-8 before saving to database 
# (don't forget to decode after fetching from database).
TmpStr = bytes(TmpStr,"utf-8")
# Now save TmpStr to database.

5). Remove both newly created temporary image and its thumbnail after saving to database.

if os.path.isfile(FileName): 
    os.remove(FileName) 
if os.path.isfile(FileNameThumb): 
    os.remove(FileNameThumb)

Don't forget to include following libraries.

# For Thumbnail 
from PIL import Image, ImageOps 
import base64, uuid, os

This is all, you have done it. Please ask in comment if there is any confusion. Happy Coding -:)

Sunday, May 28, 2017

How To: Set and Get Cursor Position in Textbox using JQuery

In this tutorial, we 'll learn how to set and get cursor position in text box using jquery.
1). Open notepad, and paste following html code in it.

<html>
<head>
 <title>Home</title>
</head>
<body>
 <input id="first_name" type="text" value="webdesignpluscode" />
 <input onclick="SetPosition();" type="button" value="Set Position" />
 <input onclick="GetPosition();" type="button" value="Get Position" />
</body>
</html>

2). Click File then Save As... in notepad menu. Save As dialogue will open
3). Enter "home.html" in File name:
4). Select All Files in Save as type:
5). Click Save. Notepad file will be saved as html page.
6). Now open this html page in edit mode.
7). Add following online JQuery library reference inside <head> tag.

<script src="https://code.jquery.com/jquery-1.10.2.js" type="text/javascript"></script>

You can download and add to project locally.

8). Now add java script block inside <head> tag and copy following code there.


<script type="text/javascript">

// Set Cursor Position
 $.fn.setCursorPosition = function (position)
{
 this.each(function (index, elem) {
 if (elem.setSelectionRange) {
 elem.setSelectionRange(position, position);
 }
 else if (elem.createTextRange) {
 var range = elem.createTextRange();
 range.collapse(true);
 range.moveEnd('character', position);
 range.moveStart('character', position);
 range.select();
 }
 });
 return this;
 };

 // Get cursor position
 $.fn.getCursorPosition = function ()
{
 var el = $(this).get(0);
 var position = 0;
 if ('selectionStart' in el) {
 position = el.selectionStart;
 }
 else if ('selection' in document) {
 el.focus();
 var Sel = document.selection.createRange();
 var SelLength = document.selection.createRange().text.length;
 Sel.moveStart('character', -el.value.length);
 position = Sel.text.length - SelLength; }
 return position;
 };

 function SetPosition() {
 $("#first_name").setCursorPosition(5);
 $("#first_name").focus();
 }

 function GetPosition() {
 alert($("#first_name").getCursorPosition());
 $("#first_name").focus();
 }
 </script>

Complete code can be downloaded from this git repository.

This is all. You have done. Save file, and view it in any browser.