/
storj.go
296 lines (243 loc) · 8.51 KB
/
storj.go
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
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package cmd
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log"
"os"
"path/filepath"
"strconv"
"time"
"storj.io/uplink"
)
// ConfigStorj depicts keys to search for within the stroj_config.json file.
type ConfigStorj struct {
APIKey string `json:"apikey"`
Satellite string `json:"satellite"`
Bucket string `json:"bucket"`
UploadPath string `json:"uploadPath"`
EncryptionPassphrase string `json:"encryptionpassphrase"`
SerializedAccess string `json:"serializedAccess"`
AllowDownload string `json:"allowDownload"`
AllowUpload string `json:"allowUpload"`
AllowList string `json:"allowList"`
AllowDelete string `json:"allowDelete"`
NotBefore string `json:"notBefore"`
NotAfter string `json:"notAfter"`
}
// LoadStorjConfiguration reads and parses the JSON file that contain Storj configuration information.
func LoadStorjConfiguration(fullFileName string) ConfigStorj {
var metric *Metric
if useDebug {
metric = &Metric{Function: "LoadStorjConfiguration"}
metric.start()
defer func() {
metric.end()
collectedMetrics = append(collectedMetrics, metric)
}()
}
var configStorj ConfigStorj
fileHandle, err := os.Open(filepath.Clean(fullFileName))
if err != nil {
log.Fatal("Could not load storj config file: ", err)
}
jsonParser := json.NewDecoder(fileHandle)
if err = jsonParser.Decode(&configStorj); err != nil {
log.Fatal(err)
}
// Close the file handle after reading from it.
if err = fileHandle.Close(); err != nil {
log.Fatal(err)
}
// Display storj configuration read from file.
fmt.Println("Read Storj configuration from the", fullFileName, "file.")
fmt.Println("API Key\t\t: ", configStorj.APIKey)
fmt.Println("Satellite : ", configStorj.Satellite)
fmt.Println("Bucket : ", configStorj.Bucket)
// Convert the upload path to standard form.
if configStorj.UploadPath != "" {
if configStorj.UploadPath == "/" {
configStorj.UploadPath = ""
} else {
checkSlash := configStorj.UploadPath[len(configStorj.UploadPath)-1:]
if checkSlash != "/" {
configStorj.UploadPath = configStorj.UploadPath + "/"
}
}
}
fmt.Println("Upload Path\t: ", configStorj.UploadPath)
fmt.Println("Serialized Access Key\t: ", configStorj.SerializedAccess)
return configStorj
}
// ShareAccess generates and prints the shareable serialized access
// as per the restrictions provided by the user.
func ShareAccess(access *uplink.Access, configStorj ConfigStorj) {
var metric *Metric
if useDebug {
metric = &Metric{Function: "ShareAccess"}
metric.start()
defer func() {
metric.end()
collectedMetrics = append(collectedMetrics, metric)
}()
}
allowDownload, _ := strconv.ParseBool(configStorj.AllowDownload)
allowUpload, _ := strconv.ParseBool(configStorj.AllowUpload)
allowList, _ := strconv.ParseBool(configStorj.AllowList)
allowDelete, _ := strconv.ParseBool(configStorj.AllowDelete)
notBefore, _ := time.Parse("2006-01-02_15:04:05", configStorj.NotBefore)
notAfter, _ := time.Parse("2006-01-02_15:04:05", configStorj.NotAfter)
permission := uplink.Permission{
AllowDownload: allowDownload,
AllowUpload: allowUpload,
AllowList: allowList,
AllowDelete: allowDelete,
NotBefore: notBefore,
NotAfter: notAfter,
}
// Create shared access.
sharedAccess, err := access.Share(permission)
if err != nil {
log.Fatal("Could not generate shared access: ", err)
}
// Generate restricted serialized access.
serializedAccess, err := sharedAccess.Serialize()
if err != nil {
log.Fatal("Could not serialize shared access: ", err)
}
fmt.Println("Shareable serialized access: ", serializedAccess)
}
// ConnectToStorj reads Storj configuration from given file
// and connects to the desired Storj network.
// It then reads data property from an external file.
func ConnectToStorj(configStorj ConfigStorj, accesskey bool) (*uplink.Access, *uplink.Project) {
var metric *Metric
if useDebug {
metric = &Metric{Function: "ConnectToStorj"}
metric.start()
defer func() {
metric.end()
collectedMetrics = append(collectedMetrics, metric)
}()
}
var access *uplink.Access
var cfg uplink.Config
// Configure the UserAgent
/* For a list of valid User Agents, refer to */
cfg.UserAgent = ""
ctx := context.Background()
var err error
if accesskey {
fmt.Println("Connecting to Storj network using Serialized access.")
// Generate access handle using serialized access.
access, err = uplink.ParseAccess(configStorj.SerializedAccess)
if err != nil {
log.Fatal(err)
}
} else {
fmt.Println("Connecting to Storj network.")
// Generate access handle using API key, satellite url and encryption passphrase.
access, err = cfg.RequestAccessWithPassphrase(ctx, configStorj.Satellite, configStorj.APIKey, configStorj.EncryptionPassphrase)
if err != nil {
log.Fatal(err)
}
}
// Open a new porject.
project, err := cfg.OpenProject(ctx, access)
if err != nil {
log.Fatal(err)
}
defer project.Close()
// Ensure the desired Bucket within the Project
_, err = project.EnsureBucket(ctx, configStorj.Bucket)
if err != nil {
log.Fatal(err)
}
return access, project
}
// UploadData uploads the backup file to storj network.
func UploadData(project *uplink.Project, configStorj ConfigStorj, uploadFileName string, fileReader *os.File) {
var metric *Metric
if useDebug {
metric = &Metric{Function: "UploadData"}
metric.start()
defer func() {
metric.end()
collectedMetrics = append(collectedMetrics, metric)
}()
}
ctx := context.Background()
// Create an upload handle.
upload, err := project.UploadObject(ctx, configStorj.Bucket, configStorj.UploadPath+filepath.Base(uploadFileName), nil)
if err != nil {
log.Fatal("Could not initiate upload : ", err)
}
fmt.Printf("Uploading %s to %s...\n", configStorj.UploadPath+filepath.Base(uploadFileName), configStorj.Bucket)
// ****Add the code here to create the reader for the file to be uploaded****
/* To directly copy the complete data to storj network, uncomment this code
and remvove/comment the section reader code snippet.
_, err = io.Copy(upload, fileReader)
if err != nil {
abortErr := upload.Abort()
log.Fatal("Could not upload data to storj: ", err, abortErr)
}
*/
// To implement uploading in parts, use the following approcach.
// This approach creates a section reader for the file handle from the current index
// to read the data in buffer with specified size and upload the corresponding data in sections.
dataProcessingAndCopy(upload, fileReader)
/* In case you have passed a byte array(buffer) to be uploaded,
comment the Copy Function block and use the following approach.
This approach creates a reader for 32KB section starting from the current position,
copies the 32KB buffer data and updaes the current position.
var lastIndex = 0
var buf = make([]byte, 32768)
// Loop to read the backup file in chunks and append the contents to the upload object.
for lastIndex < int(len(dataToUpload)) {
reader := bytes.NewBuffer(dataToUpload[lastIndex:min(lastIndex+cap(buf), len(dataToUpload))])
_, err = io.Copy(upload, reader)
lastIndex = lastIndex + cap(buf)
}
*/
// Commit the upload after copying the complete content of the backup file to upload object.
fmt.Println("Please wait while the upload is being committed to Storj.")
err = upload.Commit()
if err != nil {
log.Fatal("Could not commit object upload : ", err)
}
// Close file handle after reading from it.
if err = fileReader.Close(); err != nil {
log.Fatal(err)
}
}
// dataProcessingAndCopy implements the approcachof uploading data/file in parts.
// Code to modify the data to be uploaded can be added inside this Function.
// By default, no modification in the uploading data has been performed.
func dataProcessingAndCopy(upload *uplink.Upload, fileReader *os.File) {
var lastIndex int64
var numOfBytesRead int
var buf = make([]byte, 32768)
var err1 error
// Loop to read the backup file in chunks and append the contents to the upload object.
for err1 != io.EOF {
sectionReader := io.NewSectionReader(fileReader, lastIndex, int64(cap(buf)))
numOfBytesRead, err1 = sectionReader.ReadAt(buf, 0)
if numOfBytesRead > 0 {
reader := bytes.NewBuffer(buf[0:numOfBytesRead])
_, _ = io.Copy(upload, reader)
}
lastIndex = lastIndex + int64(numOfBytesRead)
}
}
/* Uncomment this Function if you are passing byte array(buffer) to the UploadData funtion.
func min(a, b int) int {
if a < b {
return a
}
return b
}
*/