Unable to upload media file from local to my GCS

I have successfully implemented the GCS into my local strapi system. I am able to upload media files from strapi interface to my GCS. But when I am trying to upload file manually from my code to GCS, It is throwing error for me.

Please look into my code and correct me.

Hello @Sumit_Choubey ,

Note that Content-Type is a mandatory request header for /upload.

From your code I see that you don’t send the Content-Type at all. You defined it in headers but headers is never used.

So the correct axios request would be:

await axios.post(uri,bodyFormData,{
        headers: { 'Content-Type': 'multipart/form-data' },
      })

or in your case, with predefined headers variable:

await axios.post(uri,bodyFormData,{headers})

Hello @sunnyson,

I have tried to pass the content-type as well. Still it is not working for me. Is my all code is fine?

Getting below err:

Please note that Axios works a bit differently on server-side.
I don’t remember how exactly it should be configured, but here is an working example with request module for server-side upload:

    const uri = encodeURI('./Files/TestSumit (2).pdf');
    const options = {
      method: 'POST',
      url: 'http://127.0.0.1:1337/upload',
      headers: {
        'Content-Type': 'multipart/form-data',
      },
      formData: {
        files: fs.createReadStream(uri),
      },
    };
    request(options, function (err, res, body) {
      if (err) console.log(err);
      console.log(body);
    });

I have tried your code but it is giving 403 forbidden error.

You forgot to configure the Public role to have access to the /upload path.
Right now it is accessible only to authenticated users.

No, that’s not the problem. I have already done it before. I forgot to pass the Authorization token in the header. Now we have successfully uploaded the file. Thank you so much for your guidance.

2 Likes