将大小大于5 GB的文件上传到AWS S3

时间:2019-02-21 07:38:57

标签: node.js amazon-s3 loopback

我正在使用S3存储。通过Node,Loopback4和客户端进行文件上传,我使用Angular 6。

我能够将文件上传到S3,最大容量为5GB。一切正常。但是无法上传大于5 GB的文件。

在我的工作代码下方

const s3 = new AWS.S3({
  accessKeyId: process.env.BUCKET_ACCESS_KEY_ID,
  secretAccessKey: process.env.BUCKET_SECRET_ACCESS_KEY,
  region: process.env.BUCKET_REGION,
});

@post('/upload-multipart', {
    responses: {
      '200': {
        description: 'Todo model instance',
        content: { 'application/json': { schema: { 'x-ts-type': null } } },
      },
    },
  })
  async upload_multiparty(
    @requestBody({
      description: 'multipart/form-data value.',
      required: true,
      content: {
        'multipart/form-data': {
          'x-parser': 'stream',
          schema: { type: 'object' },
        },
      },
    })
    request: Request,
    @inject(RestBindings.Http.RESPONSE) response: Response,
  ): Promise<void> {
    const startTime:Date = new Date();
    const form = new multiparty.Form();
    form.on('part', function (part) {
      if (!part.filename) {
        // filename is not defined when this is a field and not a file
        console.log('got field named ' + part.name);
        part.resume();
      }
      let fileName = "";

      if (part.filename) {
        if (!fileName) fileName=part.filename;

    console.log('File upload start...');
        var opts = {
          Bucket: process.env.BUCKET_NAME,
          Key: 'Test/' + fileName,
          Body: part,
          ContentLength: part.byteCount,
        };
        s3.putObject(opts, function (err, data) {
          if (err) {
            console.log('Put object error' + err);
            throw err;
          }
          const endTime:Date=new Date();
          const diffMs = (endTime.getTime()-startTime.getTime());
          console.log('Total time taken to upload - '+Math.round(diffMs/60000) + ' minutes');
          console.log("File upload completed!", data);
        }).on('httpUploadProgress', function (progress) {
          console.log('Upload Progress : '+ Math.round(progress.loaded/progress.total) * 100 +'%');
        });
      }

      part.on('error', function (err) {
        console.log('Part error');
        // decide what to do
        console.log(err);
      });
    });
    form.parse(request);         
  }

0 个答案:

没有答案