javascript - 如何检索 OpenAI 图像并将其保存到 S3 存储桶

标签 javascript typescript amazon-s3 openai-api

我想获取在 OpenAI/Dall E 中生成的图像并将其保存到 S3 存储桶。

到目前为止,我可以获取图像 URL 并创建一个缓冲区,内容如下:

const configuration = new Configuration({
  apiKey: procEnvVars.OPENAI_API_KEY,
});
export const openai = new OpenAIApi(configuration);

const defaultImageParams: CreateImageRequest = {
  n: 1,
  prompt: "a bad request message",
};

interface InputParams extends CreateImageRequest {
  prompt: string; // make this mandatory for the function params
}

// Once we get a URL from the OpenAI API, we want to convert it to a buffer
export async function getBufferFromUrl(openAiUrl: string) {
  const axiosResponse = await axios({
    url: openAiUrl, //your url
    method: "GET",
    responseType: "arraybuffer",
  });
  const data = axiosResponse.data;
  if (!(data instanceof Buffer))
    throw new Error("Axios response should be of type Buffer");

  return data;
}
export async function getUrlFromOpenAi(inputParams: InputParams) {
  const ImageResponse = await openai.createImage({
    ...defaultImageParams,
    ...inputParams,
  });

  const dataArray = ImageResponse.data.data;
  if (!dataArray || dataArray.length === 0) {
    console.error({
      error: "We did not return choices from createOpenAiImage()",
      data: ImageResponse.data,
      datadata: ImageResponse.data.data,
    });
  }
  return dataArray;
}

最佳答案

接下来我们需要获取缓冲区并保存到 S3:

// Create service client module using ES6 syntax.
import { S3Client } from "@aws-sdk/client-s3";
// Set the AWS Region.
const REGION = "eu-west-2";
// Create an Amazon S3 service client object.
const s3Client = new S3Client({ region: REGION });
export { s3Client };

// Import required AWS SDK clients and commands for Node.js.
import { PutObjectCommand } from "@aws-sdk/client-s3";

// Set the parameters.
export const bucketParams = {
  Bucket: "<my s3 bucket name. Can be found in S3 console>",
};

// Create and upload an object to the S3 bucket.
export async function putS3Object(inputParams: { Body: Buffer; Key: string }) {
  try {
    const data = await s3Client.send(
      new PutObjectCommand({
        ...bucketParams,
        Body: inputParams.Body,
        Key: `public/myFolder/${inputParams.Key}`,
      })
    );
    console.log(
      "Successfully uploaded object: " +
        bucketParams.Bucket +
        "/" +
        `public/myFolder/${inputParams.Key}`
    );
    return data; // For unit tests.
  } catch (err) {
    console.log("Error", err);
  }
}


关于javascript - 如何检索 OpenAI 图像并将其保存到 S3 存储桶,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74729716/

相关文章:

javascript - Gulpjs – gulp-jade 编译问题

javascript - 将 VIM omnicomplete 用于带有 ctags 的 javascript

typescript - Aurelia 应用程序中的不活动注销

javascript - TypeScript 可以与 PhoneGap(或类似的)一起使用吗?

构造函数重载的 typescript 智能感知问题

java - S3 上的 Elasticsearch 集群备份

amazon-web-services - 我可以通过 AWS API-Gateway 将表单数据文件上传到 S3 吗?

amazon-s3 - 使用 VBScript 将图像文件直接上传到 s3 Bucket

javascript - 比较对象数组并分配相似度分数

javascript - 快线功能未提升