Skip to content

Instantly share code, notes, and snippets.

@tuhin47
Created August 20, 2023 03:56
Show Gist options
  • Save tuhin47/6557250492d2f25284233faa1bcb8199 to your computer and use it in GitHub Desktop.
Save tuhin47/6557250492d2f25284233faa1bcb8199 to your computer and use it in GitHub Desktop.
This code snippet uploads an image file to a remote server using the ImageKit API. It retrieves the file name from the command line arguments, prepares the payload and headers for the API request, sends the request, and prints the URL of the uploaded image This code uploads an image to a specified folder using the OpenBSD library and prints its …

Image Upload with ImageKit API

Preview:
#!/usr/bin/python3

import json
import sys
from pathlib import Path

import requests

file = sys.argv[1]
fileName = Path(file).name.replace("ksnip_tmp_", "")

url = "https://upload.imagekit.io/api/v1/files/upload"

payload = {'fileName': fileName, 'folder': '/ApiUpload/'}

files = [
    ('file', (fileName, open(file, 'rb'), 'image/png'))
]
headers = {
    'Authorization': 'Basic <your-api-key>'
}

response = requests.request("POST", url, headers=headers, data=payload, files=files)

y = json.loads(response.text)

print(y["url"])
Associated Context
Type Code Snippet ( .py )
Associated Tags Python3 HTTP Headers Image URL ImageKit File Upload Python SDK Pathlib Library master API Upload Pathlib python-requests Imagekit API JSON Python3 Library Data Processing github: modified Requests Authorization Header Response Handling Authorization flask JSON Parsing File Uploading uploadImageKit.py
📝 Custom Description This snippet is from the file uploadImageKit.py and from project modified.
Associated Commit Messages Commit Messages:
update settings
update formatting
💡 Smart Description This code snippet uploads an image file to a remote server using the ImageKit API. It retrieves the file name from the command line arguments, prepares the payload and headers for the API request, sends the request, and prints the URL of the uploaded image
This code uploads an image to a specified folder using the OpenBSD library and prints its URL.
🔎 Suggested Searches Python code for uploading image to ImageKit API
How to upload image using requests in Python
Code snippet for uploading file to ImageKit using Python
Python script to upload image to ImageKit API
Upload image to ImageKit using Python and requests
How to upload an image using Python3?
How to create a POST request with python3 and requests?
What is the syntax for uploading files in Google API ?
How to use pathlib to download data from Azure API?
Related Links https://docs.python.org/3/library/pathlib.html
https://docs.python.org/3/library/json.html#json.loads
https://docs.python.org/3/library/sys.html
https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files
https://realpython.com/flask-by-example-part-1-project-setup/
https://realpython.com/python-requests/
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Authorization
https://docs.python-requests.org/en/latest/user/authentication/
https://upload.imagekit.io/api/v1/files/upload
https://docs.python.org/3/library/json.html#json.dumps
https://www.digitalocean.com/community/tutorials/how-to-make-a-web-application-using-flask-in-python-3
https://docs.python.org/3/library/json.html
https://docs.python-requests.org/en/latest/
Related People MD Towhidul Islam
Sensitive Information No Sensitive Information Detected
Shareable Link https://tuhin47.pieces.cloud/?p=971b4e882a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment