Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image upload not working #37

Closed
snowformatics opened this issue Feb 25, 2023 · 6 comments
Closed

Image upload not working #37

snowformatics opened this issue Feb 25, 2023 · 6 comments

Comments

@snowformatics
Copy link

Hi,

I am trying to upload my image:

import base64
import os
import sys
from imagekitio import ImageKit
from imagekitio.models.UploadFileRequestOptions import UploadFileRequestOptions

PRIVATE_KEY = os.environ.get('PRIVATE_KEY')
PUBLIC_KEY = os.environ.get('PUBLIC_KEY')

imagekit = ImageKit(
    private_key='private',
    public_key='public',
    url_endpoint='https://ik.imagekit.io/nb4gbrqqe'
)

url = "event20230225_133517_27.jpg"
upload = imagekit.upload(
    file=open(url, "rb"),
    file_name="test.jpg",
    options=UploadFileRequestOptions(
        tags = ["test"]
    )
)

list_files = imagekit.list_files()

print (list_files.response_metadata.raw)

But imagekitio.io ist showing just:

image

When I extract the metadata, it looks like the image was not uploaded as image type:

{'type': 'file', 'name': 'test_doAbVVtZJ.jpg', 'createdAt': '2023-02-25T19:42:54.915Z', 'updatedAt': '2023-02-25T19:42:55.118Z', 'fileId': '63fa64bee809dd54b091d551', 'tags': ['test'], 'AITags': None, 'versionInfo': {'id': '63fa64bee809dd54b091d551', 'name': 'Version 1'}, 'embeddedMetadata': {}, 'customCoordinates': None, 'customMetadata': {}, 'isPrivateFile': False, 'url': 'https://ik.imagekit.io/nb4gbrqqe/test_doAbVVtZJ.jpg', 'thumbnail': 'https://ik.imagekit.io/demo/img/static-file-1.png', 'fileType': 'non-image', 'filePath': '/test_doAbVVtZJ.jpg', 'size': 90}]

My manually uploaded image show 'fileType': 'image'.

How can I upload my images as image type?

Thanks in adavance
Stefanie

@Hexvortex
Copy link

Yeah i am also getting broken image after uploading via python script.

@samims
Copy link
Contributor

samims commented Mar 4, 2023

As mentioned in the test, the file needs to be base64 encoded.

file=open(url, "rb")
 imgstr = base64.b64encode(file.read())

https://github.com/imagekit-developer/imagekit-python/blob/master/tests/test_files_ops.py#L154

@Hexvortex
Copy link

Here is updated and working code.

import base64
import os
import sys
from imagekitio import ImageKit
from imagekitio.models.UploadFileRequestOptions import UploadFileRequestOptions

imagekit = ImageKit(

    private_key='private_XXXXXXXXXXXXXXXX',

    public_key='public_YYYYYYYYYYYYYYYYY',

    url_endpoint='https://ik.imagekit.io/XXXXXXXXXX/'

)


 

with open('11.png', 'rb') as f:
    # Read the contents of the file
    image_data = f.read()
    # Encode the image data to base64
    encoded_image = base64.b64encode(image_data)
    encoded_image
    
    
upload = imagekit.upload(
        encoded_image,
	    file_name="16212.png",
	    options=UploadFileRequestOptions(
		tags = ["tag1", "tag2"]
	    ) 
)

# print that uploaded file's ID
print(upload.name)

@snowformatics
Copy link
Author

Works, thanks!

@reisenmachtfreude
Copy link

I had the same issue. Documentation should be updated.
https://docs.imagekit.io/api-reference/upload-file-api/server-side-file-upload

upload = imagekit.upload(
    file=open("image.jpg", "rb"),
    file_name="my_file_name.jpg",
    options=UploadFileRequestOptions(
        tags = ["tag1", "tag2"]
    )
)

aman-squareboat pushed a commit to aman-squareboat/imagekit-python that referenced this issue Jun 21, 2023
@imagekitio
Copy link
Contributor

Fixed in version 3.0.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants