如何从服务到组件的s3上传响应

时间:2019-01-17 11:22:02

标签: angular amazon-s3

我有一个将文件上载到s3并给出响应的服务,但我无法通过调用将该服务方法的响应传递给我的组件。

这是我的服务。我正在尝试订阅该方法以获取响应。

@Injectable()
      export class DBService {
    uploadfile(file,id){

        const bucket = new S3(
          {
            accessKeyId: '*******',
            secretAccessKey: '******',
            region: '****',

          }
        );
        let extn = file.name.split('.').pop();
        let contentType = 'application/octet-stream';
        if (extn == 'html') contentType = "text/html";
        if (extn == 'css') contentType = "text/css";
        if (extn == 'js') contentType = "application/javascript";
        if (extn == 'mp4') contentType = "video/mp4";
        if (extn == 'png' || extn == 'jpg' || extn == 'gif') contentType = "image/" + extn;
        var options = {partSize: 100*1024*1024, queueSize: 1};
        var Obj = this;
        const params = {
          Bucket: 'vod-watchfolder-vidsrc',
          Key: this.IMGFOLDER +id+"/"+ file.name,
          Body: file,
          ContentType:contentType
        };
       bucket.upload(params,options, function (err, data) {
          if (err) {
            alert("Error   " +err);
            console.log('There was an error uploading your file: ', err);
            return false;
          }
         return data;`enter code here`
        })

      }
    }

my Component :

imageUpload(){
  this.myService.uploadfile(this.selectedFile,this.profile.userId).subscribe(
    response =>{
      this.profile.imgPath = response.Location;
      this.imgUrl = response.Location;
      console.log('Successfully uploaded image.', response);
    }
  )

0 个答案:

没有答案