-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHelloStorj.py
More file actions
467 lines (422 loc) · 15.8 KB
/
HelloStorj.py
File metadata and controls
467 lines (422 loc) · 15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
from storjPython.uplinkPython import *
from datetime import datetime
"""
example function to put/upload data from srcFullFileName (at local computer) to
Storj (V3) bucket's path
"""
def upload_file(storjObj, bucketHandle, uploadPath, uploadOptions, srcFullFileName):
#
# open file to be uploaded
file_handle = open(srcFullFileName, 'r+b')
data_len = os.path.getsize(srcFullFileName)
#
# call to get uploader handle
uploader, ls_Error = storjObj.upload(bucketHandle, uploadPath, uploadOptions)
if ls_Error is not None:
return False, ls_Error
#
# initialize local variables and start uploading packets of data
uploaded_total = 0
while uploaded_total < data_len:
# set packet size to be used while uploading
size_to_write = 256 if (data_len - uploaded_total > 256) else data_len - uploaded_total
#
# exit while loop if nothing left to upload
if size_to_write == 0:
break
#
# file reading process from the last read position
file_handle.seek(uploaded_total)
lc_dataToWrite = file_handle.read(size_to_write)
#
# --------------------------------------------
# data conversion to type required by function
# get size of data in c type int32 variable
lc_data_size = c_int32(len(lc_dataToWrite))
# conversion of read bytes data to c type ubyte Array
lc_dataToWrite = (c_uint8 * lc_data_size.value)(*lc_dataToWrite)
# conversion of c type ubyte Array to LP_c_ubyte required by upload write function
lc_dataToWritePtr = cast(lc_dataToWrite, POINTER(c_uint8))
# --------------------------------------------
#
# call to write data to Storj bucket
write_size, ls_Error = storjObj.upload_write(uploader, lc_dataToWritePtr, size_to_write)
if ls_Error is not None:
return False, ls_Error
#
# exit while loop if nothing left to upload / unable to upload
if write_size == 0:
break
# update last read location
uploaded_total += write_size
# commit upload data to bucket
ls_Error = storjObj.upload_commit(uploader)
#
# if error occurred
if ls_Error is not None:
return False, ls_Error
else:
return True, None
"""
example function to get/download Storj(V3) object's data and store it in given file
with destFullFileName (on local computer)
"""
def download_file(storjObj, bucketHandle, storjPath, destFullPathName):
#
# open / create file with the given name to save the downloaded data
file_handle = open(destFullPathName, 'w+b')
#
# call to get downloader handle
downloader, ls_Error = storjObj.download(bucketHandle, storjPath)
if ls_Error is not None:
return False, ls_Error
#
# get size of file to be downloaded from storj
file_size, ls_Error = get_file_size(storjObj,bucketHandle,storjPath)
if ls_Error is not None:
return False, ls_Error
else:
print("File size to be downloaded: ",file_size)
#
# set packet size to be used while downloading
size_to_read = 256
# initialize local variables and start downloading packets of data
downloaded_total = 0
while True:
# call to read data from Storj bucket
lc_dataReadPtr, read_size, ls_Error = storjObj.download_read(downloader, size_to_read)
if ls_Error is not None:
return False, ls_Error
#
# file writing process from the last written position if new data is downloaded
if read_size != 0:
#
# --------------------------------------------
# data conversion to type python readable form
# conversion of LP_c_ubyte to python readable data variable
lc_dataRead = string_at(lc_dataReadPtr, read_size)
# --------------------------------------------
#
file_handle.seek(downloaded_total)
file_handle.write(lc_dataRead)
#
# update last read location
downloaded_total += read_size
#
# break if download complete
if downloaded_total == file_size:
break
#
# close downloader and free downloader access
ls_Error = storjObj.download_close(downloader)
#
# if error occurred
if ls_Error is not None:
return False, ls_Error
else:
return True, None
"""
example function to get Storj(V3) object's size to be downloaded
"""
def get_file_size(storjObj, bucketHandle, storjPath):
#
# create list option object
lO_listOption = ListOptions()
lO_listOption.prefix = c_char_p("".encode('utf-8'))
lO_listOption.cursor = c_char_p("".encode('utf-8'))
lO_listOption.delimiter = c_char(' '.encode('utf-8'))
lO_listOption.recursive = True
lO_listOption.direction = STORJ_AFTER
lO_listOption.limit = 0
#
# get list of objects in specified bucket
objectsList, err = storjObj.list_objects(bucketHandle, lO_listOption)
if err is not None:
return 0, err
else:
# find object using path
for i in range(objectsList.length):
if objectsList.items[i].path is None:
break
if objectsList.items[i].path.decode('utf-8') == storjPath:
return int(objectsList.items[i].size), None
# free and delete list configuration object
lO_listOption = None
del lO_listOption
#
# if object not found
return 0, "Object not found in bucket"
if __name__ == "__main__":
# Storj configuration information
myAPIKey = "change-me-to-the-api-key-created-in-satellite-gui"
satellite = "us-central-1.tardigrade.io:7777"
myBucket = "my-first-bucket"
myStorjUploadPath = "(optional): path / (required): filename" # (path + filename) OR filename
myEncryptionPassphrase = "you'll never guess this"
# Source and destination path and file name for testing
srcFullFileName = "filename with extension of source file on local system"
destFullFileName = "filename with extension to save on local system"
# create an object of libUplinkPy class
StorjObj = libUplinkPy()
# function calls
# create new uplink
print("\nSetting-up new uplink...")
uplinkHandle, err = StorjObj.new_uplink()
if err is not None:
print(err)
exit()
print("New uplink: SET-UP!")
#
# parse Api Key
print("\nParsing the API Key...")
parseApiKeyHandle, err = StorjObj.parse_api_key(myAPIKey)
if err is not None:
print(err)
exit()
print("API Key: PARSED!")
#
# open Storj project
print(
"\nOpening the Storj project, corresponding to the parsed API Key, on " + satellite + " satellite...")
projectHandle, err = StorjObj.open_project(uplinkHandle, parseApiKeyHandle, satellite)
if err is not None:
print(err)
exit()
print("Desired Storj project: OPENED!")
#
# enlist all the buckets in given Storj project
print("\nListing bucket's names and creation time...")
bucketsList, err = StorjObj.list_buckets(projectHandle, None)
if err is not None:
print(err)
exit()
else:
# print all bucket name and creation time
for i in range(bucketsList.length):
if bucketsList.items[i].name is None:
break
print(bucketsList.items[i].name.decode('utf-8'), " | ",
datetime.fromtimestamp(bucketsList.items[i].created))
print("Buckets listing: COMPLETE!")
#
# delete given bucket
print("\nDeleting '" + myBucket + "' bucket...")
err = StorjObj.delete_bucket(projectHandle, myBucket)
if err is not None:
print(err)
else:
print("Desired bucket: DELETED")
#
# set bucket config according to this link:
# https://godoc.org/storj.io/storj/lib/uplink#BucketConfig
lO_configBucket = BucketConfig()
lO_configBucket.path_cipher = STORJ_ENC_AESGCM
lO_configBucket.encryption_parameters.cipher_suite = STORJ_ENC_AESGCM
lO_configBucket.encryption_parameters.block_size = 7424
lO_configBucket.redundancy_scheme.algorithm = STORJ_REED_SOLOMON
lO_configBucket.redundancy_scheme.share_size = 256
lO_configBucket.redundancy_scheme.required_shares = 29
lO_configBucket.redundancy_scheme.repair_shares = 35
lO_configBucket.redundancy_scheme.optimal_shares = 80
lO_configBucket.redundancy_scheme.total_shares = 130
# create bucket in given project with above configuration or None
print("\nCreating '" + myBucket + "' bucket...")
bucketHandle, err = StorjObj.create_bucket(projectHandle, myBucket, lO_configBucket)
if err is not None:
print(err)
exit()
print("Desired Bucket: CREATED!")
# free and delete bucket configuration object
lO_configBucket = None
del lO_configBucket
#
# get encryption access to upload and download data
print("\nCreating serialized encryption key...")
serializedEncryptionAccess, err = StorjObj.get_encryption_access(projectHandle, myEncryptionPassphrase)
if err is not None:
print(err)
exit()
print("Serialized encryption key: CREATED!")
#
# open bucket in given project with given name and access
print("\nOpening '" + myBucket + "' bucket...")
bucketHandle, err = StorjObj.open_bucket(projectHandle, serializedEncryptionAccess, myBucket)
if err is not None:
print(err)
exit()
print("Desired bucket: OPENED!")
#
# as an example of 'put' , lets read and upload a local file
# upload file/object
print("\nUploading data...")
uploadStatus, err = upload_file(StorjObj, bucketHandle, myStorjUploadPath, None, srcFullFileName)
if err is not None or uploadStatus is False:
print(err)
exit()
print("Upload: COMPLETE!")
#
# set list options before calling list objects (optional)
lO_listOption = ListOptions()
lO_listOption.prefix = c_char_p("".encode('utf-8'))
lO_listOption.cursor = c_char_p("".encode('utf-8'))
lO_listOption.delimiter = c_char(' '.encode('utf-8'))
lO_listOption.recursive = True
lO_listOption.direction = STORJ_AFTER
lO_listOption.limit = 0
# list objects in given bucket with above options or None
print("\nListing object's names...")
objectsList, err = StorjObj.list_objects(bucketHandle, lO_listOption)
if err is not None:
print(err)
exit()
else:
# print all objects path
for i in range(objectsList.length):
if objectsList.items[i].path is None:
break
print(objectsList.items[i].path.decode('utf-8'))
print("Objects listing: COMPLETE!")
# free and delete bucket configuration object
lO_listOption = None
del lO_listOption
#
# as an example of 'get' , lets download an object and write it to a local file
# download file/object
print("\nDownloading data...")
downloadStatus, err = download_file(StorjObj, bucketHandle, myStorjUploadPath, destFullFileName)
if err is not None or downloadStatus is False:
print(err)
exit()
print("Download: COMPLETE!")
#
# as an example of how to create shareable Scope key for easy storj access without API key and Encryption PassPhrase
# create new Scope
print("\nCreating new Scope...")
newScopeHandle, err = StorjObj.new_scope(satellite, parseApiKeyHandle, serializedEncryptionAccess)
if err is not None:
print(err)
exit()
print("New Scope: CREATED!")
#
# generate serialized Scope key
print("\nGenerating serialized Scope key...")
serializedScope, err = StorjObj.serialize_scope(newScopeHandle)
if err is not None:
print(err)
exit()
print("Serialized Scope key: ", serializedScope)
#
#
# close given bucket using handle
print("\nClosing bucket...")
err = StorjObj.close_bucket(bucketHandle)
if err is not None:
print(err)
exit()
print("Bucket CLOSED!")
#
# close given project using handle
print("\nClosing Storj project...")
err = StorjObj.close_project(projectHandle)
if err is not None:
print(err)
exit()
print("Project CLOSED!")
#
# close given uplink using handle
print("\nClosing uplink...")
err = StorjObj.close_uplink(uplinkHandle)
if err is not None:
print(err)
exit()
print("Uplink CLOSED!")
#
# as an example of how to retrieve information from shareable Scope key for storj access
# retrieving Scope from serialized Scope key
print("\nParsing serialized Scope key...")
parsedScope, err = StorjObj.parse_scope(serializedScope)
if err is not None:
print(err)
exit()
print("Parsing Scope key: COMPLETE")
#
# retrieving satellite from Scope
print("\nRetrieving satellite address from Scope...")
satelliteFromScope, err = StorjObj.get_scope_satellite_address(parsedScope)
if err is not None:
print(err)
exit()
print("Satellite address from Scope: ", satelliteFromScope)
#
# retrieving API key from Scope
print("\nRetrieving API key from Scope...")
apiKeyFromScope, err = StorjObj.get_scope_api_key(parsedScope)
if err is not None:
print(err)
exit()
print("API key from Scope: PARSED!")
#
# retrieving encryption access from Scope
print("\nRetrieving Encryption Access from Scope...")
encryptionAccessFromScope, err = StorjObj.get_scope_enc_access(parsedScope)
if err is not None:
print(err)
exit()
print("Encryption Access from Scope: PARSED!")
#
# create new uplink
print("\nSetting-up new uplink...")
uplinkScopeHandle, err = StorjObj.new_uplink()
if err is not None:
print(err)
exit()
print("New uplink: SET-UP!")
#
# open Storj project using Scope key values
print(
"\nOpening the Storj project, corresponding to the parsed API Key, on " + satellite + " satellite...")
projectScopeHandle, err = StorjObj.open_project(uplinkScopeHandle, apiKeyFromScope, satelliteFromScope)
if err is not None:
print(err)
exit()
print("Desired Storj project: OPENED!")
# open bucket in given project with given name and access
print("\nOpening '" + myBucket + "' bucket...")
bucketScopeHandle, err = StorjObj.open_bucket(projectScopeHandle, encryptionAccessFromScope, myBucket)
if err is not None:
print(err)
exit()
print("Desired bucket: OPENED!")
#
# delete given object
print("\nDeleting '" + myStorjUploadPath + "' object...")
err = StorjObj.delete_object(bucketScopeHandle, myStorjUploadPath)
if err is not None:
print(err)
else:
print("Desired object: DELETED!")
#
# close given bucket using handle
print("\nClosing bucket...")
err = StorjObj.close_bucket(bucketScopeHandle)
if err is not None:
print(err)
exit()
print("Bucket CLOSED!")
#
# close given project using handle
print("\nClosing Storj project...")
err = StorjObj.close_project(projectScopeHandle)
if err is not None:
print(err)
exit()
print("Project CLOSED!")
#
# close given uplink using handle
print("\nClosing uplink...")
err = StorjObj.close_uplink(uplinkScopeHandle)
if err is not None:
print(err)
exit()
print("Uplink CLOSED!")
#