Last active
August 10, 2022 21:56
-
-
Save mdayann/b9004a60d8d233098c82e39603c31536 to your computer and use it in GitHub Desktop.
Springboot upload image to cloudinary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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