How to convert an InMemoryUploadedFile in django to a fomat for flickr API?
Asked Answered
D

2

6

I have a class that uploads a file to Flickr. The file is of type
'InMemoryUploadedFile'.

I would like to know how to convert or pass the data in the 'InMemoryUploadedFile' file, to a format for flickr's API?

Eg:

{'photo': ('image.jpg', <InMemoryUploadedFile: image.jpg (image/jpeg)>)}

Upload API: https://www.flickr.com/services/api/upload.api.html

Error Code

<?xml version="1.0" encoding="utf-8" ?>
<rsp stat="fail">
    <err code="4" msg="Filesize was zero" />
</rsp>
Deaminate answered 5/4, 2016 at 4:18 Comment(0)
S
15

InMemoryUploadedFile is a wrapper around a file object. You can access the file object using the file attribute. So, in your example, try passing this to the Flickr API:

{'photo': my_in_memory_file.file}

If that doesn't work, please edit your question with more detail around the code you're using to submit the request.

Sitar answered 5/4, 2016 at 4:28 Comment(3)
Thanks. But it returns: {'photo': ('image.jpg', <_io.BytesIO object at 0x10dbefa70>)}Deaminate
<?xml version="1.0" encoding="utf-8" ?> <rsp stat="fail"> <err code="4" msg="Filesize was zero" /> </rsp>Deaminate
Can you update your question with the code you're using? Both the code that outputs {'photo': ('image.jpg', <_io.BytesIO object at 0x10dbefa70>)} and the code you're using to post the photo to the API.Sitar
D
3

The data inside the InMemoryUploadedFile onbject was extracted and passed to Flickr succesfully via:

import StringIO
file.seek(0)
file_handle = StringIO.StringIO(file.read())
{'photo': ('image.jpg', file_handle)}

Thanks

Deaminate answered 6/4, 2016 at 18:30 Comment(2)
Not valid for python 3.Botha
I would be careful with this. You'll probably want to read in chunks instead. Note the warning in the official Django docs: docs.djangoproject.com/en/2.0/ref/files/uploads/…Felton

© 2022 - 2024 — McMap. All rights reserved.