DEV Community

Austin Spivey for Wia

Posted on

How to upload a file to Wia's REST API

In this tutorial, i'm going to show you how to upload a file of any type(e.g. png, jpg, pdf or txt) to Wia.

Installing Python and the Wia library

If you haven’t already, you need to install the python language to complete this tutorial.

https://www.python.org/downloads/

  • Version 3.x.x

  • Follow the download instructions for your operating system

Next is to install the python package manager pip. Pip allows us to get the Wia python library which provides functionality to help connect to Wia with python.

For windows, open power-shell and run pip install wia in the power-shell terminal.

For Mac/Linux open a terminal and run pip install wia in the terminal.

alt text

Adding the code

  • Create a new folder(Name is irrelevant) in a location of your choosing (e.g. Documents folder)
  • Add the file that you want to upload to Wia into the folder

Note: the file can be anything, text images etc

In your favourite text editor, create a new file and call it file_upload_to_wia.py

from wia import Wia
wia = Wia()
wia.access_token = 'your-device_secret_key'

file_name = ‘name_of_your_file’ 
dir_path = os.path.dirname(os.path.realpath(__file__))
result = wia.Event.publish(name='file', file=open(dir_path + '/’ + file_name, 'rb'))
Enter fullscreen mode Exit fullscreen mode

alt text

  • Replace your-device-secret-key with a Device Secret key from Wia, if you haven't got one, details on how to get yourself setup with Wia can be found here.
  • Replace the name_of_your_file with the name of the file you want to upload
  • Save the file

Note: the location of the file and the code must be in the same folder for the tutorial for the code to run without modification.

Running the code

For windows,

  • open powershell
  • cd into the folder where your code lives
  • run python file_upload_to_wia.py in the powershell terminal

For Mac/Linux,

  • Open a terminal
  • cd into the folder where your code lives
  • run python file_upload_to_wia.py in the terminal

alt text

Head to your Wia dashboard and in your Device section, click on the Events tab and click View File on the file to see the file.

alt text

That's it for this tutorial! Check out our other tutorials, new ones are being added all the time.

Top comments (0)