javascriptbackblaze

How to upload file into a specific folder on Backblaze?


Yò! So I've followed this guide on Backblaze and JavaScript. I've successfully uploaded files on my Backblaze bucket, but I can't find a way to define a folder where to upload the files.

I've also checked the documentation and it mentions folders but not in the contest of uploading files. Does someone know how to?

{
  axios
    .post(
      credentials.apiUrl + "/b2api/v1/b2_get_upload_url",
      {
        bucketId: bucketId,
      },
      { headers: { Authorization: credentials.authorizationToken } }
    )
    .then(function (response) {
      var uploadUrl = response.data.uploadUrl;
      var uploadAuthorizationToken = response.data.authorizationToken;
      const fileSize = file.byteLength;

      var sha1 = crypto.createHash("sha1").update(file).digest("hex");

      axios
        .post(uploadUrl, file, {
          headers: {
            Authorization: uploadAuthorizationToken,
            "X-Bz-File-Name": fileName,
            "Content-Type": "b2/x-auto",
            "Content-Length": fileSize,
            "X-Bz-Content-Sha1": sha1,
            "X-Bz-Info-Author": "unknown",
          },
        })
        .then(function (response) {
          console.log(response); // successful response
        })
        .catch(function (err) {
          console.log(err); // an error occurred
        });
    })
    .catch(function (err) {
      console.log(err); // an error occurred
    });
};

Solution

  • Okay so I feel pretty stupid. The solution is simply:

    "X-Bz-File-Name": "folderName/" + fileName,
    

    This will also create all the necessary directories. Yò.