Axios Error 500 when uploading media in production

  • Strapi Version: 4.5.1
  • Operating System: Linux Debian 11
  • Database: Mysql
  • Node Version: 16.18.1
  • NPM Version: 9.2.0
    I am trying to upload media to my bucket stored in google cloud. I followed the cloud storage provider documentation, but the error throws error 500 realated to my https://www.domain.com/upload.

Here is my package.json

"dependencies": {
    "@strapi-community/strapi-provider-upload-google-cloud-storage": "^4.3.6",
    "@strapi/plugin-i18n": "4.5.1",
    "@strapi/plugin-users-permissions": "4.5.1",
    "@strapi/provider-email-nodemailer": "^4.5.3",
    "@strapi/strapi": "4.5.1",
    "axios": "^1.2.0",
    "dotenv": "^16.0.1",
    "mysql": "2.18.1",
    "sharp": "^0.31.3",
    "strapi-utils": "^3.6.11"
  },

Here is my plugins.js

module.exports = {
    upload: {
      config: {
        provider: '@strapi-community/strapi-provider-upload-google-cloud-storage',
        providerOptions: {
            bucketName: 'bucketname',
            publicFiles: true,
            uniform: true,
            baseUrl: 'https://storage.googleapis.com/bucketname',
            basePath: ''
        },
      },
    },
}

Here is my middlewares.js

module.exports = [
  'strapi::errors',{
    name: 'strapi::security',
    config: {
      contentSecurityPolicy: {
        useDefaults: true,
        directives: {
          'connect-src': ["'self'", 'https:'],
          'img-src': ["'self'", 'data:', 'blob:', 'https://storage.googleapis.com','https://dl.airtable.com'],
          'media-src': ["'self'", 'data:', 'blob:', 'https://storage.googleapis.com','https://dl.airtable.com'>
          upgradeInsecureRequests: null,
        },
      },
    },
  },
  {
    name: 'strapi::cors',
    config: {
      enabled: true,
      headers: '*',
      origin: ['http://localhost:1337']
    }
  },
  'strapi::poweredBy',
  'strapi::logger',
  'strapi::query',
  'strapi::body',
  'strapi::session',
  'strapi::favicon',
  'strapi::public',
];

N: Before adding SSL I uploaded some images to the same bucket but i was not able to see those images so I think the bucket is working (because I could upload those images), now after adding security in middlewares.js i can see those images previously created but can not upload anything else


Check the strapi console log for the actual error ?
That way you do get the correct error.

I think it was a version issue, I migrated from 4.5.1 to 4.5.5 and it’s ok now but there’s a new bug, when I POST to a collection that must be linked to an user, it does’t link it. It doesn’t throw any error but saves the data without the user sent by the POST request

I don’t think it’s an error as a relationship is not required field.
I’m going to guess it’s how you are passing the relationship with the user.
PS: Suggesting upgrade 4.5.6 not 4.5.5 due to security

I know it’s not a required field but I need to link the user with the saved data to show it in the main application.

This is how I used to POST with no errors

export async function addAddressApi(auth, address) {
  try {
    const url = `${API_URL}/addresses`;
    const params = {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        Authorization: `Bearer ${auth.token}`,
      },
      body: JSON.stringify({
        data: address,
      }),
    };
    const response = await fetch(url, params);
    const result = await response.json();
    return result;
  } catch (error) {
    console.log(error);
    return null;
  }
}

Here is an example printing the sent data

Object {
  "address": "Cnnxnx",
  "city": "Fncnc",
  "district": "Cncncn",
  "name_lastname": "Fndnc",
  "phone": "5998986",
  "reference": "Xncn",
  "title": "Fjd",
  "user": 1,
}

Also this is my collection

So if it’s a 500 error always check the strapi logs aka strapi console as it crashed and could provide more details.

The object your sending needs to be neates in a data object