How I Migrated From Sirv To ImageKit Using Python

How I Migrated From Sirv To ImageKit Using Python
5 min read Sat May 07 2022

Ever since I started using this domain, I have always been hosting my images on Sirv. It's an image CDN which optimizes your images and delivers them faster.

Why leave Sirv

Ever since I started blogging, I have been using Sirv to optimize the images on my website and it has been great. It contained all I ever expected plus more; optimizing images, resizing and cropping images via url, adding overlays to images, caching and more. The reason why I decided to migrate away from Sirv is that, the monthly bandwidth on their free plan wasn’t sufficient for my website. At first, it was very okay for me but as my website started growing and I started getting more and more visitors, the bandwidth usage increased.

The free plan provides 500 MB of storage and a monthly bandwidth of 2GB.

The bandwidth in a few days

The image above is the bandwidth usage form April 30 - May 6 and it’s 1.37 GB, a few megabytes to 2GB so I had to look for another option. I would have been okay if I upgraded my plan to the business plan but am not yet working so I have to look for free alternatives which will suit my use case. After searching and searching, the best alternatives was ImageKit which offered 20 GB storage and 20GB bandwidth, just what I was looking for 😌.

Coding The Migration Script

As a developer, I tend to automate most stuff. I could have migrated image by image but that would take a century before am done so I decided to use Python to quicken the process.

How The Script Will Work

Flowchart diagram of how program will work

The image above depicts how our program is going to work. First, we will get a list of all our images on Sirv, then we iterate through each file and upload it to ImageKit, very simple.

Getting list of files

The first method is very easy, we are going to make Sirv export a CSV file which will contain a list of all the files on their server. Head over to your My files directory on Sirv. Then you right-click an empty area and select “Export file list to CSV”.

Getting file list

Now, this will download a CSV file onto your device. Opening the file with a CSV editor should display something similar to this.

CSV file list

The CSV file which is downloaded should contain content similar to this.

Installing Required Packages

Obviously we will need to install some packages in order to make our script work. The only package which we will be installing is the ImageKit SDK.

In order to install it, just open your terminal and paste in the following code.

pip install imagekitio

After the package has been successfully installed, create a new Python file and paste the following code inside.

import csv
from imagekitio import ImageKit

imagekit = ImageKit(
	private_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
	public_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
	url_endpoint = "https://ik.imagekit.io/your_endpoint/"
	)



with open("file-list.csv") as csv_file:
	csv_reader = csv.DictReader(csv_file)
	for row in csv_reader:
		line = dict(row)
		folder = line["File"].split("/")[:-1]
		folder.append("")
		folder = "/".join(folder)
		if line['Type'] != "folder":
			imagekit.upload_file(
				file = line["URL"],
				file_name = line["Name"],
				options = {
					"use_unique_file_name": False,
					"folder": folder
				}
				)
		print("Uploaded " + line["Name"])

The code above is all you will need. First of all, we imported the csv and ImageKit packages. Then we created an instance of ImageKit, you must replace private_key andpublic_key with your accounts private key and public key respectively. Then you also replace the url_endpoint with your accounts endpoint.

On the next line, we opened the CSV file, (I renamed the CSV file downloaded from Sirv to ‘file-list.csv’) and created a csv reader.

Then, we iterate through the rows in the CSV file and upload the files to ImageKit.

Outro

So this is how I migrated all my images to ImageKit from Sirv. Following the method above will also migrate your images to ImageKit. If you face any problems, do make sure to write it out in the comments section.

👋

Share this article

Comments