Skip to content

Instantly share code, notes, and snippets.

@mdayann
Last active August 10, 2022 21:56
Show Gist options
  • Save mdayann/b9004a60d8d233098c82e39603c31536 to your computer and use it in GitHub Desktop.
Save mdayann/b9004a60d8d233098c82e39603c31536 to your computer and use it in GitHub Desktop.
Springboot upload image to cloudinary
import com.cloudinary.Cloudinary;
import com.cloudinary.Singleton;
import com.cloudinary.Transformation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.Map;
@Component
public class CloudinaryConfig {
private Cloudinary cloudinary;
@Autowired
public CloudinaryConfig(@Value("${cloud.key}") String key,
@Value("${cloud.secret}") String secret,
@Value("${cloud.name}") String cloud) {
cloudinary = Singleton.getCloudinary();
cloudinary.config.cloudName=cloud;
cloudinary.config.apiSecret=secret;
cloudinary.config.apiKey=key;
}
public Map upload(Object file, Map options){
try {
return cloudinary.uploader().upload(file, options);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public String createUrl(String name, int width,
int height, String action){
return cloudinary.url()
.transformation(new Transformation()
.width(width).height(height)
.border("2px_solid_black").crop(action))
.imageTag(name);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment