-
Notifications
You must be signed in to change notification settings - Fork 10.4k
[Images] Upload an image via a Worker #17288
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
74ec1d3
Corrected example
dcpena 7c9b9c0
Added upload an image using a Worker
dcpena 021a5f5
Implementing feedback
dcpena b000655
Added more background info to AI image task
dcpena 3a2b768
Added more info for uploading AI generated image
dcpena 9cf0789
Update src/content/docs/images/upload-images/upload-file-worker.mdx
dcpena 0d21e26
Header edit
dcpena File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/content/docs/images/upload-images/upload-file-worker.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| --- | ||
| pcx_content_type: how-to | ||
| title: Upload via a Worker | ||
|
|
||
| --- | ||
|
|
||
| You can use a Worker to upload your image to Cloudflare Images. | ||
|
|
||
| Refer to the example below or refer to the [Workers documentation](/workers/) for more information. | ||
|
|
||
| ```ts | ||
| const API_URL = "https://api.cloudflare.com/client/v4/accounts/<ACCOUNT_ID>/images/v1"; | ||
| const TOKEN = "<YOUR_TOKEN_HERE>"; | ||
|
|
||
| const image = await fetch("https://example.com/image.png"); | ||
| const bytes = await image.bytes(); | ||
|
|
||
| const formData = new FormData(); | ||
| formData.append('file', new File([bytes], 'image.png')); | ||
|
|
||
| const response = await fetch(API_URL, { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Authorization': `Bearer ${TOKEN}`, | ||
| }, | ||
| body: formData, | ||
| }); | ||
| ``` | ||
|
|
||
| ## Upload from AI generated images | ||
|
|
||
| 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). | ||
|
|
||
| ```ts | ||
| const API_URL = "https://api.cloudflare.com/client/v4/accounts/<ACCOUNT_ID>/images/v1"; | ||
| const TOKEN = "YOUR_TOKEN_HERE"; | ||
|
|
||
| const stream = await env.AI.run( | ||
| "@cf/bytedance/stable-diffusion-xl-lightning", | ||
| { | ||
| prompt: YOUR_PROMPT_HERE | ||
| } | ||
| ); | ||
| const bytes = await (new Response(stream)).bytes(); | ||
|
|
||
| const formData = new FormData(); | ||
| formData.append('file', new File([bytes], 'image.jpg'); | ||
|
|
||
| const response = await fetch(API_URL, { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Authorization': `Bearer ${TOKEN}`, | ||
| }, | ||
| body: formData, | ||
| }); | ||
| ``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.