@@ -2,17 +2,19 @@ const fs = require("fs");
2
2
3
3
//include the Node.js-Storj bindings module
4
4
const storj = require ( "uplink-nodejs" ) ;
5
- //object for all the function for uplink
5
+
6
+ //Object for all the function for uplink
6
7
const libUplink = new storj . Uplink ( ) ;
8
+
7
9
//Object for all error for uplink
8
10
const uplinkError = storj . errorhandle ;
9
- //
11
+
10
12
var BUFFER_SIZE = 80000 ;
11
- //
13
+
12
14
//demo Storj (V3) configuration
13
15
var storjConfig = {
14
16
apiKey : "change-me-to-the-api-key-created-in-satellite-gui" ,
15
- satelliteURL : "us-central-1.tardigrade.io:7777 " ,
17
+ satelliteURL : "change-me-to-desired-satellite-url " ,
16
18
encryptionPassphrase : "you'll never guess this" ,
17
19
bucketName : "change-me-to-desired-bucket-name" ,
18
20
uploadPath : "optionalpath/requiredfilename" ,
@@ -23,7 +25,7 @@ var localFullFileName = {
23
25
src : "change-me-to-source-file-name-at-local-system" ,
24
26
dest : "change-me-to-destination-file-name-at-local-system" ,
25
27
} ;
26
- //
28
+
27
29
function getDateTime ( unixTimestamp ) {
28
30
var dateTime = new Date ( unixTimestamp * 1000 ) ;
29
31
@@ -47,8 +49,10 @@ function getDateTime(unixTimestamp) {
47
49
48
50
//Hours part from the timestamp
49
51
var hours = "0" + dateTime . getHours ( ) ;
52
+
50
53
//Minutes part from the timestamp
51
54
var minutes = "0" + dateTime . getMinutes ( ) ;
55
+
52
56
//Seconds part from the timestamp
53
57
var seconds = "0" + dateTime . getSeconds ( ) ;
54
58
@@ -57,7 +61,7 @@ function getDateTime(unixTimestamp) {
57
61
58
62
return formattedDateTime ;
59
63
}
60
- //
64
+
61
65
async function accessshare ( uplink , access ) {
62
66
var permission = new storj . Permission ( false , false , false , true , 0 , 0 ) ;
63
67
var sharePrefix = storj . SharePrefix ;
@@ -66,31 +70,30 @@ async function accessshare(uplink,access){
66
70
sharePrefix . prefix = "change-me-to-desired-object-prefix-with-/" ;
67
71
sharePrefixListArray . push ( sharePrefix ) ;
68
72
await access . share ( permission , sharePrefixListArray , sharePrefixListArray . length ) . then ( async ( sharedAccess ) => {
69
- //
73
+
70
74
console . log ( "Serilazing share access" ) ;
71
75
await sharedAccess . serialize ( ) . then ( async ( stringResult ) => {
72
- //
76
+
73
77
console . log ( "Parsing access share..." ) ;
74
78
await uplink . parseAccess ( stringResult ) . then ( async ( parsedSharedAccess ) => {
75
- //
79
+
76
80
console . log ( "\nDeriving encryption key" ) ;
77
- //
81
+
78
82
var encryption = await libUplink . uplinkDeriveEncryptionKey ( "test" , [ 4 , 5 , 6 ] ) . catch ( ( error ) => {
79
83
console . log ( "Failed to derive encryption key" ) ;
80
84
console . log ( error ) ;
81
85
} ) ;
82
- //
86
+
83
87
console . log ( "Over riding encryption key" ) ;
84
- //
88
+
85
89
await parsedSharedAccess . overrideEncryptionKey ( sharePrefix . bucket , sharePrefix . prefix , encryption [ "encryption_key" ] ) . catch ( ( err ) => {
86
90
console . log ( "Failed to over write encryption key" ) ;
87
91
console . log ( err ) ;
88
92
} ) ;
89
- //
93
+
90
94
console . log ( "Opening project using paresed shared access" ) ;
91
95
await parsedSharedAccess . openProject ( ) . then ( async ( project ) => {
92
- //
93
- //
96
+
94
97
console . log ( "\nOpened Project using share access" ) ;
95
98
await project . deleteObject ( storjConfig . bucketName , storjConfig . uploadPath ) . then ( ( objectInfo ) => {
96
99
console . log ( "\nObject Deleted successfully !!" ) ;
@@ -99,7 +102,7 @@ async function accessshare(uplink,access){
99
102
console . log ( "Failed to delete object on storj V3 network using shared access" ) ;
100
103
console . log ( err ) ;
101
104
} ) ;
102
- //
105
+
103
106
await project . close ( ) . then ( ( ) => {
104
107
console . log ( "Project Closed !!" ) ;
105
108
} ) . catch ( ( err ) => {
@@ -126,10 +129,11 @@ async function accessshare(uplink,access){
126
129
/*eslint-disable */
127
130
async function uploadfile ( project ) {
128
131
console . log ( "Getting Upload Object...." ) ;
129
- //
132
+
130
133
var uploadOptions = new storj . UploadOptions ( ) ;
131
- //
134
+
132
135
uploadOptions . expires = 0 ;
136
+
133
137
//Uploading object on storj V3 network
134
138
await project . uploadObject ( storjConfig . bucketName , storjConfig . uploadPath , uploadOptions ) . then ( async ( upload ) => {
135
139
console . log ( localFullFileName . src , " File: UPLOADED as " , storjConfig . uploadPath , "!" ) ;
@@ -140,22 +144,23 @@ async function uploadfile(project){
140
144
actuallyWritten : 0 ,
141
145
totalWritten : 0
142
146
} ;
143
- //
147
+
144
148
var buffer = new Buffer . alloc ( BUFFER_SIZE ) ;
145
149
var loop = true ;
146
150
var bytesRead = 0 ;
151
+
147
152
while ( loop ) {
148
- //
153
+
149
154
size . toWrite = size . file - size . totalWritten ;
150
- //
155
+
151
156
if ( size . toWrite > BUFFER_SIZE ) {
152
157
size . toWrite = BUFFER_SIZE ;
153
158
} else if ( size . toWrite === 0 ) {
154
159
break ;
155
160
}
156
- //
161
+
157
162
bytesRead = await fs . readSync ( fileHandle , buffer , 0 , size . toWrite , size . totalWritten ) ;
158
- //
163
+
159
164
//Writing data on storj V3 network
160
165
await upload . write ( buffer , bytesRead ) . then ( ( writeResult ) => {
161
166
size . actuallyWritten = writeResult . bytes_written ;
@@ -172,7 +177,7 @@ async function uploadfile(project){
172
177
break ;
173
178
}
174
179
}
175
- //
180
+
176
181
var customMetadataEntry1 = new storj . CustomMetadataEntry ( ) ;
177
182
customMetadataEntry1 . key = "testing" ;
178
183
customMetadataEntry1 . key_length = customMetadataEntry1 . key . length ;
@@ -192,20 +197,23 @@ async function uploadfile(project){
192
197
var customMetadata = new storj . CustomMetadata ( ) ;
193
198
customMetadata . count = customMetadataEntryArray . length ;
194
199
customMetadata . entries = customMetadataEntryArray ;
200
+
195
201
//Adding custom meta to upload object
196
202
await upload . setCustomMetadata ( customMetadata ) . then ( ( ) => {
197
203
console . log ( "\nCustom Metadata set successfully" ) ;
198
204
} ) . catch ( ( err ) => {
199
205
console . log ( "Failed to set custom metadata" ) ;
200
206
console . log ( err ) ;
201
207
} ) ;
208
+
202
209
//Commiting object on storj V3 network
203
210
await upload . commit ( ) . then ( ( ) => {
204
211
console . log ( "\nObject on storj V3 network successfully" ) ;
205
212
} ) . catch ( ( err ) => {
206
213
console . log ( "Failed to commit object on storj V3 network" ) ;
207
214
console . log ( err ) ;
208
215
} ) ;
216
+
209
217
//Fetching info of uploaded object on storj V3 network
210
218
await upload . info ( ) . then ( ( object ) => {
211
219
console . log ( "\nObject Info" ) ;
@@ -214,7 +222,7 @@ async function uploadfile(project){
214
222
console . log ( "Failed to fetch information about object" ) ;
215
223
console . log ( err ) ;
216
224
} ) ;
217
- //
225
+
218
226
fs . closeSync ( fileHandle ) ;
219
227
} ) . catch ( ( err ) => {
220
228
console . log ( "Failed to upload object on storj V3" ) ;
@@ -228,32 +236,34 @@ async function downloadfile(project){
228
236
var downloadOptions = new storj . DownloadOptions ( ) ;
229
237
downloadOptions . offset = 0 ;
230
238
downloadOptions . length = - 1 ;
239
+
231
240
//Downloading file
232
241
console . log ( "Downloading file" ) ;
233
242
await project . downloadObject ( storjConfig . bucketName , storjConfig . uploadPath , downloadOptions ) . then ( async ( download ) => {
234
243
var objectsize = 0 ;
235
- //
244
+
236
245
console . log ( "Fetching download object info" ) ;
237
246
await download . info ( ) . then ( ( objectInfo ) => {
238
247
objectsize = objectInfo . system . content_length ;
239
248
} ) . catch ( ( err ) => {
240
249
console . log ( "Failed to get downloading object info" ) ;
241
250
console . log ( err ) ;
242
251
} ) ;
243
- //
252
+
244
253
var size = { download : 0 ,
245
254
actuallyWritten : 0 ,
246
255
totalWritten : 0 } ;
247
256
var buffer = new Buffer . alloc ( BUFFER_SIZE ) ;
248
- //
257
+
249
258
var fileHandle = await fs . openSync ( localFullFileName . dest , "w" ) ;
250
- //
259
+
251
260
var loop = true ;
252
261
while ( loop ) {
253
262
if ( ( objectsize - size . totalWritten > 0 ) && ( objectsize - size . totalWritten ) < BUFFER_SIZE ) {
254
263
buffer = null ;
255
264
buffer = new Buffer . alloc ( objectsize - size . totalWritten ) ;
256
265
}
266
+
257
267
//Reading data from storj V3 network
258
268
await download . read ( buffer , buffer . length ) . then ( async ( bytesread ) => {
259
269
size . download = bytesread . bytes_read ;
@@ -287,10 +297,9 @@ async function downloadfile(project){
287
297
console . log ( "Failed to download file" ) ;
288
298
console . log ( err ) ;
289
299
} ) ;
290
-
291
300
}
292
301
293
- //
302
+
294
303
//Connecting to storj network using Satellite Address , Storj API key , Encryption phassphrase
295
304
console . log ( "Getting Access\nSatellite Address : " , storjConfig . satelliteURL , "\nAPI key : " , storjConfig . apiKey , "\nEncryption Passphrase : " , storjConfig . encryptionPassphrase ) ;
296
305
var access = libUplink . requestAccessWithPassphrase ( storjConfig . satelliteURL , storjConfig . apiKey , storjConfig . encryptionPassphrase ) . then ( ( access ) => {
@@ -301,14 +310,14 @@ var access = libUplink.requestAccessWithPassphrase(storjConfig.satelliteURL,stor
301
310
console . log ( "Desired Storj Project: OPENED!" ) ;
302
311
//Fetching bucket info from the storj V3 network
303
312
console . log ( "Fetching Information About Bucket : " , storjConfig . bucketName ) ;
304
- //
313
+
305
314
await project . statBucket ( storjConfig . bucketName ) . then ( ( bucketInfo ) => {
306
315
console . log ( "\nBucket Information : \n Bucket Name : " , bucketInfo . name , "\n Bucket Created : " , getDateTime ( bucketInfo . created ) ) ;
307
316
} ) . catch ( ( err ) => {
308
317
console . log ( "Failed to get bucket Info" ) ;
309
318
console . log ( err ) ;
310
319
} ) ;
311
- //
320
+
312
321
//Creating bucket on storj V3 Network
313
322
console . log ( "\nCreating Bucket : " , storjConfig . bucketName ) ;
314
323
await project . createBucket ( storjConfig . bucketName ) . then ( ( bucketInfo ) => {
@@ -325,7 +334,7 @@ var access = libUplink.requestAccessWithPassphrase(storjConfig.satelliteURL,stor
325
334
console . log ( "Failed to fetch bucket Info" ) ;
326
335
console . log ( err ) ;
327
336
} ) ;
328
- //
337
+
329
338
//Listing buckets on storj V3 network
330
339
var listBucketsOptions = new storj . ListBucketsOptions ( ) ;
331
340
await project . listBuckets ( listBucketsOptions ) . then ( async ( bucketListResult ) => {
@@ -345,7 +354,7 @@ var access = libUplink.requestAccessWithPassphrase(storjConfig.satelliteURL,stor
345
354
} ) ;
346
355
//Uploading file on storj V3 network
347
356
await uploadfile ( project ) ;
348
- //
357
+
349
358
//Fetching info of uploaded object
350
359
console . log ( "Fetching object Info..." ) ;
351
360
await project . statObject ( storjConfig . bucketName , storjConfig . uploadPath ) . then ( ( objectinfo ) => {
@@ -381,7 +390,7 @@ var access = libUplink.requestAccessWithPassphrase(storjConfig.satelliteURL,stor
381
390
await downloadfile ( project , libUplink ) ;
382
391
//Creating share access
383
392
await accessshare ( libUplink , access ) ;
384
- //
393
+
385
394
//Deleting bucket from the storj V3 network
386
395
console . log ( "Deleting Bucket : " , storjConfig . bucketName ) ;
387
396
await project . deleteBucket ( storjConfig . bucketName ) . then ( ( bucketInfo ) => {
@@ -403,17 +412,15 @@ var access = libUplink.requestAccessWithPassphrase(storjConfig.satelliteURL,stor
403
412
console . log ( err ) ;
404
413
}
405
414
} ) ;
406
- //
407
- //
415
+
408
416
console . log ( "Deleting Bucket : " , storjConfig . bucketName ) ;
409
417
await project . deleteBucket ( storjConfig . bucketName ) . then ( ( bucketInfo ) => {
410
418
console . log ( "\nBucket Deleted : \n Bucket Name : " , bucketInfo . name , "\n Bucket Created : " , getDateTime ( bucketInfo . created ) ) ;
411
419
} ) . catch ( ( err ) => {
412
420
console . log ( "Failed to delete bucket" ) ;
413
421
console . log ( err ) ;
414
422
} ) ;
415
- //
416
- //
423
+
417
424
//Closing opened project
418
425
await project . close ( ) . then ( ( ) => {
419
426
console . log ( "\nProject closed successfully" ) ;
0 commit comments