【问题标题】:NestJS - How to upload image to aws s3?NestJS - 如何将图像上传到 aws s3?
【发布时间】:2020-08-07 15:41:23
【问题描述】:

我正在尝试使用 NestJS API 上的 multer-s3 将图像上传到 aws s3。我也试过aws-sdk。我使用 FileInterceptor 和 UploadedFile 装饰器来捕获文件请求。到目前为止,我所拥有的是:

// Controller
 @Post()
 @UseInterceptors(FileInterceptor('file', multerOptions))
    uploadImage(@UploadedFile() file) {
        console.log(file);
    }

// multerOptions. multer.ts file
const configService = new ConfigService();

export const accessParams = {
    accessKeyId: configService.get('AWS_ACCESS_KEY_ID'),
    secretAccessKey: configService.get('AWS_SECRET_ACCESS_KEY'),
    region: configService.get('AWS_REGION'),
};

const imageMimeTypes = [
    'image/jpg',
    'image/jpeg',
    'image/png',
    'image/bmp',
];

AWS.config.update(accessParams);
export const s3 = new AWS.S3();

export const multerOptions = {
    fileFilter: (req: any, file: any, cb: any) => {
        const mimeType = imageMimeTypes.find(im => im === file.mimetype);

        if (mimeType) {
            cb(null, true);
        } else {
            cb(new HttpException(`Unsupported file type ${extname(file.originalname)}`, HttpStatus.BAD_REQUEST), false);
        }
    },
    storage: multerS3({
        s3: s3,
        bucket: configService.get('S3_BUCKET_NAME'),
        acl: 'read-public',
        metadata: function (req, file, cb) {
            cb(null, { fieldName: file.fieldname })
        },
        key: (req: any, file: any, cb: any) => {
            cb(null, `${Date.now().toString()}/${file.originalname}`);
        },
        contentType: multerS3.AUTO_CONTENT_TYPE
    }),
};

这给了我以下错误:

{
  "message": null,
  "code": "InvalidArgument",
  "region": null,
  "time": "2020-04-24T05:34:19.009Z",
  "requestId": "DH224C558HTDF8E3",
  "extendedRequestId": "JKHKJH6877-LKJALDNC765llLKAL=",
  "statusCode": 400,
  "retryable": false,
  "retryDelay": 6.790294010827713,
  "storageErrors": []
}

有什么想法吗?谢谢。

【问题讨论】:

    标签: typescript amazon-s3 nestjs multer multer-s3


    【解决方案1】:

    带有类型和流:

    import { ReadStream } from 'fs';
    import * as AWS from 'aws-sdk';
    import { PromiseResult } from 'aws-sdk/lib/request';
    import { Injectable, InternalServerErrorException } from '@nestjs/common';
    import { ConfigService } from '@nestjs/config';
    
    import { TConfig, TStorageConfig } from '../../config';
    
    @Injectable()
    export class StorageService {
        private S3: AWS.S3;
        private BUCKET: string;
    
        constructor(private configService: ConfigService<TConfig>) {
            this.S3 = new AWS.S3({
                // Your config options
                accessKeyId: this.configService.get<TStorageConfig>('storage').accessKeyId,
                secretAccessKey: this.configService.get<TStorageConfig>('storage').secretAccessKey,
                endpoint: this.configService.get<TStorageConfig>('storage').endpoint,
                s3ForcePathStyle: true,
                signatureVersion: 'v4',
            });
            this.BUCKET = this.configService.get<TStorageConfig>('storage').bucket;
        }
    
        async getBlob(key: string): Promise<PromiseResult<AWS.S3.GetObjectOutput, AWS.AWSError>> {
            const params = { Bucket: this.BUCKET, Key: key };
            const blob = await this.S3.getObject(params).promise();
            
            return blob;
        }
    
        async putBlob(blobName: string, blob: Buffer): Promise<PromiseResult<AWS.S3.PutObjectOutput, AWS.AWSError>> {
            const params = { Bucket: this.BUCKET, Key: blobName, Body: blob };
            const uploadedBlob = await this.S3.putObject(params).promise();
    
            return uploadedBlob;
        }
    
        // to get stream you can use file.createReadStream()
        async putStream(key: string, stream: ReadStream): Promise<AWS.S3.PutObjectOutput> {
            const file = await new Promise<AWS.S3.PutObjectOutput>((resolve, reject) => {
                const handleError = (error) => {
                    reject(error);
                };
                const chunks: Buffer[] = [];
    
                stream.on('data', (chunk: Buffer) => {
                    chunks.push(chunk);
                });
    
                stream.once('end', async () => {
                    const fileBuffer = Buffer.concat(chunks);
    
                    try {
                        const uploaded = await this.putBlob(key, fileBuffer);
    
                        resolve(uploaded);
                    } catch (error) {
                        handleError(new InternalServerErrorException(error));
                    }
                });
    
                stream.on('error', (error) => handleError(new InternalServerErrorException(error)));
            });
    
            return file;
        }
    }
    

    【讨论】:

      【解决方案2】:

      你可以创建一个类似的控制器

      import { Post, UseInterceptors, UploadedFile } from '@nestjs/common';
      
      @Post('upload')
      @UseInterceptors(FileInterceptor('file'))
      async upload(@UploadedFile() file) {
        return await this.service.upload(file);
      }
      

      你的服务应该是这样的

      import { S3 } from 'aws-sdk';
      import { Logger, Injectable } from '@nestjs/common';
      
      @Injectable()
      export class FileUploadService {
          async upload(file) {
              const { originalname } = file;
              const bucketS3 = 'my-aws-bucket';
              await this.uploadS3(file.buffer, bucketS3, originalname);
          }
      
          async uploadS3(file, bucket, name) {
              const s3 = this.getS3();
              const params = {
                  Bucket: bucket,
                  Key: String(name),
                  Body: file,
              };
              return new Promise((resolve, reject) => {
                  s3.upload(params, (err, data) => {
                  if (err) {
                      Logger.error(err);
                      reject(err.message);
                  }
                  resolve(data);
                  });
              });
          }
      
          getS3() {
              return new S3({
                  accessKeyId: process.env.AWS_ACCESS_KEY_ID,
                  secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
              });
          }
      }
      

      【讨论】:

      • 感谢您的帮助,我将为您的答案投票,因为它是正确的,但最后我能够根据自己的喜好使用 multer-s3 解决它
      • 在最新版本中,当@UseInterceptors(FilesInterceptor("files")) 给出它显示错误:如果拦截器行被注释,则连接 ECONNREFUSED 127.0.0.1:4001 代码进入控制器没有文件和错误.对此有任何解决方案的想法吗?
      • 有没有办法为上传的文件定义对公共的访问权限?
      • 非常感谢!我在这个任务上花了 4 天!你是 js 上帝!
      猜你喜欢
      • 2018-05-12
      • 1970-01-01
      • 1970-01-01
      • 2020-12-23
      • 2018-01-19
      • 2017-01-29
      • 1970-01-01
      • 2018-03-02
      • 2018-03-16
      相关资源
      最近更新 更多