I am getting connect Request failed with status code 400
from b2.uploadFIle()
Here’s what I tried to do:
Step 1: Download backblaze-b2 node.js library and multer(to get image file in req.body)
Step 2: Set up how I’m going to call my route in POSTMAN. I have attached an IronMan.png in my request.
Step 3: Set up my code:
JavaScript
x
60
60
1
import B2 from "backblaze-b2";
2
export const uploadCreationImage = async (
3
) => {
4
try {
5
const b2 = new B2({
6
applicationKeyId: process.env.backblazeb2ApplicationKeyId,
7
applicationKey: process.env.backblazeb2ApplicationKey,
8
});
9
10
await b2.authorize(); // must authorize first (authorization lasts 24 hrs)
11
console.log("I am here");
12
let response = await b2.getBucket({
13
bucketName: "bucketName",
14
});
15
16
var storage = multer.diskStorage({
17
destination: function (req, file, cb) {
18
cb(null, "./uploads");
19
},
20
filename: function (req, file, cb) {
21
cb(null, file.originalname);
22
},
23
});
24
25
const multerUploader = multer({});
26
upload(req, res, (err: any) => {
27
if (err instanceof multer.MulterError) {
28
return res.sendStatus(INTERNAL_SERVER_ERROR_STATUS);
29
// A Multer error occurred when uploading.
30
} else if (err) {
31
// An unknown error occurred when uploading.
32
return res.sendStatus(INTERNAL_SERVER_ERROR_STATUS);
33
}
34
// console.log("joe", req.file.buffer);
35
// console.log("biden", req.file);
36
b2.getUploadUrl({
37
bucketId: "58dd09e54842aafc7dcd0917",
38
// ...common arguments (optional)
39
}).then((response) => {
40
console.log("getUploadUrl", response.data.uploadUrl , response.data.authorizationToken);
41
42
b2.uploadFile({
43
uploadUrl: response.data.uploadUrl,
44
uploadAuthToken: response.data.authorizationToken,
45
fileName: "fileName",
46
data: req.file.buffer, // this is expecting a Buffer, not an encoded string
47
48
49
onUploadProgress: null,
50
//onUploadProgress: (event) => {} || null // progress monitoring
51
// ...common arguments (optional)
52
}).then((response) => {
53
console.log('uploadFIle', response);
54
return res.send({ path: req.file.originalname });
55
}
56
57
// Everything went fine and save document in DB here.
58
});
59
});
60
I used multer to get the image file from the form request and then pass it to b2.uploadFile’s data property as a buffer.
Any help would be appreciated!
Advertisement
Answer
Fixed! all I had to do was to get rid of the optional parameters in b2.uploadFile()
JavaScript
1
47
47
1
upload(req, res, (err: any) => {
2
if (err instanceof multer.MulterError) {
3
return res.sendStatus(INTERNAL_SERVER_ERROR_STATUS);
4
// A Multer error occurred when uploading.
5
} else if (err) {
6
// An unknown error occurred when uploading.
7
return res.sendStatus(INTERNAL_SERVER_ERROR_STATUS);
8
}
9
10
b2.getUploadUrl({
11
bucketId: process.env.backblazeb2BucketId,
12
// ...common arguments (optional)
13
}).then((response) => {
14
console.log(
15
"getUploadUrl",
16
response.data.uploadUrl,
17
response.data.authorizationToken
18
);
19
20
b2.uploadFile({
21
uploadUrl: response.data.uploadUrl,
22
uploadAuthToken: response.data.authorizationToken,
23
fileName: "fileName",
24
// contentLength: 0, // optional data length, will default to data.byteLength or data.length if not provided
25
//mime: "", // optional mime type, will default to 'b2/x-auto' if not provided
26
data: req.file.buffer, // this is expecting a Buffer, not an encoded string
27
//hash: "sha1-hash", // optional data hash, will use sha1(data) if not provided
28
// info: {
29
// // optional info headers, prepended with X-Bz-Info- when sent, throws error if more than 10 keys set
30
// // valid characters should be a-z, A-Z and '-', all other characters will cause an error to be thrown
31
// key1: "value",
32
// key2: "value",
33
// },
34
onUploadProgress: (event) => {},
35
//onUploadProgress: (event) => {} || null // progress monitoring
36
// ...common arguments (optional)
37
}).then((response) => {
38
console.log("uploadFIle", response);
39
return res.send({
40
path: req.file.originalname,
41
});
42
});
43
44
// Everything went fine and save document in DB here.
45
});
46
});
47