Search code examples
node.jstypescriptamazon-s3koa

Uploading files with Koa and Typescript to S3 compatible Storage


I am trying to upload an image to the S3 compatible storage from Stackpath. I am using Koa and Typescript. I found an example on medium. I am discovering multiple issues, I am not sure how to declare the type of key, url and fileName, filePath and fileType. Also the variable endpoint is not assignable to a property of type string.

Error: TS2345 Argument of type endpoint '{endpoint: Endpoint; accessKeyId: string;}' is not assignable to parameter of type 'ClientConfiguration'.

I have ignored this issues with @ts-ignore but would like to fix them. Even when I ignore this issues to gave the code a try and send a image with postman, file.name and so on are undefined.

import fs from 'fs'
import { BaseContext } from 'koa'
import aws from 'aws-sdk'

const accessKeyId = '***********************'
const secretAccessKey = '*************************'

const bucketName = 'img-example'
const endpoint = new aws.Endpoint('s3.eu-central.stackpathstorage.com')

export async function upload(ctx: BaseContext) {
  const file = ctx.request.files.file
  const { key, url } = await _upload({
    fileName: file.name,
    filePath: file.path,
    fileType: file.type
  })
  ctx.body = { key, url }
}

function _upload({ fileName, filePath, fileType }) {
  return new Promise((resolve, reject) => {
    const s3 = new aws.S3({
      endpoint,
      accessKeyId,
      secretAccessKey
    })

    const stream = fs.createReadStream(filePath)
    stream.on('error', function(err) {
      reject(err)
    })

    s3.upload(
      {
        ACL: 'public-read',
        // You'll input your bucket name here
        Bucket: bucketName,
        Body: stream,
        Key: fileName,
        ContentType: fileType
      },
      function(err: any, data: { Key: any; Location: any }) {
        if (err) {
          reject(err)
        } else if (data) {
          resolve({ key: data.Key, url: data.Location })
        }
      }
    )
  })
}

Solution

  • The problem was I used koa-body. Koa mulder fixed my problem. Just use Koa mulder as middleware if you use form-data.