how can i download a file from s3 with nestjs?

Viewed 683

how can i download a file from s3 with nestjs? i use the multer to upload the files to s3. Is there any way to download from s3 with nestjs? if i use multer or other methods, can i make this? in nestjs document, they don't have anything about download the files.

import { Req, Res, Injectable } from '@nestjs/common';
import * as multer from 'multer';
import * as AWS from 'aws-sdk';
import * as multerS3 from 'multer-s3';
const AWS_S3_BUCKET_NAME = 'nestjs-s3';
const s3 = new AWS.S3();
AWS.config.update({
  accessKeyId: ,
  secretAccessKey: ,
});
@Injectable()
export class ImageUploadService {
  constructor() {}
  async fileUpload(@Req() req, @Res() res) {
    try {
      this.s3upload(req, res, function (error) {
        if (error) {
          console.log(error);
          return res.status(404).json(`Failed to upload image file: ${error}`);
        }
        return res.status(201).json(req.files[0].location);
      });
    } catch (error) {
      console.log(error);
      return res.status(500).json(`Failed to upload  file: ${error}`);
    }
  }
  s3upload = multer({
    storage: multerS3({
      s3: s3,
      bucket: AWS_S3_BUCKET_NAME,
      acl: 'public-read',
      key: function (request, file, cb) {
        console.log(file);
        cb(null, `${Date.now().toLocaleString()} -- ${file.originalname}`);
      },
    }),
  }).array('upload', 1);

  s3download = multer({
    storage: multerS3({
      s3: s3,
      bucket: AWS_S3_BUCKET_NAME,
      acl: 'public-read',
      contentDisposition: 'attachment',
      key: function (request, file, cb) {
        console.log(file);
        cb(null, `${Date.now().toLocaleString()} -- ${file.originalname}`);
      },
    }),
  }).array('upload', 1);
0 Answers
Related