首页 > 解决方案 > AWS Lambda 从 S3 获取文件然后使用它

问题描述

我需要从我的 S3 中提取 2 个文件并在 HttpAgent 中使用它们,我努力寻找正确的方法。使用返回 ReadableStream 的 SDKv3。

const { Readable } = require('stream');
const { createWriteStream } = require('fs');
const { S3Client, GetObjectCommand } = require('@aws-sdk/client-s3');

async (event, context) => {
  let options;
  let httpsAgent;
  try {
    console.log('> Getting content from S3');
    const s3Client = new S3Client({
      region: 'us-east-2',
    });
    let command = new GetObjectCommand({
      Key: 'lib/myCert.crt',
      Bucket: 'mybucket',
    });
    console.log('>>Getting Cert command');
    const hcert = await s3Client.send(command);
   

    command = new GetObjectCommand({
      Key: 'lib/myKey.pem',
      Bucket: 'mybucket',
    });
    console.log('Get Key');
    const ckey = await s3Client.send(command);
  
    console.log('initializing Agent');
    httpsAgent = new https.Agent(
      {
        key: ckey.Body.pipe(createWriteStream('/tmp/myKey.pem')),
        cert: hcert.Body.pipe(createWriteStream('/tmp/myCert.crt')),
        // key: fs.readFileSync('./tmp/myKey.pem'),
        // cert: fs.readFileSync('./tmp/myCert.crt'),
        keepAlive: true
      }
    );

使用上面的代码,我得到了一个 ParameterNotFound。我尝试写入磁盘然后访问(通过 fs.ReadFileSync)但同样的问题。

标签: amazon-s3aws-lambdaaws-sdk-nodejs

解决方案


推荐阅读