在亚马逊S3上传,检索和删除图像

时间:2017-09-05 06:30:52

标签: angular file-upload amazon-s3

您好我想在不使用aws-sdk的情况下从我的angular2应用程序上传s3中的图像。我发现此链接解释了没有任何库https://github.com/ntheg0d/angular2_aws_s3/blob/master/app/upload/upload.component.ts的文件上传。我按照以下步骤上传图片< / p>

<form (ngSubmit)="upload()">
    <input name="file" type="file" size="60" (change)="onChange($event)"/>
    <input type="submit" value="Upload"/>
</form>

这是我的上传逻辑

    onChange(event: any) {
      var file = event.target.files[0];
       var queryString = 'filename=' + filename +
        '&content_type=' + event.target.files[0].type; 
       this.uploadSrvc.GetS3Credentials(queryString).subscribe((response: 
        any) => {
           this.upload(file, response);
       });
   }

此处GetS3Credentials获取存储桶名称和其他凭据并调用要上传的实际事件

    upload(file: any, response: any) {
    let formData: FormData = new FormData();
    let xhr: XMLHttpRequest = new XMLHttpRequest();

    //Build AWS S3 Request
    formData.append('key', response.params.key);
    formData.append('acl', response.params.acl);
    formData.append('content-Type', response.params['content-type']);
    //Put in your access key here
    formData.append('x-amz-algorithm', response.params['x-amz-algorithm']);

    formData.append('x-amz-credential', response.params['x-amz-credential']);
    formData.append('x-amz-date', response.params['x-amz-date']);
    formData.append('policy', response.params['policy']);
    formData.append('x-amz-signature', response.params['x-amz-signature']);
    formData.append('success_action_status', response.params['success_action_status']);
    formData.append('file', file);

    xhr.open('POST', response.endpoint_url, true);
    xhr.send(formData);
}

上传活动完美无缺,我可以上传图片。现在我想从服务器上检索图像而不需要任何库。有人请告诉我该怎么做?

0 个答案:

没有答案