简体   繁体   English

Amazon s3存储桶的Angular2文件上传

[英]Angular2 File upload for Amazon s3 bucket

我知道如何做角度1的亚马逊s3文件上传。我可以知道如何上传角度2的文件。但我没有找到角度2的任何解决方案。

It still needs better/some security. 它仍然需要更好/一些安全性。

html HTML

<form  (ngSubmit)="onSubmit(f)" #f="ngForm" action="">
<input type="file" (change)="fileEvent($event)" />
</form>
<button (click)="uploadfile(f)">Upload file!</button>

ts TS

export class Page2 {
    myfile:any;
    file:any;

constructor() {
}


   uploadfile(event) {
        AWS.config.accessKeyId = 'YOUR-ACCESS-KEY';
        AWS.config.secretAccessKey = 'YOU-SECRET-ACCESS-KEY';
        var bucket = new AWS.S3({params: {Bucket: 'YOUR-BUCKET-NAME'}});
        var params = {Key: this.file.name, Body: this.file};
        bucket.upload(params, function (err, data) {
            console.log(err, data);
        });
    }


    fileEvent(fileInput: any){
        var files = event.target.files;
        var file = files[0];
        this.file = file;
    }

}

If you are using a Rails backend (or even Node) the easiest solution is to create a presigned url (which must be a PUT) and then use an Angular service to call that PUT and attach the file in the body of the request. 如果您使用的是Rails后端(甚至是Node),最简单的解决方案是创建预签名URL(必须是PUT),然后使用Angular服务调用该PUT并将该文件附加到请求正文中。

You can read more about how I did that here . 你可以在这里阅读更多关于我是如何做到的。

I don't know if you solved this problem already but all you have to do is rebuild the post request you send to S3 我不知道你是否已经解决了这个问题,但你所要做的就是重建发送给S3的发布请求

You can check out this: https://github.com/ntheg0d/angular2_aws_s3 你可以看看这个: https//github.com/ntheg0d/angular2_aws_s3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM