How do I upload an AI generated images to cloudflare images

This code fails.

3113 var ai = new Ai(env.AI);
3114 var inputs = { prompt: “cyberpunk cat”, };
3115 var image = await ai.run( “@cf/stabilityai/stable-diffusion-xl-base-1.0”, inputs);
3116 const arrayBuffer = await image.arrayBuffer();
3117 const blob = new Blob([arrayBuffer]);
3118 var body = new FormData();
3119 body.append(“file”, blob, “xxxx”);
3120 body.append(“id”, “aa-bb-cc”);
3121
-122 const result = await fetch(https://api.cloudflare.com/client/v4/accounts/${env.CLOUDFLARE_ID}/images/v1,
-123 {
5124 method: “POST”,
5125 headers:
-126 {
6127 “Authorization”: Bearer ${env.CLOUDFLARE_IMAGE_TOKEN},
6128 ‘X-Auth-Key’: ${env.CLOUDFLARE_AUTH_KEY},
6129 ‘X-Auth-Email’: ${env.CLOUDFLARE_AUTH_EMAIL},
6130 },
5131 body: body
5132 });
3133

3134 var json = await result.json();
3135 return new Response(JSON.stringify(json),{headers});

Hi there,

It seems you’re trying to use X-Auth-Key/X-Auth-Email with a Bearer token at the same time.
This might be your issue.

Take care.

1 Like

Still not working. Could you paste in a code snippit that you know works that creates an AI generated image from the samples and then uploads it to Cloudflare images? I can get all that working separately, but not together as a complete sample. Suggests that you add that as a sample to the samples page to. Thanks.

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.