How to set Cloudinary upload option

System Information
  • Strapi Version: 3.3.3

Hi there,

I am using the official cloudinary plugin for Strapi.

I would like to save all my photos in a specific folder on Cloudinary, however I am not so sure how to do it with Strapi.

Looking at the code, it seems that it is intended to take configuration options:

 return {
      upload(file, customConfig = {}) {
        return new Promise((resolve, reject) => {
          const upload_stream = cloudinary.uploader.upload_stream(
            { resource_type: 'auto', public_id: file.hash, ...customConfig },

How can I pass costum Configuration to the upload function?

Thank you!

If you already use the cloudinary plugin, that means you already have ./config/plugins.js file with the following code:

module.exports = ({ env }) => ({
  // ...
  upload: {
    provider: 'cloudinary',
    providerOptions: {
      cloud_name: env('CLOUDINARY_NAME'),
      api_key: env('CLOUDINARY_KEY'),
      api_secret: env('CLOUDINARY_SECRET'),
      // you can add any config option here, it will be passed to cloudinary.config()
    },
  },
  // ...
});

List of available options:

sure, but this is not exactly what I want.
I want all my files to be uploaded into a specific directory, which means I need to populate the customConfig parameter somehow.

Looking at the code, it seems that customConfig is always empty: was it on purpose?

Anyway, being strapi open-source, I was quicky:neutral_face: able to create a custom provider based on the official one, configured like so:

// strapi-provider-upload-cloudinary
let upload_config = {}
module.exports = {
  init(config) {
   // configuration for cloudinary.config()
   cloudinary.config(config.account_config); 
 
   // configuration for cloudinary.upload()
   // https://cloudinary.com/documentation/image_upload_api_reference
   upload_config = config.upload_config;

    return {
      upload(file) {
        return new Promise((resolve, reject) => {
          const upload_stream = cloudinary.uploader.upload_stream(
            { resource_type: 'auto', public_id: file.hash, ...upload_config }

With the following configuration:

// plugins.js
upload: {
        provider: 'cloudinary',
        providerOptions: {
            account_config: {
                cloud_name: env('CLOUDINARY_NAME'),
                api_key: env('CLOUDINARY_KEY'),
                api_secret: env('CLOUDINARY_SECRET'),
            },
            upload_config: {
                folder: env('CLOUDINARY_FOLDER'),
               // ... and some
            }
        },
    }

I think this is the way it was intended. Let me know. I can send you a PR if you want to implement my solution.

This worked for me:

module.exports = ({ env }) => ({
    // ...
    upload: {
      provider: 'cloudinary',
      providerOptions: {
        cloud_name: env('CLOUDINARY_NAME'),
        api_key: env('CLOUDINARY_KEY'),
        api_secret: env('CLOUDINARY_SECRET'),
      },
      actionOptions: {
        upload: {
          folder: env('CLOUDINARY_FOLDER'),
        },
        delete: {},
      },
    },
    // ...
  });
1 Like

In Strapi v4 it seems no to work anymore. I have the very same config in which folder was some string like my-folder-on-cloudinary. Am I missing something or it’s v4 problem?

1 Like

Passing the upload folder no longer works in V4, assets are uploaded to the main directory

1 Like

I saw somewhere this solution and it works for me:

actionOptions: {
        uploadStream: {
          folder: env("CLOUDINARY_FOLDER", ""),
        },
        delete: {},
}

The options expect uploadStream property and not upload.

6 Likes

It’s funny how I just came here now to post just that :smiley: (found the same suggestion in on of the GitHub issues a minute ago). Passing “uploadStream” seems to be the fix. Thanks for replying!

1 Like

Yep. It fixed it for me too. I looked in Cloudinery doc but must of been looking in the wrong place. Found this late, but thanks for the fix.

1 Like

Yeah, it fixes the problem, and also creates the folder automatically in Cloudinary.

1 Like

is there a way to upload into a cloudinary folder based on the folder the image is uploaded on strapi media library?

Awesome. I had a limited time and needed a solution. Thanks Much

You would have to extend the upload plugin to take the media into account when uploading it. I saw a snippet related to it, maybe here, maybe in another thread, but I don’t remember where it is.

1 Like

thank you sir . it’s also works for me

Hi @LuisAlaguna

Did you find the snippet?
I think that might be the fix to my current issue

@stanislaus-oc this snippet fixes it.

Thanks, I’ll try it out

I would like to have granular upload by media folders, but as far as I know, there is no a way to do that.