GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — cloudinary_java/examples/UploadImage.java at master · cloudinary/cloudinary_java

Expand file tree

/

UploadImage.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
72 lines (63 loc) · 2.85 KB
/

UploadImage.java

Copy path

File metadata and controls

72 lines (63 loc) · 2.85 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
* Upload an image and print its delivery URL.
*
* Prerequisites
* - CLOUDINARY_URL set in the environment:
* export CLOUDINARY_URL="cloudinary://<api_key>:<api_secret>@<cloud_name>"
* - cloudinary-http5 (and its transitive cloudinary-core) on the classpath.
*
* Run
* javac -cp "<classpath>" -d out examples/UploadImage.java
* java -cp "<classpath>:out" UploadImage
*
* How this differs in a real project
* A real application would take the file from a request rather than a constant, would not
* print credentials-adjacent data, and would persist ASSET_ID in its own datastore instead
* of relying on the public ID.
*
* Docs: docs/upload-image.md
*/
import com.cloudinary.Cloudinary;
import com.cloudinary.Transformation;
import com.cloudinary.utils.ObjectUtils;
import java.io.IOException;
import java.util.Map;
public class UploadImage {
private static final String SOURCE_URL =
"https://res.cloudinary.com/demo/image/upload/sample.jpg";
private static final String PUBLIC_ID = "examples/upload_image";
public static void main(String[] args) {
try {
Cloudinary cloudinary = new Cloudinary();
if (cloudinary.config.cloudName == null) {
System.err.println("No Cloudinary configuration found. Set CLOUDINARY_URL.");
System.exit(1);
}
Map result = cloudinary.uploader().upload(SOURCE_URL, ObjectUtils.asMap(
"public_id", PUBLIC_ID,
"overwrite", true));
System.out.println("public_id: " + result.get("public_id"));
System.out.println("asset_id: " + result.get("asset_id"));
System.out.println("format: " + result.get("format"));
System.out.println("dimensions: " + result.get("width") + "x" + result.get("height"));
System.out.println("secure_url: " + result.get("secure_url"));
// Append the format the server reported, so the URL names a real file. A bare
// public ID would produce ".../examples/upload_image" with no extension.
String optimized = cloudinary.url()
.transformation(new Transformation()
.width(400).crop("fill").gravity("auto")
.fetchFormat("auto").quality("auto"))
.generate(PUBLIC_ID + "." + result.get("format"));
System.out.println("optimized: " + optimized);
} catch (IllegalArgumentException e) {
System.err.println("Configuration error: " + e.getMessage());
System.exit(1);
} catch (IOException e) {
System.err.println("I/O error: " + e.getMessage());
System.exit(1);
} catch (RuntimeException e) {
System.err.println("Upload failed: " + e.getMessage());
System.exit(1);
}
}
}
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.