Last active
September 21, 2021 17:47
-
-
Save kdclaw3/7706e224c43f1ded7ea51010c858c322 to your computer and use it in GitHub Desktop.
Oracle OCI Object Storage Upload Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Oracle OCI Object Storage Upload Example | |
// Using Upload Manager with Progress Bar Tracking | |
// imports | |
const fs = require('fs'); | |
const { basename, join } = require('path'); | |
const cliProgress = require('cli-progress'); | |
const os = require('oci-objectstorage'); | |
const common = require('oci-common'); | |
// oracle specific settings | |
const configurationFilePath = `C:/Users/firstlast/.env_oracle`; | |
const configProfile = 'DEFAULT'; | |
const namespaceName = 'namespacename'; | |
const bucketName = 'bucketname'; | |
const provider = new common.ConfigFileAuthenticationDetailsProvider(configurationFilePath, configProfile); | |
const client = new os.ObjectStorageClient({ authenticationDetailsProvider: provider }); | |
const uploadManager = new os.UploadManager(client, { enforceMD5: true }); | |
// create progress bar | |
const multibar = new cliProgress.MultiBar({ | |
format: '[\u001b[32m{bar}\u001b[0m] {percentage}% | {filename} | {onfile}/{totalfiles}', | |
barCompleteChar: '#', | |
barIncompleteChar: '#', | |
barGlue: '\u001b[33m', | |
clearOnComplete: true, | |
hideCursor: true | |
}, cliProgress.Presets.shades_grey); | |
const directoryPath = './files/'; | |
let bars = {}; | |
// these are the files to be procssed. As they are being worked on we are removign them from files | |
let files = fs.readdirSync(directoryPath); | |
async function call (round, file) { | |
const totalfiles = files.length; | |
// if no more fiels exit | |
if (!totalfiles > 0) return; | |
// get and remove first filename | |
const filename = files.shift(); | |
const objectName = `${basename(filename)}`; | |
// this could be better | |
let onfile = 1; | |
try { | |
bars[filename] = multibar.create(100, 0, { filename: filename, onfile, totalfiles }); | |
await uploadManager.upload( | |
{ | |
content: { filePath: join(directoryPath, filename) }, | |
requestDetails: { | |
namespaceName, | |
bucketName, | |
objectName | |
} | |
}, | |
res => { | |
// console.log(res.progress); | |
bars[filename].update(Number(res.progress), { filename: filename, onfile, totalfiles }); | |
} | |
); | |
// remove the bar from display | |
multibar.remove(bars[filename]); | |
// delete the object from tracking | |
delete bars[filename]; | |
// move the file so we know that it is processed | |
fs.rename(join(directoryPath, filename), join('./files_processed/', filename), function (err) { | |
if (err) throw err; | |
}); | |
} catch (ex) { | |
// remove the bar from display | |
multibar.remove(bars[filename]); | |
// delete the object from tracking | |
delete bars[filename]; | |
// add the file back | |
files.push(filename); | |
} | |
// i < files.length ? call(++round, file) : null; | |
call(++round, file); | |
} | |
(async () => { | |
await Promise.all[call(1, 1), call(1, 2), call(1, 3), call(1, 4), call(1, 5)]; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment