By default, when using our API for uploading, Cloudinary will set a randomly generated public ID for the uploaded image. It's also possible to choose this value yourself or have it chosen based on the filename of the file you uploaded.
API
When calling our Upload API, you can set the use_filename
parameter to true
to set the uploaded image's public ID to match the original file's name, normalized, with some random characters added to the end to ensure uniqueness.
It is possible to use the original filename without any additional characters by setting unique_filename
parameter to false along with setting use_filename
parameter to true and Cloudinary will use the normalized original filename without adding random characters for uniqueness.
Here's an example using Cloudinary's Ruby SDK:
Cloudinary::Uploader.upload("my_image.jpg", :use_filename => true, :unique_filename => false)
This example will upload the image with the public ID my_image
. If you already have an asset stored in your account with the same type, resource type, and public_id, it will be overwritten in this case.
Media Library UI / Upload Widget
You can also specify those options in an Upload Preset, including an upload preset which will be used when you upload files using our Upload Widget or our Media Library interface. For more information about setting up Upload Presets for your uploads, please see our Upload Preset Configuration article.
The corresponding options for your Upload Preset if you want the Public ID to be set based on the filename of the uploaded file are:
- Use filename or externally defined Public ID: On
- Unique filename: Off
Uploading files to a folder(while keeping the original filename)
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 by 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
Comments
26 comments
i think it not really keep the file name as it's current name** if** it has more period(s) other than it's file extension. I've test it today, the period will changed into underscore.
for example : image.name.png=>** image_name**.png
is there any solution to keep it as it is? it's real name with period(s) in it.
In Cloudinary we, indeed, replace periods with underscores as part of our naming conventions. Although, you can by-pass it by either explicitly supply the public_id, e.g., (in rails):
Cloudinary::Uploader.upload(" my.image.jpg", :public_id => "my.image")
Or, post-upload, rename the image using the Media-Library or the API, e.g., (in rails):
Cloudinary::Uploader.rename("my_image","my.image")
oh i see. so i supposed to specifically fill in the public_id with image file name
Did it work for you?
If there is any thing else we can assist you with, please feel free to open a ticket:
http://support.cloudinary.com/tickets/new
UPDATE
The system was just updated to support dot-included filenames, so
use_filename
will not replace dots with underscores.Hi
I am trying to upload the file keeping the original filename. Please check what I am doing wrong.
echo cl_image_upload_tag('test', array("tags" => "direct_photo_album", "callback" => $cors_location, "html" => array("multiple" => true),"folder" => "my_folder","unique_filename" => false));
I couldn't find the
use_filename
parameter in the code you shared.Please try the following and let me know if this works for you:
echo cl_image_upload_tag('test', array("tags" => "direct_photo_album", "callback" => $cors_location, "html" => array("multiple" => true),"folder" => "my_folder","use_filename" => TRUE, "unique_filename" => FALSE));
Hi Itay
I am using this in my view on RoR
<%= f.attachinary_file_field :file, as: :attachinary, cloudinary: {use_filename: true, unique_filename: false} %>
but it doesn't work. It is always changing the document name. Could you help me please?
For some reason the underscore characters are deleted from my comment.
this is how i have
http://screencast.com/t/PpWmDIgmQ7co
Hi,
Sorry for the very late reply, we have noticed that this request was left unattended. I see that your issue was handled through a support ticket with Itay:)
Hi there, I have the same problem as Mirta reported...
If you guys gave her the solution, could you please also answer it in here to help other people?
Thx!
Hi Luciano,
Please try to reach us at support@cloudinary.com
Best,
Yaki
Hello,
new here.
I havent found how to change the upload preset for image uploads from my computer, in order to keep the file names unchanged.
"You can also specify those options in an Upload Preset, including an upload preset which will be used when you upload files using our Upload Widget or our Media Library interface. For an example of this, please see our Upload Preset Configuration article."
(That article is now a video created 8 months ago and does not deal with that anymore, I believe
Where/how do I have to add use_filename: true
Hi Torsten,
To change this value in your Upload Preset go to your settings (at the top right) => Click on Upload => Click on "edit" in the desired upload preset => Click on the toggle button in "Use filename or externally defined Public ID" to make it "true".
If you have any more questions please let us know.
Thanks,
Tamara
Hi, I've turned "Use filename or externally defined Public ID" to "On", and "Unique Filename" to "Off" but I'm still getting a random filename when I upload via AppSmith.
Any idea what I might be doing wrong?
Hi @Hoopersclub,
May I please ask you to share a public_id of an asset that you've uploaded which has the random characters appended at the end? We can check the logs on our side and confirm what lead to this.
Hi @aleksandar, thanks for helping out. With some debugging on AppSmith end I figured out that for unsigned upload presets I had to pass public_id inside the parameters. I hadn't figured it out prior to this. Thanks anyway. Working ok!
Hi @Hoopersclub - no worries at all - glad you resolved that!
Hi,
what is the correct regular expression to apply to a string to obtain the exact public_id, using use_filename?
I need to check if a file already exsists, but I need to know the final format of public_id.
ex: "this is (my file) name.mp3" => "this_is_my_file_name.mp3"
Hi @ManzoniFausto,
Thanks for reaching out.
At the time of upload:
If you have set public_id value, then your public_id will have the value you set in this field. Please check the documentation for more information: Upload API
If you have set use_filename as true and unique_filename as false, then your public_id will be the same as file name.
If you have set use_filename as true and unique_filename as true, then your public_id will have random characters at the end of it.
If you have set use_filename as false and unique_filename as true, then your public_id will have random characters.
If you have set use_filename as false and unique_filename as false, then your public_id will have random characters.
Please refer to our Search API documentation in order for you to search by specifying the public_id based on the above conditions(whichever you chose to upload the file).
Alternatively, you can use our Admin API GetResources API call to fetch the list of public_ids in your case and figure out the pattern.
Hope this helps, please let me know if you have any further questions.
Best regards,
Sreenath
Hello,
When I upload an image using Cloudinary's
v2.uploader.upload_stream({use_filename: true})
, it saves the file with the field name instead of the filename.Hey Ankur.
As the upload is consuming a stream, the filename gets lost. What I would recommend is retrieving the filename at some point earlier in the code and passing it as the public_id parameter when using upload_stream().
I hope this helps, but if you have any further queries, the best thing to do would be to raise a support ticket with us.
All the best,
-Danny
When I apply and add options i.e. setting the public_id, the secure_url I receive only returns a dot of an image and also shows apect ratio of 1x1. Kindly help!
Hi Joshua,
As Danny mentioned, it would be better at this point if you could open a support ticket with us. Please share your cloud name and your code, so that we can take a look and help you to debug the issue.
https://support.cloudinary.com/hc/en-us/requests/new
Kind regards,
Tia
hi, i used cloudinary generated public_id for uniqueness, and there is this setting that in my impression would keep the original file name somewhere in the metadata or else, but i don't see the data when i pull the media detail from cloudinary, is this the intended behavior? when do this setting applied (as in what condition that this setting works)?
Hi Toh Xue Zun,
Cloudinary assigns a randomly generated unique public ID to each uploaded media asset. Alternatively, you can either define your own custom public ID or one based on the original file name of the uploaded image. If you upload an image with a public ID that already exists, the file will be overwritten.
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 as false .
The “Discard original file name” setting does not store the original file name in the metadata. Instead, whether to discard the name of the original uploaded file. Relevant when delivering assets as attachments (setting the flag transformation parameter to attachment ). Default: false.
For example, in the below link, I have added `fl_attachment` in order to download the asset, but the asset is downloaded with a random name.
https://res.cloudinary.com/wissamkhalilicloudinary/image/upload/fl_attachment/images/Flowers.jpg
I hope it makes sense.
Please let me know if you have additional questions.
Thanks, Wissam
Please sign in to leave a comment.