简体   繁体   中英

Angular 2/Javascript AWS S3 SDK - Abort Upload onClick

I am working in Angular 2 and have implemented an S3 upload through the javascript AWS S3 SDK. The question I have is how do I abort the upload via a button click?

So far I have tried below but I get an 'Undefined is not an object (evaluating 'this.bucket.abort.bind')

My Upload Function:

upload(): void {
AWS.config.update({
    region: AWSREGION,
    credentials: new AWS.CognitoIdentityCredentials(
        {
            IdentityPoolId: AWSIDENTITYPOOLID,
            IdentityId: obj.IdentityId,
            Logins: {
                'cognito-identity.amazonaws.com': obj.Token
            }
        }
    )
});
this.awsToken = obj.Token;
// set bucket 
this.bucket = new AWS.S3({params: {Bucket: 'myBucket'}});
// Upload to S3
          this.bucket.putObject(params, (err, data) => {
            if (err) {
                console.log('AWS Error', err);      
            } else {
              // Success 
              console.log('Success');
            }
           });  
}

My Cancel Button

 cancel(): void {

    this.bucket.abort.bind(this.bucket);
  }

The following end up working:

upload(): void {
AWS.config.update({
    region: AWSREGION,
    credentials: new AWS.CognitoIdentityCredentials(
        {
            IdentityPoolId: AWSIDENTITYPOOLID,
            IdentityId: obj.IdentityId,
            Logins: {
                'cognito-identity.amazonaws.com': obj.Token
            }
        }
    )
});
this.awsToken = obj.Token;
// set bucket 
this.bucket = new AWS.S3({params: {Bucket: 'myBucket'}});
// Set request
this.request = this.bucket.putObject(params);   
// Upload to S3
          this.request.send((err, data) => {
            if (err) {
                console.log('AWS Error', err);      
            } else {
              // Success 
              console.log('Success');
            }
           });  
}

My Cancel Button

 cancel(): void {

    this.request.abort();
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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