/**
 * Copyright (c) 2020, 2021 Oracle and/or its affiliates.  All rights reserved.
 * This software is dual-licensed to you under the Universal Permissive License (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose either license.
 */ import common = require ( "oci-common" ) ; import { ObjectStorageClient , UploadManager } from "oci-objectstorage" ; import { basename , join } from "path" ; import { readdir } from "fs" ; const provider = new common . ConfigFileAuthenticationDetailsProvider ( ) ; const args = process . argv . slice ( 2 ) ; if ( args . length !== 3 ) { console . error ( "Unexpected number of arguments received. Please pass absloute directory path to read files from" ) ; process . exit ( - 1 ) ; } const directoryPath = args [ 0 ] ; const namespaceName = args [ 1 ] ; const bucketName = args [ 2 ] ; const client = new ObjectStorageClient ( { authenticationDetailsProvider : provider } ) ; const uploadManager = new UploadManager ( client , { enforceMD5 : true } ) ; ( async ( ) => { readdir ( directoryPath , ( err , files ) => { if ( err ) return console . log ( "Unable to scan directory: " + err ) ; files . forEach ( async ( filename ) => { const objectName = `${ basename ( filename ) }` ; console . log ( `Uploading ${ objectName }` ) ; try { console . time ( "Upload Time" ) ; const callback = ( res ) => { console . log ( "Progress: " , res ) ; } ; await uploadManager . upload ( { content : { filePath : join ( directoryPath , filename ) } , requestDetails : { namespaceName : namespaceName , bucketName : bucketName , objectName : objectName } } , callback ) ; console . timeEnd ( "Upload Time" ) ; } catch ( ex ) { console . error ( `Failed due to ${ ex }` ) ; } } ) ; } ) ; } ) ( ) ;