简体   繁体   English

无法上传到我的 Lambda function 内的 AWS S3

[英]Cannot upload to AWS S3 inside my Lambda function

I have the following lambda function.我有以下 lambda function。 It received an XML, looks through it, finds a base64 pdf file and tries to upload it to S3.它收到一个 XML,查看它,找到一个 base64 pdf 文件并尝试将其上传到 S3。

index.js

const AWS = require('aws-sdk');
const xml2js = require('xml2js');
const pdfUpload = require('./upload_pdf');
const s3 = new AWS.S3();

exports.handler = async (event, context, callback) => {
    let attachment;
    xml2js.parseString(event.body, function(err, result) {      
      attachment = 
        result.Attachment[0].Data[0];

      if (attachment) {
        pdfUpload(attachment); 
      }

    });

    return {
      statusCode: 200
    }
};

upload_pdf.js

/**
 *
 * @param  {string}  base64 Data
 * @return {string}  Image url
 */
const pdfUpload = async (base64) => {

  const AWS = require('aws-sdk');
  const s3 = new AWS.S3();
  const base64Data = new Buffer.from(base64, 'base64');

  // With this setup, each time your user uploads an image, will be overwritten.
  // To prevent this, use a different Key each time.
  // This won't be needed if they're uploading their avatar, hence the filename, userAvatar.js.
  const params = {
    Bucket: 'mu-bucket',
    Key: `123.pdf`, 
    Body: base64Data,
    ACL: 'public-read',
    ContentEncoding: 'base64', 
    ContentType: `application/pdf` 
  }

  let location = '';
  let key = '';
  try {
    const { Location, Key } = await s3.upload(params).promise();
    location = Location;
    key = Key;
  } catch (error) {
     // console.log(error)
  }

  console.log(location, key);

  return location;
}

module.exports = pdfUpload;

No matter what I do, the file does not get uploaded.无论我做什么,文件都不会上传。 I have checked the permissions, and the lambda has access to the bucket.我已经检查了权限,并且 lambda 可以访问存储桶。 Running the lambda I'm not receiving any errors either.运行 lambda 我也没有收到任何错误。 Can anybody see what might be wrong here?任何人都可以看到这里可能有什么问题吗?

First, as an advice, I think you should put more logs to see at which steps the function is stuck / failing首先,作为建议,我认为您应该放置更多日志以查看 function 卡住/失败的步骤

The second thing you can try is to put await您可以尝试的第二件事是await

        await pdfUpload(attachment); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM