Angular2想上传到S3

时间:2017-04-23 12:55:43

标签: javascript angular amazon-web-services amazon-s3

我想使用Angular 2进行分段上传。 我写了下面的代码。

import * as AWS from 'aws-sdk'

@Component({

})
export class UploadComponent implements OnInit {

    progress: Array<any> = []
    file: any

    constructor() {

    }

    ngOnInit() {
    }

    uploadfile(event) {
        AWS.config.accessKeyId = '';
        AWS.config.secretAccessKey = '';
        AWS.config.region = ''
        let bucket = new AWS.S3({params: {Bucket: ''}});
        let params = {Key: this.file.name, Body: this.file, ContentType: this.file.type};
        bucket.upload(params, function (err, data) {
            console.log(err, data);
        });
    }

    fileEvent(event: any){
        let files = event.target.files
        let file = files[0]
        this.file = file
        console.log(this.file)
    }
}

我收到了错误。

 Argument of type '{ Key: any; Body: any; ContentType: any; }' is not assignable to parameter of type 'PutObjectRequest'.
  Property 'Bucket' is missing in type '{ Key: any; Body: any; ContentType: any; }'.)

请帮帮我。 我想建议。

我试图获取信息,但我无法得到正确答案。

2 个答案:

答案 0 :(得分:0)

检查这个link您传递的对象似乎与预期传递给upload函数的对象不同。

答案 1 :(得分:0)

让params = {Key:this.file.name,Body:this.file,ContentType:this.file.type};

//只需更改上一行

var params = {Bucket:&#39; your-bucket-name&#39;,Key:this.file.name,Body:this.file,ContentType:this.file.type};