Cannot Get Error from getObject API to download/get an object(audio file) from S3

0

My call recordings of amazon connect are being stored on S3 in .wav file, i am looking to get and play those recordings in a third party application. For this i am using getObject API & trying to get/download the .wav files by name but i am getting the Cannot Get Error. At the same time i do want to provide the path in key e.g. Connect/Lab/2022/04/08/abc.wav, is it possible? How to resolve it? here is my code:

require("dotenv").config();
const expres = require("express");
const app = expres();
app.listen(3001);

const aws = require("aws-sdk");

aws.config.update({
    secretAccessKey: process.env.ACCESS_SECRET,
    accessKeyId: process.env.ACCESS_KEY,
    region: process.env.REGION
})

const BUCKET = process.env.BUCKET
const Key = '/connect/oblab2/CallRecordings/2022/04/08/'
const s3 = new aws.S3(secretAccessKey = process.env.ACCESS_SECRET, accessKeyId = process.env.ACCESS_KEY);

 app.get("/download/filename", async(req, res)=>{
    const filename = req.params.filename
    let x = await s3.getObject({Bucket:BUCKET, Key:Key + filename}).promise();
    res.send(x.Body);
})
1개 답변
1
수락된 답변

Achieved it through:

require("dotenv").config();
const aws = require('aws-sdk');

const expres = require("express");
const app = expres();

app.listen(3001);

app.get('/getfilefromS3', async (req, res, next) => {

  aws.config.update({
    secretAccessKey: process.env.ACCESS_SECRET,
    accessKeyId: process.env.ACCESS_KEY,
    region: process.env.REGION
  })

  const s3 = new aws.S3(secretAccessKey = process.env.ACCESS_SECRET, accessKeyId = process.env.ACCESS_KEY);

  var params = { Bucket: process.env.BUCKET, Key: "connect/oblab2/CallRecordings/2022/04/08/" + req.query.filename };

  s3.getObject(params, function (err, data) {
    if (err) {
      res.status(200);
      res.end('Error Fetching File');
    }
    else {
      res.attachment(params.Key); // Set Filename
      res.type(data.ContentType); // Set FileType
      res.send(data.Body);        // Send File Buffer
    }
  });
})```

And than hitting endpoint:
http://localhost:3001/getfilefromS3?filename=filename.wav
답변함 2년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠