Deepak
Deepak

Reputation: 1433

Angular2 File upload for Amazon s3 bucket

I know how to do amazon s3 file upload in angular 1. May i know how to upload file in angular 2. but i didn't find any solution for angular 2.

Upvotes: 7

Views: 13274

Answers (3)

rmcsharry
rmcsharry

Reputation: 5552

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.

You can read more about how I did that here.

Upvotes: 4

CESCO
CESCO

Reputation: 7768

It still needs better/some security.

html

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

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;
    }

}

Upvotes: 7

ntheg0d
ntheg0d

Reputation: 53

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

You can check out this: https://github.com/ntheg0d/angular2_aws_s3

Upvotes: 0

Related Questions