In this article, we will be looking at SDKs that can be used for file uploading in python applications. These SDKs make it easier for you to perform file uploads by providing an API for uploading images, videos, and any other kind of file to the cloud.
Table of Contents
Filestack Python SDK
Filestack provides a Python SDK for a faster, more reliable file uploading experience. It’s over 3x faster than other file upload solutions. For example, it’s capable of uploading a 100MB file in about 6 seconds, which is 18 seconds less than the upload time of normal solutions. Filestack is also reliable, having a 99.999% upload success rate and being able to handle poor network conditions.
It also supports cloud uploads as well as uploading from different sources. When it comes to helping users have a seamless file upload experience, Filestack’s SDK for Python does the job.
What’s even better is that it can be integrated and implemented easily. You can learn how to do so by following the steps below.
Installing
Install filestack
with pip
pip install filestack-python
or directly from GitHub
pip install git+https://github.com/filestack/filestack-python.git
Uploading
The Filestack SDK allows you to upload and handle filelinks using two main classes: Client and Filelink.
Uploading files with filestack.Client
from filestack import Client
client = Client('<YOUR_API_KEY>')
new_filelink = client.upload(filepath='path/to/file')
print(new_filelink.url)
Uploading files using Filestack Intelligent Ingestion
To upload files using Filestack Intelligent Ingestion, simply add intelligent=True
argument
new_filelink = client.upload(filepath='path/to/file', intelligent=True)
Filstack always uses multipart uploads. In case of network issues, it will dynamically split file parts into smaller chunks (sacrificing upload speed in favor of upload reliability).
Working with Filelinks
Filelink objects can be created by uploading new files or by initializing filestack.Filelink with an already existing file handle
from filestack import Filelink, Client
client = Client('<APIKEY>')
filelink = client.upload(filepath='path/to/file')
filelink.url # 'https://cdn.filestackcontent.com/FILE_HANDLE
# work with previously uploaded file
filelink = Filelink('FILE_HANDLE')
Basic Filelink Functions
With a Filelink, you can download to a local path or get the content of a file. You can also perform various transformations.
file_content = new_filelink.get_content()
size_in_bytes = new_filelink.download('/path/to/file')
filelink.overwrite(filepath='path/to/new/file')
filelink.resize(width=400).flip()
filelink.delete()
Cloudinary Python SDK
Cloudinary offers an API for uploading pictures, videos, and other types of files to the cloud. When files are uploaded to Cloudinary, they are securely backed up in the cloud and have a revision history. The secure uploading capabilities of Cloudinary’s APIs are available from your servers, directly from visitors’ browsers or mobile applications, or obtained from distant public URLs.
The Python SDK for Cloudinary encapsulates the upload API and makes integration easier. Python view helper methods are available for uploading straight from a browser to Cloudinary, and Python methods are available for doing simple Python image and video uploads to the cloud.
The following section describes typical practices for uploading Python images and videos to Cloudinary.
Prerequisites
You’ll need the following steps:
- An account for Cloudinary Programmable Media.
- Your login information. Your Cloudinary console’s Dashboard page is where you may discover your login information.
- A supported version of Python in an environment that supports it.
Set up and configure the SDK
In a terminal in your Python3 environment, run the following code:
pip3 install cloudinary
pip3 install python-dotenv
In your project, create a file called .env containing your API environment variable from your account credentials:
.env file
# Copy and paste your API environment variable
# =============================================
CLOUDINARY_URL=cloudinary://<api_key>:<api_secret>@<cloud_name>
In your project, create a new file called my_file.py. Copy and paste the following into this file:
my_file.py
# Set your Cloudinary credentials
# ==============================
from dotenv import load_dotenv
load_dotenv()
# Import the Cloudinary libraries
# ==============================
import cloudinary
import cloudinary.uploader
import cloudinary.api
# Import to format the JSON responses
# ==============================
import json
# Set configuration parameter: return "https" URLs by setting secure=True
# ==============================
config = cloudinary.config(secure=True)
# Log the configuration
# ==============================
print("****1. Set up and configure the SDK:****\nCredentials: ", config.cloud_name, config.api_key, "\n")
Upload an image
Copy and paste this into my_file.py:
# my_file.py (continued)
def uploadImage():
# Upload the image and get its URL
# ==============================
# Upload the image.
# Set the asset's public ID and allow overwriting the asset with new versions
cloudinary.uploader.upload("https://cloudinary-devs.github.io/cld-docs-assets/assets/images/butterfly.jpeg", public_id="quickstart_butterfly", unique_filename = False, overwrite=True)
# Build the URL for the image and save it in the variable 'srcURL'
srcURL = cloudinary.CloudinaryImage("quickstart_butterfly").build_url()
# Log the image URL to the console.
# Copy this URL in a browser tab to generate the image on the fly.
print("****2. Upload an image****\nDelivery URL: ", srcURL, "\n")
Get and use details of the image
Copy and paste this into my_file.py:
def getAssetInfo():
# Get and use details of the image
# ==============================
# Get image details and save it in the variable 'image_info'.
image_info=cloudinary.api.resource("quickstart_butterfly")
print("****3. Get and use details of the image****\nUpload response:\n", json.dumps(image_info,indent=2), "\n")
# Assign tags to the uploaded image based on its width. Save the response to the update in the variable 'update_resp'.
if image_info["width"]>900:
update_resp=cloudinary.api.update("quickstart_butterfly", tags = "large")
elif image_info["width"]>500:
update_resp=cloudinary.api.update("quickstart_butterfly", tags = "medium")
else:
update_resp=cloudinary.api.update("quickstart_butterfly", tags = "small")
# Log the new tag to the console.
print("New tag: ", update_resp["tags"], "\n")
Transform the image
Copy and paste this into my_file.py:
# my_file.py (continued)
def createImageTag():
# Transform the image
# ==============================
# Create an image tag with transformations applied to the src URL.
imageTag = cloudinary.CloudinaryImage("quickstart_butterfly").image(radius="max", effect="sepia")
# Log the image tag to the console
print("****4. Transform the image****\nTransfrmation URL: ", imageTag, "\n")
Run your code
Copy and paste this into my_file.py:
# my_file.py (continued)
def main():
uploadImage()
getAssetInfo()
createImageTag()
main();
In the terminal, run the following command:
python3 my_file.py
The following original image is uploaded to your Cloudinary account, tagged appropriately and accessible via the URL shown below.
The transformed version of the image is accessible via the URL shown below.
Original image
http://res.cloudinary.com/<cloud-name>/image/upload/v1/quickstart_butterfly
Transformed image
http://res.cloudinary.com/<cloud-name>/image/upload/e_sepia/r_max/v1/quickstart_butterfly
AWS SDK
There are two ways to upload a file to an S3 bucket via the AWS SDK for Python:
The first way is by using the upload_file method where the file name, bucket name, and object name can be sent to. By breaking up large files into smaller pieces and concurrently uploading each piece, this approach can manage large files.
import logging
import boto3
from botocore.exceptions import ClientError
import os
def upload_file(file_name, bucket, object_name=None):
"""Upload a file to an S3 bucket
:param file_name: File to upload
:param bucket: Bucket to upload to
:param object_name: S3 object name. If not specified then file_name is used
:return: True if file was uploaded, else False
"""
# If S3 object_name was not specified, use file_name
if object_name is None:
object_name = os.path.basename(file_name)
# Upload the file
s3_client = boto3.client('s3')
try:
response = s3_client.upload_file(file_name, bucket, object_name)
except ClientError as e:
logging.error(e)
return False
return True
The next way is by using the upload_fileobj method, which accepts a readable file-like object. Binary mode, not text mode, must be used to open the file object.
s3 = boto3.client('s3')
with open("FILE_NAME", "rb") as f:
s3.upload_fileobj(f, "BUCKET_NAME", "OBJECT_NAME")
The S3 Client, Bucket, and Object classes all support the upload_file and upload_fileobj methods. Each class provides the same method capabilities. There are no advantages to using a method from one class over another. Use the class that is most convenient for you.