Upload Files to Your Site by URL in Python

You can easily import a file by its URL on your back end in Python; here’s how.

Just like you’d upload files in Python from local storage, we’ll start with the basics by importing the Filestack Python SDK:

pip install filestack-python

Pro tip: if you’re using VisualStudio Code or another text editor that switches between Python versions, you should make sure you’re running pip in the same context as you’re editing the code, otherwise it can’t find the pip package. You can also run the pip install inside of the VS Code shell, or automate this with a script that runs with the version of Python you want to work with and knows what to install.

Code

Create a text file that ends with the .py extension. I’ll call mine python-url.py. Edit that file, and start with an import of the Client from the Filestack Python SDK. (You did run that pip install command, right?)

from filestack import Client

Now we’ll warm up the engine by passing our APIKEY to the Client:

client = Client('APIKEY')

Unlike the JavaScript and PHP SDKs, the Python SDK uses client.upload for both file paths and URLs, but you need to name your args with keywords. For example, if you wanted to upload a file in Python from your local file system, you’d use something like this:

new_filelink = client.upload(filepath="python.png")

Instead of uploading via URL with this:

new_filelink = client.upload(url="https://cdn.filestackcontent.com/NOys4RVMTyaaM7qfu8QO")

Here’s a clean little Python script: python.url.py:

# -*- coding: utf-8 -*-
from filestack import Client
client = Client('APIKEYGOESHERE')
new_filelink = client.upload(url="https://cdn.filestackcontent.com/NOys4RVMTyaaM7qfu8QO")
print(new_filelink.url)

An animated Gif showing the execution of the python code that uploads a file by URL

It’s printing out the full URL with File Handle to the resulting upload. That file actually gets copied in to your Filestack app on your account; it’s not just a link or anything. Make sure you’re keeping track of those file handles, especially if your users are the ones uploading things. You don’t want to forget which files go with which users!

Read More →

Ready to get started?

Create an account now!