是否可以通过JavaScript中的API Gataway将文件对象传递到AWS Lambda?

问题描述 投票:1回答:1

是否可以通过API网关将JavaScript File Object从浏览器传递到AWS Lambda(最终传递到S3)。详细信息在下面。 (就我而言,正是在TypeScript和React中)

环境

前端

  • TypeScript
  • 反应
  • AWS Amplify(使用API​​模块)

后端(AWS Lambda)

  • Node.js

期待什么

File Object从浏览器获得的<input type='file'>传递到API网关的后端AWS Lambda。

发生了什么

[console.log(event)在后端,日志输出为空。

我想知道

如何正确地将File Object(Blob)传递给lambda的方法。 (只是指向文档的链接,它也对我有很大帮助。)

代码(前端)

import { API } from 'aws-amplify'
...
const file = { body: this.state.file }
console.log(file) // output was collect File Object at this time
API.post(<API_NAME>, <PATH>, file)
  .then(response => {
    console.log(response)
  }).catch(error => {
    console.log(error)
  })

代码(后端)

exports.lambdaHandler = function (event, context, callback) {
  console.info(`event.body: ${event.body}`) // got `event.body: {}` at Cloud Watch Logs
  ...
}

是否可以通过设置Content-Type或其他方式来尝试做什么?有文件吗? (我找不到任何文档。我只开始编程了一年……)

如果缺少任何资源,例如版本,我正在使用的库或任何其他资源,请告诉我。我会尽快回复。

非常感谢您对我的帮助。

javascript reactjs typescript aws-lambda
1个回答
0
投票

您必须将文件直接上传到s3到特定的存储桶,并在此特定存储桶上的s3上传上创建lambda触发事件,

通过这种方式,lambda会在您每次发送文件时触发,您可以在其中读取文件并进行处理

请阅读https://docs.aws.amazon.com/lambda/latest/dg/with-s3.html

© www.soinside.com 2019 - 2024. All rights reserved.