import { Injectable } from '@angular/core' ; import S3 from 'aws-sdk/clients/s3' ; import AWS from 'aws-sdk/global' ; import { uuid } from './utils' ; const BUCKET = 'com.zerotoheroes.support' ; @ Injectable ( ) export class S3FileUploadService { public async postBlob ( blob , extension ? , bucket ? ) { const fileKey = uuid ( ) + ( extension || '' ) ; AWS . config . region = 'us-west-2' ; AWS . config . httpOptions . timeout = 3600 * 1000 * 10 ; const s3 = new S3 ( ) ; const params = { Bucket : bucket || BUCKET , Key : fileKey , ACL : 'public-read-write' , Body : blob , } ; return new Promise < string > ( ( resolve ) => { s3 . makeUnauthenticatedRequest ( 'putObject' , params , ( err , data2 ) => { if ( err ) { console . warn ( 'An error during upload' , err ) ; resolve ( null ) ; } else { resolve ( fileKey ) ; } } ) ; } ) ; } }