代码之家  ›  专栏  ›  技术社区  ›  Ka Tech

Angular 2/Javascript AWS S3 SDK-中止上传onClick

  •  0
  • Ka Tech  · 技术社区  · 8 年前

    我在Angular 2中工作,并通过javascript AWS S3 SDK实现了S3上传。我的问题是如何通过点击按钮中止上传?

    到目前为止,我已经在下面进行了尝试,但我得到了一个“未定义不是对象(正在评估”this.bucket.abort.bind“)

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

    我的取消按钮

     cancel(): void {
    
        this.bucket.abort.bind(this.bucket);
      }
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   Ka Tech    8 年前

    以下结果有效:

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

    我的取消按钮

     cancel(): void {
    
        this.request.abort();
      }