Cloudinary supports specifying a folder name while uploading by adding the folder
parameter to your upload API call.
When you use the folder
parameter, the public ID given to the newly uploaded image will begin with the folder name you provided and continue based on the other options you provided for naming the file.
To keep the original filename of the file you uploaded, you can set the use_filename
parameter to true
and Cloudinary will use the file name of the uploaded image for the public ID.
The file name is normalized and random characters are appended to ensure uniqueness.
To skip adding random characters that ensure uniqueness, you can additionally specify the unique_filename
parameter, with the value set to false
Here's an example (in Ruby) of uploading a file, keeping its filename and adding some random characters for uniqueness, and placing it in the specified folder:
Cloudinary::Uploader.upload("sample.jpg", :use_filename => true, :folder => "folder1/folder2")
This will create a URL for your file similar to this:
http://res.cloudinary.com/<cloud_name>/image/upload/1589807462/folder1/folder2/sample_mzzi4k.jpg
Here's an example in Ruby for uploading multiple images using cl_image_upload_tag
:
<%= cl_image_upload_tag(:image_id, :html => { :multiple => true }, :use_filename => true, :folder => "folder1/folder2") %>
Comments
19 comments
If I upload to an folder, it includes the folder in the image URL, but if I check on the Media Library the image is there with the folder in its url, but the new folder has not been created. If I then create the new folder using the create folder button, it creates the folder and any images can then be viewed under that folder.
It would be nice for the folder to automatically be generated, or is there a way via the API?
Hi Frank,
You can go to your Settings page and enable Auto-create folders.
This will automatically synchronize the resources and folders created by the API with the Media library.
Is this "folder" parameter available for the PHP API? I do not see it listed in the API Documentation http://cloudinary.com/documentation/php_image_upload
I guess I can just try it. Also how many images can a folder hold before a new folder should be created? I'll be processing the images and compressing them by 75% first so the files should be fairly small.
Hi Robert,
While it isn't documented yet, indeed the
folder
parameter is available for the PHP API as well. Here's an example:\Cloudinary\Uploader::upload('my_image.jpg', array("folder" => "my_folder"));
You can store as many images as you need, there is no limit per folder.
Awesome thanks. I am about to try this. My local file structure will be different from my cloudinary file structure.
So, could I use this to separate staging and production environments? Or do you have another suggestion for that?
You can indeed use folders to separate the resources from staging and production environments.
However, we usually recommend to have different accounts for staging and production, and copy required images from one account to the other. You can use Cloudinary's Auto-migration feature which you can have your staging account syncing with your production automatically only for your used images. Note that all images of production account will be stored within a folder in your staging account.
For more information:
http://cloudinary.com/blog/how_to_automatically_migrate_all_your_images_to_the_cloud
From our Advanced Extra plan, we support having our multi-accounts feature which allows you to manage up to five different accounts via a single login, where all accounts share the master account's usage quotas.
Hi I am uploading images from the media library and I dont want the appended unique characters for the original file name. Is there any setting for that from the interface.
When you upload images via the Media library to the root directory, Cloudinary indeed automatically adds a short random string to the public ID to ensure uniqueness. For more information:
http://support.cloudinary.com/entries/26977753-Why-does-the-public-ID-include-additional-characters-appended-to-the-original-file-name-
However, when you upload into a folder, the extra string is not added to the public ID.
You can also tell Cloudinary to not add the extra string when uploading the API. For more information:
http://support.cloudinary.com/entries/25830181-How-to-upload-images-while-keeping-their-original-filenames-
UPDATE
You can now set "Use file name in Media Library" in your account's Upload settings page to tell Cloudinary to not add random characters in the root directory either.
Hello Itay,
I use the Auto Upload Feature to get an image.
I want the image name as Public ID and enabled it in the settings.
Now I want to use THIS image as watermark and use it with the overlay parameter "l".
But because I have to choose a folder for Auto upload,
the Public ID always containts the foldername and "/" in it`s name, which I can not use with the overlay parameter.
How can I do it this way ?
I do not want to put hands on the prozess, (the images changes, no solid watermark ! - no renaming e.g. manuell)
Is there a way to choose the root folder for auto upload ?
Or is there an "Auto" Rename Function to get rid of the (/) in the name ?
Thank you so much for your help !
Regards
Hi Michael,
Here's an article that explains how to solve the folders hierarchy matter:
https://support.cloudinary.com/hc/en-us/articles/202521352-How-to-use-an-image-from-a-sub-folder-as-an-overlay-
Moreover, you can map your root directory by putting a slash ("/") inside the "Folder" field.
Hi, i want to upload image to my created custom folder with C#
How to populate with C#.
Here is my code
@Naw
If you would like to upload to a specific folder in Cloudinary via C#, add the Folder parameter as part of the ImageUploadParams:
Hi, is there any example using JS ?
Hi Khaled,
You can specify the folder you wish to upload to in the public_id. For example, using formdata-
The image will be given the public id "my_folder/mysample' meaning it will be in the folder named "my_folder"
var formdata = new FormData();
formdata.append('file', file);
formdata.append('cloud_name', '<cloud_name>');
formdata.append('resource_type', 'image');
formdata.append('upload_preset', '<upload_preset>');
formdata.append('public_id', 'my_folder/mysample')
var xhr = new XMLHttpRequest();
xhr.open('POST', "https://api.cloudinary.com/v1_1/<cloud_name>/image/upload",true);
xhr.send(formdata);
Hello Cloudinary,
How could I specify the upload folder in Python 3 ?
example:
Tristan,
Best regard!
Goal: My goal ist to upload images and a) keep the original name and b) know the exact URL path.
I set the Upload preset to:
When I upload images using the Dashboard Media Library my images keep the original filename (which is great) but cloudinary adds a unique ID "v1585290075" into the URL.
https://res.cloudinary.com/xxxxx/image/upload/ => v1585290075 <= /2020-03-27-ee-apps/xxxxxxx.png
This unique ID prevents me from knowing the exact URL.
Question: How can I avoid this unique ID??
Cheers, Philipp
Hi Philipp,
The v123456789 is not part of the public_id and URLs will work with or without it. It represents the upload time of the asset. It is used as one way of delivering the latest version and avoid any cached copies that may reside on the CDN. The other way would be to use CDN invalidation to clear any cached copies when using URLs without a version number.
The API will always return the URLs with the latest version number, but you don't have to use those and can build the URLs yourself, or use the SDK helper methods we provide to build the URLs for you. However, if you would like to use URLs without a version number, then we would need to update your account settings so that the URLs we send for invalidation at the CDN match the URL format you are using. By default, the URLs that are sent for invalidation use a 'v1' as the version number for resources in folders and no version number for resources in the root. Please see the below article with details on these invalidation options -
https://support.cloudinary.com/hc/en-us/articles/360001208732-What-URL-conventions-are-invalidated
Afterwards, I suggest opening a ticket with us at https://support.cloudinary.com/hc/en-us/requests/new so that we can configure your account to invalidate URLs without a version number. Then you can build the URLs without one and upon deleting and overwriting existing images the CDN cache would be purged of any cached copies.
Please sign in to leave a comment.