/**
 * 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 , requests , models } from "oci-objectstorage" ; import { Region } from "oci-common" ; import { readdir } from "fs" ; const provider = new common . ConfigFileAuthenticationDetailsProvider ( ) ; const args = process . argv . slice ( 2 ) ; if ( args . length !== 5 ) { console . error ( "Unexpected number of arguments received. Please pass absloute directory path to read files from" ) ; process . exit ( - 1 ) ; } const filePath = args [ 0 ] ; const bucketName = args [ 1 ] ; const namespaceName = args [ 2 ] ; const regionId = args [ 3 ] ; const secondLevelDomain = args [ 4 ] ; const serviceName = "objectstorage" ; const client = new ObjectStorageClient ( { authenticationDetailsProvider : provider } ) ; ( async ( ) => { try { console . time ( `Download Time ${ filePath }` ) ; const today = new Date ( ) ; const neverExpires = new Date ( today ) ; neverExpires . setDate ( neverExpires . getDate ( ) + 25 ) ; const parUniqueId = Date . now ( ) ; const createPreauthenticatedRequestDetails = { name : parUniqueId . toString ( ) , objectName : filePath , accessType : models . CreatePreauthenticatedRequestDetails . AccessType . ObjectRead , timeExpires : neverExpires } as models . CreatePreauthenticatedRequestDetails ; const createPreauthenticatedRequest = { bucketName : bucketName , namespaceName : namespaceName , createPreauthenticatedRequestDetails : createPreauthenticatedRequestDetails } ; const resp = await client . createPreauthenticatedRequest ( createPreauthenticatedRequest ) ; const baseUrl = `https://${ serviceName }.${ regionId }.${ secondLevelDomain }` ; const downloadUrl = resp . preauthenticatedRequest . accessUri ; console . log ( "download url for the file " + filePath + " is " + baseUrl + downloadUrl ) ; console . timeEnd ( `Download Time ${ filePath }` ) ; } catch ( ex ) { console . error ( `Failed due to ${ ex }` ) ; } } ) ( ) ;