Bug in base.py? TypeError: a bytes-like object is required, not 'str' #109
Description
Dear project maintainers,
I am trying to read an attachment media as described [here](attach = client.CreateAttachmentAndUploadMedia(doc['_self'], readable_stream = pic)
print(attach))
attach = client.CreateAttachmentAndUploadMedia(doc['_self'], readable_stream = pic)
print(attach)
b = client.ReadMedia(attach['media'])
and getting an error
TypeError: a bytes-like object is required, not 'str'
after debugging with %pdb in jupyter I narrowed down the issue:
C:\PROGLANG\Anaconda3\lib\base64.py in b64encode(s, altchars)
59 if altchars is not None:
60 assert len(altchars) == 2, repr(altchars)
---> 61 return encoded.translate(bytes.maketrans(b'+/', altchars))
62 return encoded
63
TypeError: a bytes-like object is required, not 'str'
> c:\proglang\anaconda3\lib\base64.py(61)b64encode()
59 if altchars is not None:
60 assert len(altchars) == 2, repr(altchars)
---> 61 return encoded.translate(bytes.maketrans(b'+/', altchars))
62 return encoded
63
ipdb> altchars
'+-'
ipdb> altchars = b'+-'
ipdb> encoded.translate(bytes.maketrans(b'+/', altchars))
b'lrzzairkcgebaaaaaaaaalzcq9g='
ipdb> q
So it looks like you should pass altchars as binary as maketrans function expects both binary arguments
static bytes.maketrans(from, to)
static bytearray.maketrans(from, to)¶
This static method returns a translation table usable for bytes.translate() that will map each character in from into the character at the same position in to; from and to must both be bytes-like objects and have the same length.
I am using python 3.6.
Full error trace
TypeError Traceback (most recent call last)
in ()
3 documents.MediaReadMode.Streamed)
4
----> 5 b = client.ReadMedia(attach['media'])
C:\PROGLANG\Anaconda3\lib\site-packages\pydocumentdb\document_client.py in ReadMedia(self, media_link)
1661 path = base.GetPathFromLink(media_link)
1662 media_id = base.GetResourceIdOrFullNameFromLink(media_link)
-> 1663 attachment_id = base.GetAttachmentIdFromMediaId(media_id)
1664 headers = base.GetHeaders(self,
1665 default_headers,
C:\PROGLANG\Anaconda3\lib\site-packages\pydocumentdb\base.py in GetAttachmentIdFromMediaId(media_id)
249 if len(buffer) > resoure_id_length:
250 # We are cutting off the storage index.
--> 251 attachment_id = base64.b64encode(buffer[0:resoure_id_length], altchars)
252 if not six.PY2:
253 attachment_id = attachment_id.decode('utf-8')
C:\PROGLANG\Anaconda3\lib\base64.py in b64encode(s, altchars)
59 if altchars is not None:
60 assert len(altchars) == 2, repr(altchars)
---> 61 return encoded.translate(bytes.maketrans(b'+/', altchars))
62 return encoded
63
TypeError: a bytes-like object is required, not 'str'