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 Answer
1
Accepted Answer

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
answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions