如何在Java中使用环境变量值访问cloudinary API?

2019年6月29日 30点热度 0条评论

我需要使用cloudinary的java API将图像上传到cloudinary。我已经创建了cloudinary帐户,通过该帐户我获得了api Cloud nameAPI KeyAPI Secret。使用这些东西,我可以像下面这样将图像上传到cloudinary:

@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);
    }

但是现在我的问题是我将此代码保存在公共(public)git repo中,然后从那里将代码推送到
Heroku。但是,使用此方法会将我的
cloudinary详细信息(如
Cloud name
API Key
API Secret)暴露给我不需要的每个人。

查看cloudinary的
getting started文档,我发现了使用环境变量存储这些值并从那里访问它的方法,但是文档不能正确地指导我。

我用过了

在Java EE环境中,可以设置Java EE容器可用的环境变量:

CLOUDINARY_URL=cloudinary://{api_key}:{api_secret}@{cloud_name}

这将使您能够接收Cloudinary实例:

Cloudinary cloudinary = Singleton.getCloudinary();

但是我收到类
Singleton的编译时错误,它无法在任何jar中找到。在其他文章中,此类应为
com.cloudinary.Singleton,但我的cloudinary jar没有此类。

我正在使用cloudinary gradle依赖性:

// https://mvnrepository.com/artifact/com.cloudinary/cloudinary-http43
compile group: 'com.cloudinary', name: 'cloudinary-http43', version: '1.2.2'

如果有人可以指导我正确的方向,将不胜感激。

提前致谢。

解决方案如下:

我找到了自己的解决方案。
我们必须设置环境变量CLOUDINARY_URL=cloudinary://{api_key}:{api_secret}@{cloud_name}并重新启动eclipse以反射(reflect)环境变量的更改和后续更改。这样,它不会将我的帐户详细信息公开给公共(public)仓库。

@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);
}