Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pilot #25

Merged
merged 5 commits into from
Feb 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
build
dist
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Storj-Nodejs Changelog

## [1.2.0]-29-01-2021
* Updated HelloStorj.js
* Updated package.json
* Updated documentation

## [1.0.9] - 27-11-2020
* Merged PR

Expand Down
87 changes: 47 additions & 40 deletions HelloStorj.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ const fs = require("fs");

//include the Node.js-Storj bindings module
const storj = require("uplink-nodejs");
//object for all the function for uplink

//Object for all the function for uplink
const libUplink = new storj.Uplink();

//Object for all error for uplink
const uplinkError = storj.errorhandle;
//

var BUFFER_SIZE = 80000;
//

//demo Storj (V3) configuration
var storjConfig = {
apiKey : "change-me-to-the-api-key-created-in-satellite-gui",
satelliteURL : "us-central-1.tardigrade.io:7777",
satelliteURL : "change-me-to-desired-satellite-url",
encryptionPassphrase : "you'll never guess this",
bucketName : "change-me-to-desired-bucket-name",
uploadPath : "optionalpath/requiredfilename",
Expand All @@ -23,7 +25,7 @@ var localFullFileName = {
src : "change-me-to-source-file-name-at-local-system",
dest: "change-me-to-destination-file-name-at-local-system",
};
//

function getDateTime(unixTimestamp) {
var dateTime = new Date(unixTimestamp * 1000);

Expand All @@ -47,8 +49,10 @@ function getDateTime(unixTimestamp) {

//Hours part from the timestamp
var hours = "0" + dateTime.getHours();

//Minutes part from the timestamp
var minutes = "0" + dateTime.getMinutes();

//Seconds part from the timestamp
var seconds = "0" + dateTime.getSeconds();

Expand All @@ -57,7 +61,7 @@ function getDateTime(unixTimestamp) {

return formattedDateTime;
}
//

async function accessshare(uplink,access){
var permission = new storj.Permission(false,false,false,true,0,0);
var sharePrefix = storj.SharePrefix;
Expand All @@ -66,31 +70,30 @@ async function accessshare(uplink,access){
sharePrefix.prefix ="change-me-to-desired-object-prefix-with-/";
sharePrefixListArray.push(sharePrefix);
await access.share(permission,sharePrefixListArray,sharePrefixListArray.length).then(async (sharedAccess) => {
//

console.log("Serilazing share access");
await sharedAccess.serialize().then(async (stringResult) => {
//

console.log("Parsing access share...");
await uplink.parseAccess(stringResult).then(async (parsedSharedAccess) => {
//

console.log("\nDeriving encryption key");
//

var encryption = await libUplink.uplinkDeriveEncryptionKey("test",[4,5,6]).catch((error) => {
console.log("Failed to derive encryption key");
console.log(error);
});
//

console.log("Over riding encryption key");
//

await parsedSharedAccess.overrideEncryptionKey(sharePrefix.bucket,sharePrefix.prefix,encryption["encryption_key"]).catch((err) => {
console.log("Failed to over write encryption key");
console.log(err);
});
//

console.log("Opening project using paresed shared access");
await parsedSharedAccess.openProject().then(async (project) => {
//
//

console.log("\nOpened Project using share access");
await project.deleteObject(storjConfig.bucketName,storjConfig.uploadPath).then((objectInfo) => {
console.log("\nObject Deleted successfully !!");
Expand All @@ -99,7 +102,7 @@ async function accessshare(uplink,access){
console.log("Failed to delete object on storj V3 network using shared access");
console.log(err);
});
//

await project.close().then(() => {
console.log("Project Closed !!");
}).catch((err) => {
Expand All @@ -126,10 +129,11 @@ async function accessshare(uplink,access){
/*eslint-disable */
async function uploadfile(project){
console.log("Getting Upload Object....");
//

var uploadOptions = new storj.UploadOptions();
//

uploadOptions.expires = 0;

//Uploading object on storj V3 network
await project.uploadObject(storjConfig.bucketName,storjConfig.uploadPath,uploadOptions).then(async (upload) => {
console.log(localFullFileName.src, " File: UPLOADED as ", storjConfig.uploadPath, "!");
Expand All @@ -140,22 +144,23 @@ async function uploadfile(project){
actuallyWritten : 0,
totalWritten : 0
};
//

var buffer = new Buffer.alloc(BUFFER_SIZE);
var loop = true;
var bytesRead = 0;

while (loop) {
//

size.toWrite = size.file - size.totalWritten;
//

if (size.toWrite > BUFFER_SIZE) {
size.toWrite = BUFFER_SIZE;
} else if (size.toWrite === 0) {
break;
}
//

bytesRead = await fs.readSync(fileHandle, buffer, 0, size.toWrite, size.totalWritten);
//

//Writing data on storj V3 network
await upload.write(buffer,bytesRead).then((writeResult) => {
size.actuallyWritten = writeResult.bytes_written;
Expand All @@ -172,7 +177,7 @@ async function uploadfile(project){
break;
}
}
//

var customMetadataEntry1 =new storj.CustomMetadataEntry();
customMetadataEntry1.key = "testing";
customMetadataEntry1.key_length = customMetadataEntry1.key.length;
Expand All @@ -192,20 +197,23 @@ async function uploadfile(project){
var customMetadata = new storj.CustomMetadata();
customMetadata.count = customMetadataEntryArray.length;
customMetadata.entries = customMetadataEntryArray;

//Adding custom meta to upload object
await upload.setCustomMetadata(customMetadata).then(() => {
console.log("\nCustom Metadata set successfully");
}).catch((err) => {
console.log("Failed to set custom metadata");
console.log(err);
});

//Commiting object on storj V3 network
await upload.commit().then(() => {
console.log("\nObject on storj V3 network successfully");
}).catch((err) => {
console.log("Failed to commit object on storj V3 network");
console.log(err);
});

//Fetching info of uploaded object on storj V3 network
await upload.info().then((object) => {
console.log("\nObject Info");
Expand All @@ -214,7 +222,7 @@ async function uploadfile(project){
console.log("Failed to fetch information about object");
console.log(err);
});
//

fs.closeSync(fileHandle);
}).catch((err) => {
console.log("Failed to upload object on storj V3");
Expand All @@ -228,32 +236,34 @@ async function downloadfile(project){
var downloadOptions = new storj.DownloadOptions();
downloadOptions.offset = 0;
downloadOptions.length = -1;

//Downloading file
console.log("Downloading file");
await project.downloadObject(storjConfig.bucketName,storjConfig.uploadPath,downloadOptions).then(async (download) => {
var objectsize =0;
//

console.log("Fetching download object info");
await download.info().then((objectInfo) => {
objectsize = objectInfo.system.content_length;
}).catch((err) => {
console.log("Failed to get downloading object info");
console.log(err);
});
//

var size = { download : 0,
actuallyWritten : 0,
totalWritten : 0};
var buffer = new Buffer.alloc(BUFFER_SIZE);
//

var fileHandle = await fs.openSync(localFullFileName.dest, "w");
//

var loop = true;
while(loop) {
if((objectsize-size.totalWritten>0)&&(objectsize-size.totalWritten)<BUFFER_SIZE){
buffer = null;
buffer = new Buffer.alloc(objectsize-size.totalWritten);
}

//Reading data from storj V3 network
await download.read(buffer,buffer.length).then(async (bytesread) => {
size.download = bytesread.bytes_read;
Expand Down Expand Up @@ -287,10 +297,9 @@ async function downloadfile(project){
console.log("Failed to download file");
console.log(err);
});

}

//

//Connecting to storj network using Satellite Address , Storj API key , Encryption phassphrase
console.log("Getting Access\nSatellite Address : ",storjConfig.satelliteURL,"\nAPI key : ",storjConfig.apiKey,"\nEncryption Passphrase : ",storjConfig.encryptionPassphrase);
var access = libUplink.requestAccessWithPassphrase(storjConfig.satelliteURL,storjConfig.apiKey,storjConfig.encryptionPassphrase).then((access) => {
Expand All @@ -301,14 +310,14 @@ var access = libUplink.requestAccessWithPassphrase(storjConfig.satelliteURL,stor
console.log("Desired Storj Project: OPENED!");
//Fetching bucket info from the storj V3 network
console.log("Fetching Information About Bucket : ",storjConfig.bucketName);
//

await project.statBucket(storjConfig.bucketName).then((bucketInfo) => {
console.log("\nBucket Information : \n Bucket Name : ",bucketInfo.name,"\n Bucket Created : ",getDateTime(bucketInfo.created));
}).catch((err) => {
console.log("Failed to get bucket Info");
console.log(err);
});
//

//Creating bucket on storj V3 Network
console.log("\nCreating Bucket : ",storjConfig.bucketName);
await project.createBucket(storjConfig.bucketName).then((bucketInfo) => {
Expand All @@ -325,7 +334,7 @@ var access = libUplink.requestAccessWithPassphrase(storjConfig.satelliteURL,stor
console.log("Failed to fetch bucket Info");
console.log(err);
});
//

//Listing buckets on storj V3 network
var listBucketsOptions = new storj.ListBucketsOptions();
await project.listBuckets(listBucketsOptions).then(async (bucketListResult) => {
Expand All @@ -345,7 +354,7 @@ var access = libUplink.requestAccessWithPassphrase(storjConfig.satelliteURL,stor
});
//Uploading file on storj V3 network
await uploadfile(project);
//

//Fetching info of uploaded object
console.log("Fetching object Info...");
await project.statObject(storjConfig.bucketName,storjConfig.uploadPath).then((objectinfo) => {
Expand Down Expand Up @@ -381,7 +390,7 @@ var access = libUplink.requestAccessWithPassphrase(storjConfig.satelliteURL,stor
await downloadfile(project,libUplink);
//Creating share access
await accessshare(libUplink,access);
//

//Deleting bucket from the storj V3 network
console.log("Deleting Bucket : ",storjConfig.bucketName);
await project.deleteBucket(storjConfig.bucketName).then((bucketInfo) => {
Expand All @@ -403,17 +412,15 @@ var access = libUplink.requestAccessWithPassphrase(storjConfig.satelliteURL,stor
console.log(err);
}
});
//
//

console.log("Deleting Bucket : ",storjConfig.bucketName);
await project.deleteBucket(storjConfig.bucketName).then((bucketInfo) => {
console.log("\nBucket Deleted : \n Bucket Name : ",bucketInfo.name,"\n Bucket Created : ",getDateTime(bucketInfo.created));
}).catch((err) => {
console.log("Failed to delete bucket");
console.log(err);
});
//
//

//Closing opened project
await project.close().then(() => {
console.log("\nProject closed successfully");
Expand Down
2 changes: 1 addition & 1 deletion HelloStorjTS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const BUFFER_SIZE = 80000,
"apiKey": "change-me-to-the-api-key-created-in-satellite-gui",
"bucketName": "change-me-to-desired-bucket-name",
"encryptionPassphrase": "you'll never guess this",
"satelliteURL": "us-central-1.tardigrade.io:7777",
"satelliteURL": "change-me-to-desired-satellite-url",
"uploadPath": "optionalpath/requiredfilename"
},
//
Expand Down
Loading