tomgross/pcloud

Uploading Images

Closed this issue · 2 comments

I'm trying to upload an image to a given pCloud path

>>> from pcloud import PyCloud
>>> pc = PyCloud('MyID','MyPSW')
>>> pc.uploadfile(filename='image.jpg', data='What goes here?', path='/path-to-pcloud-dir')
That creates a file in a pcloud given path with the text 'What goes here?'

As data I tried  Image.open, Image.fromarray, io.BytesIO, image.to bytes but always 
I get "AttributeError: 'JpegImageFile' object has no attribute 'read'"

For example:
>>>from PIL import Image
>>>i=Image.open('image.jpg','r')
>>> pc.uploadfile(filename='image.jpg', data=i, path='/path-to-pcloud-dir')

Traceback (most recent call last):
File "", line 1, in
File "/usr/local/lib/python3.7/dist-packages/pcloud/validate.py", line 20, in wrapper
return func(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/pcloud/api.py", line 135, in uploadfile
return self._upload('uploadfile', files, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/pcloud/api.py", line 112, in _upload
data=kwargs)
File "/usr/lib/python3/dist-packages/requests/sessions.py", line 581, in post
return self.request('POST', url, data=data, json=json, **kwargs)
File "/usr/lib/python3/dist-packages/requests/sessions.py", line 519, in request
prep = self.prepare_request(req)
File "/usr/lib/python3/dist-packages/requests/sessions.py", line 462, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File "/usr/lib/python3/dist-packages/requests/models.py", line 316, in prepare
self.prepare_body(data, files, json)
File "/usr/lib/python3/dist-packages/requests/models.py", line 504, in prepare_body
(body, content_type) = self._encode_files(files, data)
File "/usr/lib/python3/dist-packages/requests/models.py", line 169, in _encode_files
body, content_type = encode_multipart_formdata(new_fields)
File "/usr/lib/python3/dist-packages/urllib3/filepost.py", line 90, in encode_multipart_formdata
body.write(data)
TypeError: a bytes-like object is required, not 'JpegImageFile'

@marciano1 Thanks for your report. I've added the two upload options to the documentation: See https://github.com/tomgross/pycloud#examples

You migth want to try the latest version 1.0a8. Previously there was an issue with multiple file uploads.

Great! That worked. I would add to the uploadfile example "path='/full-pcloud-path' ". You forgot to close the parenthesis. Thank you. This method works for me several times faster than using rclone