Last updated: Nov-14-2022
This quick start is intended to let you quickly try using several common Cloudinary features. It doesn't necessarily employ coding best practices and the code you create here isn't intended for production.
You can perform this quick start in a code sandbox or in a clean project in the dev environment of your choice.
You can also view the completed code of this quick start in a GitHub repo.
You may also find our Glossary helpful to understand Cloudinary-specific terminology.
To perform the steps in this quick start, you'll need:
- A Cloudinary account. If you don't have one yet, you can quickly register for free.
- Your product environment credentials. You can find your credentials in the Dashboard page of your Cloudinary Console.
- A working Java development environment with a supported version of Java.
- A clean java project using Maven.
Add the cloudinary and dotenv dependencies to the list of dependencies in
<dependency> <groupId>com.cloudinary</groupId> <artifactId>cloudinary-http44</artifactId> <version>1.32.2</version> </dependency> <dependency> <groupId>com.cloudinary</groupId> <artifactId>cloudinary-taglib</artifactId> <version>1.32.2</version> </dependency> <dependency> <groupId>io.github.cdimascio</groupId> <artifactId>dotenv-java</artifactId> <version>2.2.4</version> </dependency>
Then, in your project, create a file called
.env and add the following line with your Cloudinary environment variable (replace
cloudinary://<API_KEY>:<API_SECRET>@<CLOUD_NAME> below with your own environment variable value):
// Copy and paste your API environment variable CLOUDINARY_URL=cloudinary://<API_KEY>:<API_SECRET>@<CLOUD_NAME>
In your code, include the following
Cloudinary libraries as well as the
dotenv library in your project. Copy and paste the following code into your
// Import the required packages import com.cloudinary.*; import com.cloudinary.utils.ObjectUtils; import io.github.cdimascio.dotenv.Dotenv; import java.util.Map;
You can now load your Cloudinary credentials from your .env file as shown below. Copy and paste the following code into your Main class:
// Set your Cloudinary credentials Dotenv dotenv = Dotenv.load(); Cloudinary cloudinary = new Cloudinary(dotenv.get("CLOUDINARY_URL")); cloudinary.config.secure = true; System.out.println(cloudinary.config.cloudName);
Copy and paste the following code into a
try block within your Main class:
// Upload the image Map params1 = ObjectUtils.asMap( "use_filename", true, "unique_filename", false, "overwrite", true ); System.out.println( cloudinary.uploader().upload("https://cloudinary-devs.github.io/cld-docs-assets/assets/images/coffee_cup.jpg", params1));
// Get the asset details Map params2 = ObjectUtils.asMap( "quality_analysis", true ); System.out.println( cloudinary.api().resource("coffee_cup", params2));
// Create the image tag with the transformed image and log it to the console System.out.println( cloudinary.url().transformation(new Transformation() .crop("pad") .width(300) .height(400) .background("auto:predominant")) .imageTag("coffee_cup")); // The code above generates an HTML image tag similar to the following: // <img src='https://res.cloudinary.com/demo/image/upload/b_auto:predominant,c_pad,h_400,w_300/coffee_cup' height='400' width='300'/>
Run your code by clicking the Run button on the
You can use the returned image tag to display the image on your website. For now, copy and paste the URL to see the transformed image in the browser:
You can find the full code example for this on GitHub.
- Learn more about the Java SDK by visiting the other pages in this SDK guide.
- Get comprehensive details about Cloudinary features and capabilities:
- Media upload guide: Provides details and examples of the upload options.
- Image transformations guide: Provides details and examples of the transformations you can apply to image assets.
- Video transformations guide: Provides details and examples of the transformations you can apply to video assets.
- Transformation URL API Reference: Provides details and examples of all available transformation parameters.
- Admin API guide: Provides details and examples of the methods available for managing and organizing your media assets.