Waivio

Recommended Posts

How To Upload Images To BackBlaze

3 comments

achimmertens989.65last yearPeakD5 min read

Hello Hivers, Developers and all others,

in this post I want to show how to upload images to BackBlaze in order to use it for automatic Hive reports.

Content:

Table of contents generated with markdown-toc
 

The Problem

If you write a blogpost in Hive and want to insert an image, you usually use a frontend like PeakD, Ecency or something else, that does some background work for you.
But if you i.e. want to generate automatic reports via the HIVE API (like I explained here) and use some images in there, then you get to the point, where you have to type:
![some text](URL of image)
And there we have the problem: We need a place to put our images and then to get the URL of it.
The HIVE-API only stores text, but no files nor images.

Register at BackBlaze

Peakd uses the file hoster "BackBlaze", so I also want to use it.
The first thing I did was to register there:
https://www.backblaze.com/b2/sign-up.html

After that I created a bucket to get a place where to upload the images:

https://files.peakd.com/file/peakd-hive/achimmertens/23viJnAEhdZEy8v1G9ZJChWGWM8JvFscwmv1K3kiCVv79SRfkEjemVQEKDQcCFsu2ucPS.png

With this, I get application keys:

https://files.peakd.com/file/peakd-hive/achimmertens/23tcNeS8TkYdqp2Y5YPvvBtARfD1uTiFEgiNB59XyA3iV586biAKedYJU9mC39h5wz1sU.png
 
It is important to note on a seperate space:

  • applikation Master Key
  • applikation Key-ID
  • application private key

https://files.peakd.com/file/peakd-hive/achimmertens/23uFsLZ8rznNsmNJSt2b3dcFCToJkaDsDiMn91HLL3BPChKFLmGY7Mprsw6zcLCL7pcVW.png

Check Access

GUI

Now we can access the Backblace Server via GUI (Webclient):

When I open my personal bucket list, I can see, what I have created:
https://secure.backblaze.com/b2_buckets.htm

https://files.peakd.com/file/peakd-hive/achimmertens/23tHbKgyTyt3RUYbKo5Faaumc5ttZXWe76vMKyYfymBTBFgtKDxa67T9FetzwPkfYUMju.png
After clicking on "Upload/Download", I can see my "Hive-Upload" folder. For a test I have uploaded the file 2023-06-15_13h49_09.png:

https://files.peakd.com/file/peakd-hive/achimmertens/23tSyz4YybhiaiTRDia3iZu74X6KgQFPvUdUhc4Mu3AAoXuortQJnnAJHdsdUa5K6U3NG.png

API

To see my file via API / curl commands we need to do the following:

Get Authorization_Token

There is a list of API commands we can execute: https://www.backblaze.com/apidocs
But the most commands only work with an authorization_Token that is valid for 24 h. So the first thing to do is to get this token.
For this, we need the bucket/application KeyID and the corresponding private key, which we noted in the step before.
We put it together like this:
KEY_ID:APPLIKATION_ID
Example:
0058a471231edbb0000000001:K005**************P48w
Then we bring it to a base64 encoder like https://www.base64encode.org/
And get a result like this:
MDA1OGE0NzEyMzFlZGJ**************1R0NacGMxVlphb0dhZEVIRzllbVN2aWdQNDh3
Then we put the word "Basic" in front of it and the result is our Authentication_Token (not to exchange with the Authorization_token):
"BasicMDA1OGE0NzEyMzFlZG*****************VN2aWdQNDh3"

With this Authentication_Token we ask for authorization:

curl -H "Authorization: BasicMDA1OGE0NzEyMzFlZG**************VN2aWdQNDh3"  https://api.backblazeb2.com/b2api/v2/b2_authorize_account

https://files.peakd.com/file/peakd-hive/achimmertens/23tRrRmTE3mPWo5pNgiNoWM5nbV9zX2HzAdAgpGsMgk5gCUbpvu6bqvyCTXHrE2i7pTRs.png

In this example, the resulting authorizationToken is:
"4_0058a471231edbb0000000001_01adba90_962a3e_acct_Jmow0Avzek5Gjzz9_rPyLFP3Og8="

Get File List

Now we want to see, if our uploaded file exists on the BackBlaze-Server.
Therefore we use the API-command b2-list-file-names
(Details see: https://www.backblaze.com/apidocs/b2-list-file-names)

For this command we need the API-URL. This we got with the command from above for the authorizationToken and it is: "https://api005.backblazeb2.com"
At the end of this URL we concatenate the API command "b2api/v2/b2_list_file_names".
The first (and only) header parameter is our authorizationToken.
The data, that we send (as this API request is a POST request) is our bucketId, and if we want, the number of maximal results.
So the complete command looks like this:

curl https://api005.backblazeb2.com/b2api/v2/b2_list_file_names -H "Authorization:4_0058a471231edbb0000000001_01adba90_962a3e_acct_Jmow0Avzek5Gjzz9_rPyLFP3Og8=" -d "{\"bucketId\": \"d86af4770152d3218e8d0b1b\", \"maxFileCount\": 1000}" 

In the result, we can see our file:
"fileName": "2023-06-15_13h49_09.png"

https://files.peakd.com/file/peakd-hive/achimmertens/23t7572JE8mfWBDDT1WnfJE2U3akkQLNzRMRiSBWziEwBunFeaBRLeL4ZcG7SWM9ocSzX.png

So, it works and we can start doing what we are here for:

Upload A File

Here I have listed the steps we need to do, to upload a file and to get the URL of it:

Get Authorization_Token

See above.

Get File-Upload Server URL

The files are distributed among different servers on BackBlaze. Therefore we need a location, where we have to put the file. We get it with the "b2api/v3/b2_get_upload_url" command.

Now we fill some variables, to make it more readable and handy:

set ACCOUNT_AUTH_TOKEN=4_0058a471231edbb0000000001_01adb976_94a2ab_acct_3dwqC00fVGFwiJ-S1uoc2usx2uQ=
set BUCKET_ID=d86af4770152d3218e8d0b1b
set API_URL=https://api005.backblazeb2.com

The command is:

curl -H "Authorization: %ACCOUNT_AUTH_TOKEN%" -d"{"bucketId": "%BUCKET_ID%"}" %API_URL%/b2api/v3/b2_get_upload_url
 
The result is:
https://files.peakd.com/file/peakd-hive/achimmertens/23tmmNARdf6rBXiW86sG3sZDREYsJ6hR7s2J4FwSQCeFBdH1ACTrGEFd1XVZN5KyQtPns.png
 

File Upload

Now we fill in the following parameters:

set MIME_TYPE=b2/x-auto 
set SHA1_OF_FILE=84ba101ecaf319a7fb82dd624d2bbe7f1fc5a539
set UPLOAD_URL=https://pod-050-1001-11.backblaze.com/b2api/v3/b2_upload_file/d86af4770152d3218e8d0b1b/c005_v0501001_t0056
set UPLOAD_AUTHORIZATION_TOKEN=4_0058a471231edbb0000000001_01adb97a_305f02_upld_TFiqv_ISVzrOwmvnrLETK5OS8T8=
set FILE_TO_UPLOAD=FeG_Ziegel_lang.jpg

The UPLOAD-URL is taken from the result above.
The FILE_TO_UPLOAD is our local image, that we want to upload.

To get the SHA1_OF_FILE, we execute the following command in the local folder of our image:

certutil -hashfile "FeG_Ziegel_lang.jpg" SHA1

https://files.peakd.com/file/peakd-hive/achimmertens/23tRxJC5YinqiF6REvzYCVT2EnBXfcMY9bHWUrAwpWb5s8CLyGJTPRd9WmXaw3rQJ6bhL.png
 
The command to upload the file is:

curl -H "Authorization: %UPLOAD_AUTHORIZATION_TOKEN%" -H "X-Bz-File-Name: %FILE_TO_UPLOAD%" -H "Content-Type: %MIME_TYPE%" -H "X-Bz-Content-Sha1: %SHA1_OF_FILE%" -H "X-Bz-Info-Author: unknown" --data-binary "@%FILE_TO_UPLOAD%" %UPLOAD_URL%
 
https://files.peakd.com/file/peakd-hive/achimmertens/23sxon1LUaDf4UswWhsvQheJHUpvfWna7vmUjaKy3E4cbdPFspHdm6SjJY5JmibbRvD36.png

So, this was successful and we can derive the File-URL by concatenating the fileserver-URL "https://f005.backblazeb2.com" and the resulting fileId "4_zd86af4770152d3218e8d0b1b_f111da3fb3ca27b46_d20230718_m054958_c005_v0501001_t0020_u01689659398256"

So here we have the final resulting file URL, that we can use for HIVE uploads:

![Example-Picture](https://f005.backblazeb2.com/b2api/v1/b2_download_file_by_id?fileId=4_zd86af4770152d3218e8d0b1b_f111da3fb3ca27b46_d20230718_m054958_c005_v0501001_t0020_u01689659398256)

Yes, and if you want to see my uploaded picture, here it is (it was just an example, that I found in my download folder ;-)):
https://f005.backblazeb2.com/b2api/v1/b2_download_file_by_id?fileId=4_zd86af4770152d3218e8d0b1b_f111da3fb3ca27b46_d20230718_m054958_c005_v0501001_t0020_u01689659398256

Conclusion

Now we have a tool, that we can use to generate automatic reports to the hive API.

Regards, Achim

Comments

Sort byBest
AI
Waivio AI Assistant
How can I help you today?