Uploading Files to Object Storage using Multipart in VBCS
Object Storage has become a go to alternative to store large amounts of data, hence uploading huge chunks of file has become a common requirement in Cloud. Anyone who has tried to upload a huge file to server knows the drawbacks and shortcomings of such approach, this is where the MultiPart Upload approach comes in handy.
Below I try to explain the steps to achieve the same
MultiPart upload to Object Storage is done in 3 phases
Phase 1: Create a multi part upload request
Phase 2: Upload Parts
Phase 3: Commit Upload
Note: Before you start building the VBCS application, make sure your object storage instance is setup and you have the bucket name for the same
Let’s start into building the VBCS application
1. Build a VBCS application and drag and drop a File upload component to the samea. REST endpoint for object storage looks like below :
https://objectstorage.eu-frankfurt-1.oraclecloud.com/{version}/{account}
6. Create a ojAction event on Button, this is where all the Multipart code will be written and executed
PageModule.prototype.getFileChunks = function(file,byteSize){
let startPointer = 0;
let byteSizeNum = parseInt(byteSize,10);
let endPointer = file.size;
console.log("File Size : "+endPointer);
console.log("byteSizeNum : "+byteSizeNum+" is type of "+typeof (byteSizeNum));
let b64Chunks = [];
while(startPointer<endPointer){
let newStartPointer = 0;
newStartPointer = startPointer+byteSizeNum;
b64Chunks.push(convertBlobToB64(file.slice(startPointer,newStartPointer)));
startPointer = newStartPointer;
}
return b64Chunks;
};
async function convertBlobToB64 (file){
let fileContentPromise = new Promise(function(resolve){
if (file){
console.log("File exists");
var reader = new FileReader();
reader.onloadend = function (fileReadEvent){
var fileDataBase64 = fileReadEvent.target.result;
resolve(fileDataBase64);
};
reader.readAsDataURL(file);
}
});
return await fileContentPromise;
};
8. Call JS function to create body for “initializeMultiPartRest” Rest service
PageModule.prototype.commitMultiPartBody = function(multiPartDetails){
let multiPartJson = JSON.stringify(multiPartDetails);
console.log("multiPartJson - "+multiPartJson);
let multiPartJson1 = multiPartDetails;
multiPartJson1.map(jsonObj => {
delete jsonObj.md5;
delete jsonObj.size;
delete jsonObj.lastModified;
jsonObj.partNum = jsonObj.partNumber;
delete jsonObj.partNumber;
return jsonObj;
});
console.log("multiPartJson1 - After delete : "+JSON.stringify(multiPartJson1));
let body = '{ "partsToCommit": '+JSON.stringify(multiPartJson1)+' }';
console.log("Commit Body : "+body);
return body;
Comments
Post a Comment