Hello, I am using the Open Ai Api to generate images (DALL-E) model, it returns an URL. I want to download this image and upload it to my S3 bucket, then save it into my Images table in my Postgres DB. Any idea how to download the image from the url that the OpenAI Api returns? AFAIK this url last for 1 week or less, that's why I need to download the file and upload it to my s3 bucket
#Download the generated image from DALL-E and upload it to my bucket
1 messages · Page 1 of 1 (latest)
Hi @shell herald. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
Hmm, I don't know enough to give you a confident answer yet. However, I can provide some guidance based on the information available.
To upload files to AWS S3 with Vercel, you can use the AWS SDK. Here is an example of how to generate a pre-signed POST URL for uploading files to S3:
import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3';
import { createPresignedPost } from '@aws-sdk/s3-presigned-post';
import { NextApiRequest, NextApiResponse } from 'next';
export default async function handler(
req: NextApiRequest,
res: NextApiResponse,
) {
const s3Client = new S3Client({});
const post = await createPresignedPost(s3Client, {
Bucket: process.env.S3_BUCKET_NAME,
Key: req.query.file,
Fields: {
acl: 'public-read',
'Content-Type': req.query.fileType,
},
Expires: 600, // seconds
Conditions: [
['content-length-range', 0, 1048576], // up to 1 MB
],
});
res.status(200).json(post);
}
(Source: Vercel Guide (https://vercel.com/guides/how-can-i-use-aws-s3-with-vercel))
As for downloading the image from the URL returned by the OpenAI API, you would typically use a library like node-fetch or axios to make a GET request to the URL and save the response data as a file. However, the specifics of this process are not covered in the provided extracts.
Once you have the image file, you can use the pre-signed POST URL to upload the file to your S3 bucket. After the file is uploaded, you can save the S3 URL of the file in your Postgres database.