replicate/replicate-javascript

Docs: An example showing how to upload the output image to s3

Opened this issue · 0 comments

An example would be great in the docs that shows how to stream the result to s3 and get the correct content type for the image. I am correctly creating a Buffer and using that. But it would be great if I could stream the result.

My bad code:

export async function getBuffer(
  result: FileOutput
): Promise<Uint8Array<ArrayBuffer>> {
  const blob = await result.blob()
  const arrayBuffer = await blob.arrayBuffer()
  const buffer = new Uint8Array(arrayBuffer)
  return buffer
}

export async function getContentType(
  buffer: Uint8Array<ArrayBuffer>
): Promise<string> {
  if (buffer.length < 4)
    throw new Error('Content type not found: buffer.length < 4')

  // JPEG: starts with FF D8 FF
  if (buffer[0] === 0xff && buffer[1] === 0xd8 && buffer[2] === 0xff)
    return 'image/jpeg'
//..............
}

const command = new PutObjectCommand({
  Bucket: ...,
  Key: ...,
  Body: buffer,
  ContentType:contentType,
})