Skip to content

[v4] It's impossible to use a custom upload provider #11782

@andreaSimonePorceddu

Description

@andreaSimonePorceddu

Bug report

Describe the bug

By configuring a custom upload provider in config/plugins.js as described in the documentation docs, Strapi is not using the configured provider but instead, it is still using the default one (@strapi/provider-upload-local)

Steps to reproduce the behaviour

Note: ENV Variables and the S3 bucket are correctly set

  • Install @strapi/provider-upload-aws-s3
  • Create the file config/plugins.js as explained here:
module.exports = ({ env }) => ({
   upload: {
       provider: 'aws-s3',
       providerOptions: {
       accessKeyId: env('AWS_ACCESS_KEY_ID'),
       secretAccessKey: env('AWS_ACCESS_SECRET'),
       region: env('AWS_REGION'),
       params: {
          Bucket: env('AWS_BUCKET'),
          },
      }
   }
})
  • run npm run build && npm run start or npm run develop
  • Open the admin panel and navigate into the media libraries section
  • Upload a file

By checking your bucket you will not see any new file, instead, you can see the new file inside the public folder.

I also place a console.log inside node_modules/@strapi/provider-upload-local upload function
and inside node_modules/@strapi/provider-upload-aws-s3 upload function and in fact only @strapi/provider-upload-local is executed.

Expected behaviour

It should use the new provider configuration.

System

  • Node.js Version: v14.17.5
  • NPM version: 6.14.14
  • Strapi version: v4
  • Operating system: Mac OS Catalina 10.15.7

Which alternatives I tried

  • Using the old library strapi-provider-upload-aws-s3 instead of the @strapi/provider-upload-aws-s3 (not working)
  • Create a json configuration inside src/extensions/upload/config/settings.json (not working)
  • Create a js configuration inside src/extensions/upload/config/settings.js (not working)
  • Using npm local paths to rename the new library @strapi/provider-upload-aws-s3 to strapi-provider-upload-aws-s3 as suggested in the documentation for the scoped packages docs (not working)
  • Create a local provider (not working) docs

Workaround

⚠️ NO NEED TO DO THIS, SCROLL DOWN TO COMMENTS TO SEE THE SOLUTION

I managed to get the aws-s3 provider work in this way:

  • Create a local provider in ./providers/my-upload-provider/
  • I copied the ./node_modules/@strapi/provider-upload-aws-s3 into my provider folder
  • Since the configuration file written inside ./configuration /plugins.js seems not to be provided to our custom provider, we edit the ./providers/my-upload-provider/lib/index.js by adding the right config:
module.exports = {
  init(config) {
   // I add my configuration here
    const configInternal = {
      accessKeyId: process.env.AWS_KEY_ID, 
      secretAccessKey: process.env.AWS_SECRET_KEY,
      region: process.env.AWS_REGION,
      params: {
        Bucket: process.env.AWS_BUCKET_NAME,
      },
    }

    const S3 = new AWS.S3({
      apiVersion: '2006-03-01',
      ...config,
      // And than I passed the configuration to AWS.S3
      ...configInternal,
    });
    ...
}
  • I force npm to override the @strapi/provider-upload-local with my custom provider by using npm local paths:

package.json

{
...
    "dependencies": {
        ...
        "@strapi/provider-upload-local": "file:providers/my-upload-provider",
    } 
}
  • Run yarn install && yarn develop

It works for me

Note: I had other issues after this fix, related to CORS. It can be fixed by following this: #11637

UPDATE

It seems a docs mistake. Scroll down to the comments to read the solution.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions