Recently I’ve been working on a backup script to backup a folder to a Backblaze B2 bucket. If only I had known I was about to spend an hour working on four silly lines of python code…
Backblaze’s documentation has absolutely no mention that this only works with Python 2. I’m not the only one. Pronoy had this exact issue with a Google API. Again, no mention it only works in Python 2. In fact, the moment I stumbled across Pronoy’s post is the same moment I stopped ripping my hair out.
Let’s get into the code. According to Backblaze’s API docs, they require
the Account ID and Key to be base64 encoded in the following format:
'accoundId:accountKey'. Seems easy enough.
Try this in a Python 2 shell:
You get an output like:
Try running the same thing in a Python 3 shell and you see something closer to this:
File "<stdin>", line 1, in <module> File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/ Versions/3.6/lib/python3.6/base64.py", line 58, in b64encode encoded = binascii.b2a_base64(s, newline=False) TypeError: a bytes-like object is required, not 'str'
The solution? There’s a few. Pronoy’s works: which is to encode the
UTF-8. Then, once it’s base64 encoded, you decode that as
After doing some more scouring, I found this Stackoverflow thread.
# The following works, too, and looks a little neater. base64.b64encode(bytes('A string', 'utf-8')).decode('ascii')
Here’s another Stackoverflow thread explaining why.
Articles from blogs I follow around the net
Have you ever found yourself staring at a failed CI build, wondering desperately what happened? Or, have you ever needed a fresh machine on-demand to test out an idea in? Have you been working on Linux, but need to test something on OpenBSD? Starting this we…via Drew DeVault's Blog August 19, 2019
I hope I don’t have to explain why Google is bad, but just to give a few reasons to switch to alternatives: You’ll probably get better privacy because those alternatives collect less data about you, your data won’t get sold to advertisers or government or…via Posts on jlelse's Blog August 7, 2019
Generated by openring