11import { UploadState , UploadStates } from "@/src/types/shared/upload" ;
2- import { S3 } from 'aws-sdk' ;
32import { ConfigManager } from "./config" ;
43import { Observable } from "rxjs" ;
4+ import { GetObjectCommand , PutObjectCommand , S3Client } from "@aws-sdk/client-s3" ;
55
66
77export function uploadFile ( credentialsAndUploadIdParsed : any , file : File , filename : string ) : Observable < UploadState > {
@@ -11,42 +11,33 @@ export function uploadFile(credentialsAndUploadIdParsed: any, file: File, filena
1111 const s3Endpoint = ConfigManager . getConfigValue ( "KERN_S3_ENDPOINT" ) ;
1212 const s3Region = ConfigManager . getConfigValue ( 's3_region' ) ;
1313
14- const s3Client = new S3 ( {
14+ const s3Client = new S3Client ( {
1515 endpoint : s3Endpoint ,
16- accessKeyId : credentials [ "AccessKeyId" ] ,
17- secretAccessKey : credentials [ "SecretAccessKey" ] ,
18- sessionToken : credentials [ "SessionToken" ] ,
1916 region : s3Region ,
20- s3BucketEndpoint : false ,
21- s3ForcePathStyle : true ,
22- signatureVersion : 'v4' ,
23- sslEnabled : false ,
17+ credentials : {
18+ accessKeyId : credentials [ "AccessKeyId" ] ,
19+ secretAccessKey : credentials [ "SecretAccessKey" ] ,
20+ sessionToken : credentials [ "SessionToken" ] ,
21+ } ,
2422 } ) ;
2523
2624 const key = uploadTaskId + "/" + filename ;
2725
2826 return new Observable ( ( subscriber ) => {
29- var managedUpload = s3Client . upload ( {
27+ s3Client . send ( new PutObjectCommand ( {
3028 Bucket : bucket ,
3129 Key : key ,
3230 Body : file ,
33- } , { } , function ( err , data ) {
34- if ( err ) {
35- subscriber . error ( {
36- state : UploadStates . ERROR ,
37- progress : 100
38- } )
39- }
31+ } ) ) . then ( data => {
4032 subscriber . next ( {
4133 state : UploadStates . DONE ,
4234 progress : 100
43- } )
44- } ) ;
45- return managedUpload . on ( 'httpUploadProgress' , function ( progress ) {
46- subscriber . next ( {
47- state : UploadStates . IN_PROGRESS ,
48- progress : Math . round ( progress . loaded / progress . total * 100 )
49- } )
35+ } ) ;
36+ } ) . catch ( err => {
37+ subscriber . error ( {
38+ state : UploadStates . ERROR ,
39+ progress : 100
40+ } ) ;
5041 } ) ;
5142 } )
5243}
@@ -57,34 +48,36 @@ export function downloadFile(credentialBlock: any, isStringData: boolean = true)
5748 const object = credentialBlock [ "objectName" ] ;
5849 const bucket = credentialBlock [ "bucket" ] ;
5950 const s3Endpoint = ConfigManager . getConfigValue ( "KERN_S3_ENDPOINT" ) ;
60- const s3Region = ConfigManager . getConfigValue ( 'S3_REGION ' ) ;
51+ const s3Region = ConfigManager . getConfigValue ( 's3_region ' ) ;
6152
62- const s3Client = new S3 ( {
53+ const s3Client = new S3Client ( {
6354 endpoint : s3Endpoint ,
64- accessKeyId : credentials [ "AccessKeyId" ] ,
65- secretAccessKey : credentials [ "SecretAccessKey" ] ,
66- sessionToken : credentials [ "SessionToken" ] ,
6755 region : s3Region ,
68- s3BucketEndpoint : false ,
69- s3ForcePathStyle : true ,
70- signatureVersion : 'v4' ,
56+ credentials : {
57+ accessKeyId : credentials [ "AccessKeyId" ] ,
58+ secretAccessKey : credentials [ "SecretAccessKey" ] ,
59+ sessionToken : credentials [ "SessionToken" ] ,
60+ } ,
7161 } ) ;
7262
7363 var getParams = {
7464 Bucket : bucket ,
7565 Key : object
7666 }
7767 return new Observable ( ( subscriber ) => {
78- s3Client . getObject ( getParams , function ( err , data ) {
79- if ( err ) {
80- subscriber . error ( null )
81- }
82- if ( isStringData ) {
83- let objectData = data . Body . toString ( 'utf-8' ) ; // Use the encoding necessary
84- subscriber . next ( objectData )
68+ s3Client . send ( new GetObjectCommand ( getParams ) ) . then ( data => {
69+ const bodyContents = data . Body ;
70+ if ( isStringData && bodyContents instanceof ReadableStream ) {
71+ const reader = bodyContents . getReader ( ) ;
72+ reader . read ( ) . then ( ( { done, value } ) => {
73+ const objectData = new TextDecoder ( 'utf-8' ) . decode ( value ) ;
74+ subscriber . next ( objectData ) ;
75+ } ) ;
8576 } else {
86- subscriber . next ( data . Body )
77+ subscriber . next ( bodyContents ) ;
8778 }
79+ } ) . catch ( err => {
80+ subscriber . error ( null ) ;
8881 } ) ;
8982 } ) ;
9083
0 commit comments