|
| 1 | +import Head from 'next/head'; |
| 2 | +import CodeBlock from '../../components/CodeBlock'; |
| 3 | +import OgImage from '../../components/OgImage'; |
| 4 | +import { CldImage } from '../../../next-cloudinary'; |
| 5 | +import HeaderImage from '../../components/HeaderImage'; |
| 6 | +import Video from '../../components/Video'; |
| 7 | + |
| 8 | + |
| 9 | +<Head> |
| 10 | + <title>Remote Images - Next Cloudinary</title> |
| 11 | + <meta name="og:title" content="Delivering Remote Images - Next Cloudinary" /> |
| 12 | + <meta name="og:url" content={`https://next.cloudinary.dev/guides/remote-images`} /> |
| 13 | +</Head> |
| 14 | + |
| 15 | +<OgImage title="Remote Images" twitterTitle="Delivering Remote Images" /> |
| 16 | + |
| 17 | +# Working with Remote Images |
| 18 | + |
| 19 | +You can leverage Cloudinary's powerful transformation and delivery features even for images not stored in your Cloudinary account. |
| 20 | + |
| 21 | +The `CldImage` component provides two primary methods for working with remote images: |
| 22 | + |
| 23 | +## Method 1: Fetching Remote Images On-the-Fly |
| 24 | + |
| 25 | +[The `fetch` delivery method](https://cloudinary.com/documentation/fetch_remote_images) allows you to use a remote image URL as a source for the `CldImage` component. |
| 26 | + |
| 27 | +Cloudinary will fetch the image, apply transformations, cache it on the CDN, and deliver it, all without storing it in your Media Library. |
| 28 | + |
| 29 | +This is the simplest way to get started with remote media. |
| 30 | + |
| 31 | +### Example: |
| 32 | + |
| 33 | +To use this method, provide the remote image URL to the `src` prop and set the `deliveryType` prop to `"fetch"` |
| 34 | + |
| 35 | +<HeaderImage> |
| 36 | + <CldImage |
| 37 | + width="960" |
| 38 | + height="600" |
| 39 | + src="https://www.wikipedia.org/portal/wikipedia.org/assets/img/[email protected]" |
| 40 | + alt="Wikipedia logo" |
| 41 | + tint="70:blue:purple" |
| 42 | + deliveryType="fetch" |
| 43 | + /> |
| 44 | +</HeaderImage> |
| 45 | + |
| 46 | +<CodeBlock> |
| 47 | +```jsx copy showLineNumbers |
| 48 | + import { CldImage } from 'next-cloudinary'; |
| 49 | + |
| 50 | + <CldImage |
| 51 | + width="1080" |
| 52 | + height="675" |
| 53 | + src ="https://www.wikipedia.org/portal/wikipedia.org/assets/img/[email protected]" |
| 54 | + deliveryType="fetch" |
| 55 | + // Apply transformations like any other Cloudinary image |
| 56 | + tint="70:blue:purple" |
| 57 | + alt="Wikipedia Logo" |
| 58 | + /> |
| 59 | + ``` |
| 60 | +</CodeBlock> |
| 61 | + |
| 62 | +This will generate an `<img>` with a `srcset` containing Cloudinary URLs like: |
| 63 | + |
| 64 | +``` |
| 65 | +https://res.cloudinary.com/eric-cloudinary/image/fetch/e_tint:70:blue:purple/c_limit,w_1080/f_auto/q_auto/v1/https://www.wikipedia.org/portal/wikipedia.org/assets/img/[email protected]?_a=BAVAZGGf0 |
| 66 | +``` |
| 67 | + |
| 68 | +This URL instructs Cloudinary to retrieve the image from the source URL. |
| 69 | + |
| 70 | +## Method 2: Auto-Uploading Remote Images |
| 71 | + |
| 72 | +This method automatically uploads an image from a remote source to your Cloudinary Media Library the first time it's requested. |
| 73 | + |
| 74 | +### Use Cases |
| 75 | + |
| 76 | +- Migrating an existing media library from a service like S3 to Cloudinary without downtime |
| 77 | +- Permanently storing and managing remote assets in Cloudinary |
| 78 | +- Building a media library from a trusted, remote source over time |
| 79 | + |
| 80 | +### How to Set Up Auto-Upload Mapping: |
| 81 | + |
| 82 | +1. Navigate to your Cloudinary settings: go to `Settings` > `Upload`. |
| 83 | +2. Find the `Auto upload mapping` section: here you will define the mapping. |
| 84 | +3. Create a new mapping: |
| 85 | +- **Target Folder**: A virtual folder name to use in your src prop (e.g., `s3-images`). |
| 86 | +- **Source URL prefix**: Paste the base URL of your remote image storage (e.g., `https://my-s3-bucket.s3.amazonaws.com/images/`). |
| 87 | +4. Save your changes. |
| 88 | + |
| 89 | +For more details, see the official Cloudinary documentation on [Lazy Migration with Auto-Upload](https://cloudinary.com/documentation/migration#lazy_migration_with_auto_upload). |
| 90 | + |
| 91 | +### Watch & Learn |
| 92 | +<Video |
| 93 | + title="Upload Images & Videos to Cloudinary Automatically with Auto Upload" |
| 94 | + url="https://www.youtube.com/watch?v=LJhsn5A0PFE" |
| 95 | +/> |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | +### Implementation with `CldImage` |
| 100 | + |
| 101 | +Once your mapping is configured, construct the `src` prop by combining your mapped folder with the remote image's path |
| 102 | + |
| 103 | +<CodeBlock> |
| 104 | +```jsx copy showLineNumbers |
| 105 | + import { CldImage } from 'next-cloudinary'; |
| 106 | + |
| 107 | +// Remote URL: https://my-s3-bucket.s3.amazonaws.com/images/product-image.jpg |
| 108 | +// Mapped Folder: s3-images |
| 109 | + |
| 110 | +<CldImage |
| 111 | + width="960" |
| 112 | + height="600" |
| 113 | + src="s3-images/product-image.jpg" |
| 114 | + alt="An image that will be auto-uploaded" |
| 115 | +/> |
| 116 | + ``` |
| 117 | +</CodeBlock> |
0 commit comments