Created
March 9, 2021 16:05
-
-
Save krisrice/c6a8e01760adfda76c2f11c42574b276 to your computer and use it in GitHub Desktop.
objectstore.js
This file contains hidden or 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
var OCIProfiles = Java.type("oracle.dbtools.oci.OCIProfiles"); | |
var OCIRESTClient = Java.type("oracle.dbtools.oci.OCIRESTClient"); | |
var OCIRequest = Java.type("oracle.dbtools.oci.OCIRequest"); | |
var Instant = Java.type("java.time.Instant"); | |
var DateTimeFormatter = Java.type("java.time.format.DateTimeFormatter"); | |
var TemporalAccessor = Java.type("java.time.temporal.TemporalAccessor"); | |
var FileUtils = Java.type("oracle.dbtools.common.utils.FileUtils"); | |
var StandardCopyOption = Java.type("java.nio.file.StandardCopyOption"); | |
var Files = Java.type("java.nio.file.Files"); | |
var Paths = Java.type("java.nio.file.Paths"); | |
function ObjectStore(profile,ns,bucket,region){ | |
var ociProfiles = new OCIProfiles(); | |
this.client = ociProfiles.getOciClient(profile); | |
this.fqdn = "objectstorage."+region+".oraclecloud.com"; | |
this.ns=ns; | |
this.bucket=bucket; | |
this.region = region; | |
this.ls=function(prefix,startWith){ | |
var target = "/n/"+this.ns+"/b/"+this.bucket+"/o/?fields=timecreated&limit=1000"; | |
if ( startWith ) { | |
target = target + "&startAfter=" + startWith; | |
} | |
if ( prefix ) { | |
target = target + "&prefix=" + prefix; | |
} | |
ctx.write("Target:"+ target + "\n"); | |
var req = new OCIRequest.Builder().method("get").host(this.fqdn).target(target).build(); | |
var pendingObjects = JSON.parse(this.client.getString(req)) | |
return pendingObjects; | |
} | |
this.getObject = function(name){ | |
var docReq = new OCIRequest.Builder().method("get").host(this.fqdn).target("/n/"+ns+"/b/"+bucket+"/o/" + name).build(); | |
// convert to json | |
ctx.write("Stream:"+ target + "\n"); | |
var ret = this.client.getString(docReq); | |
var doc = JSON.parse(ret); | |
} | |
this.saveObject2File = function(name,filename){ | |
var docReq = new OCIRequest.Builder().method("get").host(this.fqdn).target("/n/"+ns+"/b/"+bucket+"/o/" + name).build(); | |
// convert to json | |
var inputStream = this.client.getStream(docReq); | |
var responsePath = FileUtils.getNewOrExistingPath(ctx, filename); | |
try { | |
Files.copy(inputStream,responsePath, StandardCopyOption.REPLACE_EXISTING); | |
} finally { | |
inputStream.close(); | |
} | |
} | |
this.getObjectStream = function(name){ | |
var docReq = new OCIRequest.Builder().method("get").host(this.fqdn).target("/n/"+ns+"/b/"+bucket+"/o/" + name).build(); | |
// convert to json | |
return this.client.getStream(docReq); | |
} | |
this.deleteObject = function(name){ | |
//DELETE /n/{namespaceName}/b/{bucketName}/o/{objectName} | |
var docReq = new OCIRequest.Builder().method("delete").host(this.fqdn).target("/n/"+ns+"/b/"+bucket+"/o/" + name).build(); | |
// convert to json | |
var ret = this.client.getString(docReq); | |
return ret; | |
} | |
this.putObject = function(file,name){ | |
// PUT /n/{namespaceName}/b/{bucketName}/o/{objectName} | |
var docReq = new OCIRequest.Builder().method("put").host(this.fqdn).target("/n/"+ns+"/b/"+bucket+"/o/" + name).bodyFile(file).build(); | |
// convert to json | |
var ret = this.client.getString(docReq); | |
return ret; | |
} | |
this.rename=function(oldname,newname){ | |
// POST /n/{namespaceName}/b/{bucketName}/actions/renameObject | |
var cmd = { | |
"sourceName" : oldname, | |
"newName" : newname | |
}; | |
var docReq = new OCIRequest.Builder().method("post").host(this.fqdn).target("/n/"+ns+"/b/"+bucket+"/actions/renameObject").contentType("application/json").body(JSON.stringify(cmd)).build(); | |
var ret = this.client.getString(docReq); | |
} | |
this.copy=function(name,destns,destbucket,toName){ | |
//POST /n/{namespaceName}/b/{bucketName}/actions/copyObject | |
var cmd = { | |
"destinationBucket": destbucket, | |
"destinationNamespace": destns, | |
"destinationObjectName": toName, | |
"sourceObjectName": name, | |
"destinationRegion": this.region | |
}; | |
var docReq = new OCIRequest.Builder() | |
.method("post") | |
.host(this.fqdn) | |
.target("/n/"+ns+"/b/"+bucket+"/actions/copyObject") | |
.contentType("application/json") | |
.body(JSON.stringify(cmd)) | |
.build(); | |
ctx.write("/n/"+ns+"/b/"+bucket+"/actions/copyObject" + "\n"); | |
ctx.write(JSON.stringify(cmd,2,' ') + "\n"); | |
var ret = this.client.getString(docReq); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment