-
Notifications
You must be signed in to change notification settings - Fork 135
Expand file tree
/
Copy pathupload-image.py
More file actions
54 lines (44 loc) · 1.7 KB
/
Copy pathupload-image.py
File metadata and controls
54 lines (44 loc) · 1.7 KB
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
"""Upload an image and print its public ID and an optimized delivery URL.
Prerequisites: set CLOUDINARY_URL in your environment (Console > Settings > API Keys).
No account? Run examples/provision-claimable-cloud.py first.
Related:
- Task doc: cloudinary/docs/upload-image.md
- Delivery URLs: examples/transform-and-deliver-image.py
"""
import cloudinary
import cloudinary.exceptions
import cloudinary.uploader
# Configuration is read from CLOUDINARY_URL automatically.
SAMPLE_IMAGE = "https://res.cloudinary.com/demo/image/upload/sample.jpg"
def main() -> None:
result = cloudinary.uploader.upload(
SAMPLE_IMAGE,
public_id="examples/uploaded-sample",
tags=["example"],
context={"alt": "A sample image uploaded by the pycloudinary example"},
)
# asset_id never changes; public_id changes on rename or move.
print(f"Stored reference: {result['asset_id']}")
print(f"Public ID: {result['public_id']}")
print(f"Dimensions: {result['width']}x{result['height']} {result['format']}")
print(f"Original URL: {result['secure_url']}")
optimized = cloudinary.CloudinaryImage(result["public_id"]).build_url(
width=400,
height=400,
crop="fill",
gravity="auto",
fetch_format="auto",
quality="auto",
secure=True,
)
print(f"Optimized URL: {optimized}")
if __name__ == "__main__":
try:
main()
except cloudinary.exceptions.Error as error:
print(f"Upload failed: {error}")
raise SystemExit(1)
except ValueError as error:
print(f"Configuration error: {error}")
print("Set CLOUDINARY_URL (Console > Settings > API Keys).")
raise SystemExit(1)