簡體   English   中英

如何使用 Node 從 SAM Lambda function 在 S3 中寫入 json 文件

[英]How can I write a json file in S3 from a SAM Lambda function with Node

我正在嘗試使用 Node 從 Lambda function 保存一個 json 文件,但該文件未顯示在我的 s3 存儲桶中。 根據我的發現,我必須對我的 template.yaml 文件進行一些更改,但我不知道它是什么。 我從 api 郵寄電話中得到了積極回應,但存儲桶中沒有任何反應。

我怎么解決這個問題?

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  webhook

  Sample SAM Template for webhook
  
Globals:
  Function:
    Timeout: 3

Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function 
    Properties:
      CodeUri: webhook_nath/
      Handler: app.lambdaHandler
      Runtime: nodejs14.x
      Architectures:
        - x86_64
      Events:
        HelloWorld:
          Type: Api
          Properties:
            Path: /webhook
            Method: POST
    Metadata: # Manage esbuild properties
      BuildMethod: esbuild
      BuildProperties:
        Minify: true
        Target: "es2020"
        Sourcemap: true
        EntryPoints: 
        - app.ts

import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
import  aws  from 'aws-sdk';

const s3 = new aws.S3();

export const lambdaHandler = async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
    let response: APIGatewayProxyResult;

    try {

        s3.putObject({
            
            Body: JSON.stringify(event.body),
            Bucket: 'new-bucket-nath',
            Key: 'file_name.json',
            ContentType:'application/json'
           
        }).promise();   
    
        response = {
            statusCode: 200,
            body: JSON.stringify({
                message: 'worked',
            }),
        };
    } catch (err) {
        console.log(err);
        response = {
            statusCode: 500,
            body: JSON.stringify({
                message: 'some error happened',
            }),
        };
    }

    return response;
};

對 s3.putObject 的調用需要等待。

'等待 s3.putObject'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM