Reputation: 1773
I have a requirement of uploading images to cloudinary
using cloudinary's java API.I have created cloudinary account through which i got api Cloud name
,API Key
,API Secret
. Using these things i am able to upload my images to cloudinary like bellow:
@PostMapping("/uploadPic")
public ResponseEntity<Object> upload(@RequestParam("file") MultipartFile multipartFile){
String cloudinaryImgURL=null;
try {
File fileDir = new File("rowFiles");
if (! fileDir.exists()){
fileDir.mkdir();
}
String fileName=multipartFile.getOriginalFilename();
File physicalFile=new File(multipartFile.getOriginalFilename());
FileOutputStream fout=new FileOutputStream(fileDir.getName()+"/"+physicalFile);
fout.write(multipartFile.getBytes());
fout.close();
//For stack-overflow question using dummy values for credientials.
Cloudinary cloudinary = new Cloudinary(ObjectUtils.asMap(
"cloud_name", "your_cloud_name",
"api_key", "your_api_key",
"api_secret", "your_secret_key"));
File toUpload = new File("rowFiles/"+fileName);
Map params = ObjectUtils.asMap("public_id", "SRWRestImageBase/"+fileName);
Map uploadResult =Singleton.getCloudinary().uploader().upload(toUpload, params);
toUpload.delete();
System.out.println("==============>>"+uploadResult.get("url"));
cloudinaryImgURL=uploadResult.get("url").toString();
} catch (Exception e) {
System.out.println("upload:"+e.getMessage());
// TODO: handle exception
}
return new ResponseEntity<Object>("File uploaded successfully:"+cloudinaryImgURL,HttpStatus.OK);
}
But now my problem is that i keep this code in public git repo, and from there i push this code to Heroku
. But using this method will expose my cloudinary
details like Cloud name
,API Key
,API Secret
to everyone which i don't want.
Looking at cloudinary's documentation for getting started i found the way of using environment variable to store these values and access it from there, but document doesn't guide me properly.
I have used In a Java EE environment you can set an environment variable available to your Java EE container:
CLOUDINARY_URL=cloudinary://{api_key}:{api_secret}@{cloud_name}
This will enable you to receive a Cloudinary instance:
Cloudinary cloudinary = Singleton.getCloudinary();
But i am getting compile-time error for class Singleton
that it is not able to find in any jar. Looking at some other article, this class should com.cloudinary.Singleton
, but my cloudinary jar does not have this class.
I am using cloudinary gradle dependancy as:
// https://mvnrepository.com/artifact/com.cloudinary/cloudinary-http43
compile group: 'com.cloudinary', name: 'cloudinary-http43', version: '1.2.2'
It would be appreciated if someone can guide me in right direction. Thanks in Advance.
Upvotes: 1
Views: 2473
Reputation: 1773
I have found my own solution.
We have to set environment variable CLOUDINARY_URL=cloudinary://{api_key}:{api_secret}@{cloud_name}
and restart eclipse to reflect the environment variable changes and following changes. This way it wont expose my account details to public repo.
@PostMapping("/uploadPic")
public ResponseEntity<Object> upload(@RequestParam("file") MultipartFile multipartFile){
String cloudinaryImgURL=null;
try {
File fileDir = new File("rowFiles");
if (! fileDir.exists()){
fileDir.mkdir();
}
String fileName=multipartFile.getOriginalFilename();
File physicalFile=new File(multipartFile.getOriginalFilename());
FileOutputStream fout=new FileOutputStream(fileDir.getName()+"/"+physicalFile);
fout.write(multipartFile.getBytes());
fout.close();
File toUpload = new File("rowFiles/"+fileName);
Cloudinary cloudinary = new Cloudinary();
System.out.println("API Key:"+cloudinary.config.apiKey);
Map params = ObjectUtils.asMap("public_id", "SRWRestImageBase/"+fileName);
Map uploadResult = cloudinary.uploader().upload(toUpload, params);
toUpload.delete();
System.out.println("==============>>"+uploadResult.get("url"));
cloudinaryImgURL=uploadResult.get("url").toString();
} catch (Exception e) {
System.out.println("upload:"+e.getMessage());
}
return new ResponseEntity<Object>("File uploaded successfully:"+cloudinaryImgURL,HttpStatus.OK);
}
Upvotes: 1