DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Languages
  4. Upload Files With Python

Upload Files With Python

In this quick article, we take a look at how to use an open source SDK made by the Filestack team to upload files to a server using Python.

David Liedle user avatar by
David Liedle
·
Oct. 15, 18 · Tutorial
Like (2)
Save
Tweet
Share
43.10K Views

Join the DZone community and get the full member experience.

Join For Free

Python is eating the world! You will find many passionate Python programmers and just as many critics, but there's no denying that Python is a powerful, relevant, and constantly growing force in software development today.

Python is just a language, though, and languages can't solve business problems like workflow, architecture, and logistics; these things are up to you, the developer! The packages you choose, the architecture you implement, and strategy you follow will all impact the success of your Python project. Let's take a look at the logistics behind uploading a file to the cloud using Python. I'll discuss some of the considerations every team faces when implementing a file upload and management solution and then end with a concise recipe for you to upload files with Python using Filestack's Python SDK.

The Problem: How Do I Manage User-Generated Uploads? 

You've built your MVP, and all the tests are passing. You have the ability to upload files via HTML forms on your site; that's easy! Just a simple POST on form submit, and your server catches whatever your users throw at it. That's only the beginning, though — next comes the hard part(s).

Ensuring that your users have a great experience when they upload, transform, and share their content means providing them with reliable, resumable, fast uploads. It means providing them with the tools they need to crop, resize, and otherwise adjust their uploads. It can mean converting files from one format to another. It certainly means optimizing the delivery so they are saving data and getting the highest quality content as fast as possible from a powerful, distributed CDN.

The Solution: Use a Python SDK

When a project can separate the grunt work from the business logic, everybody wins. You could build everything from scratch with a small army of developers and a time machine, but why do that when you can use the Filestack API? And why call the API directly, implementing everything in your own code, when you could just pop in the SDK and call it a day?

Obtain the SDK

Using pip, run the following command:

pip install filestack-python

You should see a nice display of progress bars as the files are automatically loaded up for you:

Once you have the Filestack Python SDK installed you'll just need to bring it into your very own Python script, create an instance of the Client with your Filestack API key, and then call client.upload() with some parameters. Import it like this:

from filestack import Client

That client does all the heavy lifting for you. Here's a simple recipe to put it to work:

Requirements:

Create a file to test inside of the directory where you would like to try things out. I'll call mine `upyougo.py`. Add an image in there for your upload example. Mine is the comic strip from xkcd with the flying Pythonista.

from filestack import Client
client = Client(“APIKEY”)

params = {‘mimetype’: ‘image/png’}
new_filelink = client.upload(filepath=”python.png”, params=params)
print(new_filelink.url)

Image title

Be sure to replace "APIKEY" with your actual API key.

Run this by passing the filename to Python as an argument in the command line, like this:

python upyougo.py

That's it! With this simple code, we can successfully upload a file from our local machine to our Filestack app using Python. You should see a URL output to your terminal upon completion of the upload. Copy and paste that into your browser to view, and try out some of our transformation URLs to play with it further!

But, Docker...

Ok, here's a ridiculously fast way to try this out on any machine: if you have Docker installed on your system, you can run the example upload shown above with the following command:

docker run -it –rm -e APIKEY=MYAPIKEYHERE filestack/python:example

This has just been a tiny peek at what you can do with Filestack's JavaScript SDK on the server side with node. To learn more, check out https://www.filestack.com/products/file-upload/ and explore our docs for detailed features and implementation instructions.

Python (language) Upload

Published at DZone with permission of David Liedle, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Java Development Trends 2023
  • Using AI and Machine Learning To Create Software
  • Kubernetes vs Docker: Differences Explained
  • RabbitMQ vs. Memphis.dev

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: