小编Ade*_*ANI的帖子

Angular / AWS S3 - 使用预签名 url 将文件上传到 AWS S3

我正在开发一项功能,使用 Angular/presigned url 和 API Gateway/Lambda 将文件上传到 S3,以生成预签名 url。

我的工作流程描述如下:

  1. 从模板中获取选定的文件
  2. 请求我的 api(网关/Lambda)使用文件名生成预签名 URL。

const body = { fileName: this.selectedFile.name } const preSignedUrl = await this.http.post('https://xxxxx.execute-api.eu-west-1.amazonaws.com/dev/v1/profile/avatar ', body).toPromise();

  1. 在生成的预签名 url 上以 PUT 模式发布文件。

后端

我正在使用无服务器(Lambda/API 网关)来计算 presignedUrl。

拉姆达

const AWS = require('aws-sdk')
 
 
module.exports.uploadLargeFile = async (event) => {  
 
  console.log('1. Event: ', event.body);
  const reqBodyAsString = event.body || '{}';
  const fileName = JSON.parse(reqBodyAsString).fileName;
 
  return getUploadURL(fileName);
}
 
const getUploadURL = async (fileName) => {
  const s3Params = {
      Bucket: process.env.AVATAR_BUCKET,
      Expires: 60 …
Run Code Online (Sandbox Code Playgroud)

file-upload amazon-s3 amazon-web-services pre-signed-url angular

7
推荐指数
1
解决办法
3513
查看次数