0
votes

I have configured my project to upload an images to Cloudinary (Cloud storage for images). In my cloudinary account i have created a folder of my application name say MyAppImages. Now my requirement is that my uploaded images through MyApp application should go to MyAppImages folder.

With my current code it is going directly to Samples folder.

My Code:

{
String fileName=multipartFile.getOriginalFilename();
            File physicalFile=new File(multipartFile.getOriginalFilename());
            FileOutputStream fout=new FileOutputStream(fileDir.getName()+"/"+physicalFile);
            fout.write(multipartFile.getBytes());
            fout.close();
            Cloudinary cloudinary = new Cloudinary(ObjectUtils.asMap(
                      "cloud_name", "my_cloud_name",
                      "api_key", "my_key",
                      "api_secret", "my_secret"));

            File toUpload = new File("rowFiles/"+fileName);
            Map params = ObjectUtils.asMap("public_id", "MyAppImages/"+fileName);
            Map uploadResult = cloudinary.uploader().upload(toUpload, params );
            System.out.println("==============>>"+uploadResult.get("url"));
            cloudinaryImgURL=uploadResult.get("url").toString();
}

with above code i am able to upload image to specific directory MyAppImages , but with exactly name with original image name. I want to store it image by cloudinary generated random name, so that if i upload the same image twice it should not override first image with second uploaded image as is happening with above code.

2

2 Answers

0
votes

By default, when using Cloudinary's API for uploading, Cloudinary will set a randomly generated public ID for the uploaded image.

You can set the use_filename parameter to true to set the uploaded image's public ID to be as the original file's name. However, a random string is appended to the image's 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-

0
votes

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

Source : https://support.cloudinary.com/hc/en-us/articles/202520752-Can-I-upload-files-to-a-folder-while-keeping-their-original-file-names-

For more information on the upload API call parameters: https://cloudinary.com/documentation/image_upload_api_reference#required_parameters