Skip to content

Commit 88cb15e

Browse files
[Images] Upload an image via a Worker (#17288)
* Corrected example * Added upload an image using a Worker * Implementing feedback * Added more background info to AI image task * Added more info for uploading AI generated image * Update src/content/docs/images/upload-images/upload-file-worker.mdx Co-authored-by: hyperlint-ai[bot] <154288675+hyperlint-ai[bot]@users.noreply.github.com> * Header edit --------- Co-authored-by: hyperlint-ai[bot] <154288675+hyperlint-ai[bot]@users.noreply.github.com>
1 parent 47dbb64 commit 88cb15e

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

src/components/models/code/TextToImageCode.astro

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ export default {
2323
prompt: "cyberpunk cat",
2424
};
2525
26-
const response = await env.AI.run(
26+
const stream = await env.AI.run(
2727
"${name}",
2828
inputs
2929
);
3030
3131
return new Response(response, {
3232
headers: {
33-
"content-type": "image/png",
33+
"content-type": "image/jpg",
3434
},
3535
});
3636
},
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
---
2+
pcx_content_type: how-to
3+
title: Upload via a Worker
4+
5+
---
6+
7+
You can use a Worker to upload your image to Cloudflare Images.
8+
9+
Refer to the example below or refer to the [Workers documentation](/workers/) for more information.
10+
11+
```ts
12+
const API_URL = "https://api.cloudflare.com/client/v4/accounts/<ACCOUNT_ID>/images/v1";
13+
const TOKEN = "<YOUR_TOKEN_HERE>";
14+
15+
const image = await fetch("https://example.com/image.png");
16+
const bytes = await image.bytes();
17+
18+
const formData = new FormData();
19+
formData.append('file', new File([bytes], 'image.png'));
20+
21+
const response = await fetch(API_URL, {
22+
method: 'POST',
23+
headers: {
24+
'Authorization': `Bearer ${TOKEN}`,
25+
},
26+
body: formData,
27+
});
28+
```
29+
30+
## Upload from AI generated images
31+
32+
You can use an AI Worker to generate an image and then upload that image to store it in Cloudflare Images. For more information about using Workers AI to generate an image, refer to the [SDXL-Lightning Model](/workers-ai/models/stable-diffusion-xl-lightning).
33+
34+
```ts
35+
const API_URL = "https://api.cloudflare.com/client/v4/accounts/<ACCOUNT_ID>/images/v1";
36+
const TOKEN = "YOUR_TOKEN_HERE";
37+
38+
const stream = await env.AI.run(
39+
"@cf/bytedance/stable-diffusion-xl-lightning",
40+
{
41+
prompt: YOUR_PROMPT_HERE
42+
}
43+
);
44+
const bytes = await (new Response(stream)).bytes();
45+
46+
const formData = new FormData();
47+
formData.append('file', new File([bytes], 'image.jpg');
48+
49+
const response = await fetch(API_URL, {
50+
method: 'POST',
51+
headers: {
52+
'Authorization': `Bearer ${TOKEN}`,
53+
},
54+
body: formData,
55+
});
56+
```

0 commit comments

Comments
 (0)