NodeJS 意外的关键字或标识符.ts(1434)

mwkjh3gx  于 2023-02-03  发布在  Node.js
关注(0)|答案(1)|浏览(449)

下面的代码尝试从本地机器读取文件并将其上传到AWS s3云服务:

import AWS from 'aws-sdk';
import fs, { ReadStream } from "fs";
import path from "path";

class Uploads {

  static const bucketName = "myappllbuck";
  static const s3 = new AWS.S3({ apiVersion: '2006-03-01' });
  static const filePath = "/home/s/img2.png";

  static const uploadParams: {
    Bucket: string;
    Key: string;
    Body: string | ReadStream;
    ContentType: any; ACL: string
  } = {
      Bucket: this.bucketName,
      Key: '',
      Body: '',
      ContentType: null,
      ACL: 'public-read',
    };

  static const fileStream  = fs.createReadStream(this.filePath);

  
  fileStream.on('error', function(err) {
    console.log('File Error', err);
  });
   this.uploadParams.Body = this.fileStream;
   this.uploadParams.Key = path.basename(filePath);
   this.uploadParams.ContentType = 'image/png';
   this.uploadParams.ACL = 'public-read';
}

但是我得到了下面的错误@fileStream.on()行:

Unexpected keyword or identifier.ts(1434)
Member 'fileStream' implicitly has an 'any' type.ts(7008)

和下面的错误@this.UploadParams.行代码:

Unexpected token. A constructor, method, accessor, or property was expected.ts(1068)
Object is possibly 'undefined'.ts(2532)
dm7nw8vv

dm7nw8vv1#

fileStream.on('error', function(err) {开始的代码仍在类中。请确保在运行此代码之前关闭类声明。

相关问题