I am trying to use DreamStudio’s Upscaler API to take an image from the /tmp folder, run it through the upscaler, and send it to a cloudflare URL.
It seems like PipeDream is loading the entire image into memory before sending it to CloudFlare, which is causing me to hit the limit. Is there a way to prevent the image from being loaded into memory and simply point the upscaler API to post the image directly to the cloudflare folder?
Here is my code:
import { axios } from “@pipedream/platform”;
import fs from ‘fs’;
export default defineComponent({
props: {
apiKey: {
type: “string”,
label: “API Key”,
secret: true,
},
cloudflareToken: {
type: “string”,
label: “Cloudflare Token”,
secret: true,
},
inputFilename: {
type: “string”,
label: “Input File Name”,
default: “”,
},
},
async run({ steps, $ }) {
// Read the previously generated image file
const imageData = fs.readFileSync(this.inputFilename);
**const** path = "https://api.stability.ai/v1/generation/esrgan-v1-x2plus/image-to-image/upscale";
**const** headers = {
Accept: "image/png",
Authorization: `Bearer ${**this**.apiKey}`,
"Content-Type": "application/octet-stream",
};
_// Perform the upscale API call_
**const** response = **await** axios($, {
url: path,
headers,
method: "POST",
data: imageData,
responseType: 'arraybuffer'
});
_// Generate a unique filename for the upscaled image_
**const** timestamp = Date.now();
**const** filename = `/tmp/upscaled_image_${timestamp}.png`;
_// Save the upscaled image to the /tmp/ directory_
fs.writeFileSync(filename, Buffer.**from**(response.data));
_// Send the upscaled image to Cloudflare using $.send.http()_
**await** $.send.http({
method: "POST",
url: "https://api.cloudflare.com/client/v4/accounts/myid/images/v1",
headers: {
"Authorization": `Bearer ${**this**.cloudflareToken}`,
},
data: fs.createReadStream(filename),
});
_// Return the filename for use in other steps_
**return** {
filename
};
},
});