Brandon Taylor
Brandon Taylor

Reputation: 34553

Specifying "folder" option does not upload image to folder in Cloudinary

I'm having some difficulty getting uploads to the correct folder. If I specify:

cloudinary.uploader.upload(filePath, {
  folder: 'test-directory'
});

I'm following the Node integration guide at: http://cloudinary.com/documentation/image_upload_api_reference#upload

I created the "test-directory" ahead of time. The image uploads successfully, but always goes to the root of my media library.

I have "auto create folders" enabled in my account settings.

Here's a sample upload response from Cloudinary:

{
  public_id: 'we1yjvlpxc1qtuf1oaqe',
  version: 1503236713,
  signature: '37edbc2b19ea72b75298acce8075f9e8ddb12d09',
  width: 675,
  height: 37,
  format: 'jpg',
  resource_type: 'image',
  created_at: '2017-08-20T13:45:13Z',
  tags: [],
  bytes: 602,
  type: 'upload',
  etag: 'b5522ae652340881b213c46c035d0aed',
  url: 'http://res.cloudinary.com/alsoicode/image/upload/v1503236713/we1yjvlpxc1qtuf1oaqe.jpg',
  secure_url: 'https://res.cloudinary.com/alsoicode/image/upload/v1503236713/we1yjvlpxc1qtuf1oaqe.jpg',
  original_filename: 'test_r6_c1'
}

I have also tried adding the folder name to the public_id option, but that hasn't worked either.

What am I doing wrong?

Upvotes: 5

Views: 4864

Answers (4)

Mistico
Mistico

Reputation: 355

Cloudinary upload function looks like the following one, options contains different fields including folder field.

upload(file: string, options?: UploadApiOptions, callback?: UploadResponseCallback)

For those who use upload_stream can refer to the following example method

  uploadFile(file: Express.Multer.File): Promise<CloudinaryResponse> {
    return new Promise<CloudinaryResponse>((resolve, reject) => {
      const uploadStream = cloudinary.uploader.upload_stream(
        {
          folder: 'folderName',
        },
        (error, result) => {
          if (error) return reject(error);
          resolve(result);
        },
      );

      streamifier.createReadStream(file.buffer).pipe(uploadStream);
    });
  }

Upvotes: 0

cloudinary upload function expects three parameters, cloudinary.uploader.upload(path,options,callback) if you didn't declare the three parameters this way, cloudinary will understand your folder path as callback not an option, thus it won't understand the path nor the folder that you are pointing at.

Upvotes: 0

Talha Javed
Talha Javed

Reputation: 125

  • upload parameters' order as (file, options, callback) *

    cloudinary.uploader.upload(
              file,
              {
                  folder: 'directory',
                  use_filename: true
              },
              function (error, result) {
                  if (result.url) {
                      resolve({ url: result.url })
                  } else {
                      reject({ message: "image upload fail", error })
                  }
              },
    
          );
    

Upvotes: 1

Maor.G
Maor.G

Reputation: 460

Add the v2 to have the upload parameters' order as (file, options, callback) -

cloudinary.v2.uploader.upload(filePath, {
 folder: 'test-directory',
 use_filename: true
});

Without it the order is (file, callback, options) -

cloudinary.uploader.upload(filePath,function(res){console.log(res);},{
 folder: 'test-directory',
 use_filename: true
})

Upvotes: 10

Related Questions