Upload a file

Learn how to upload a file to Ranoz.


When you upload a file to Ranoz via the API, more information about the file is returned, such as the url or the id, which you can then save.

Examples

To upload a file, first, extract the file's metadata (filename and size) and send a POST request to https://ranoz.gg/api/v1/files/upload_url.

This will return the file object including the url that you can store for later use.

With it the pre-signed URL. Then use a PUT request to upload the file to the pre-signed URL.

Parameter
Location
Description
filenameBody (JSON)The name of the file you intend to upload.
sizeBody (JSON)The size of the file in bytes.
import requests
import os

# Step 1: Obtain a Pre-Signed URL

file_path = 'your-file.png'
filename = os.path.basename(file_path)
size = os.path.getsize(file_path)

url = 'https://ranoz.gg/api/v1/files/upload_url'
file_metadata = {
"filename": filename,
"size": size
}

response = requests.post(url, json=file_metadata)
response_data = response.json()
print(response_data)
upload_url = response_data['data']['upload_url']

# Step 2: Upload the File to the Pre-Signed URL

with open(file_path, 'rb') as file:
    upload_response = requests.put(upload_url, data=file, headers={
    "Content-Length": str(size)
    })

print(upload_response)

Again: For the actual PUT request to upload a file, on success nothing from the response is needed. We extract all data for the file from the response of the first request, i.e. before we actually upload the file.