-
Notifications
You must be signed in to change notification settings - Fork 0
/
glacier.go
803 lines (770 loc) · 29.3 KB
/
glacier.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
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
package aws
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/glacier"
)
// VaultInventory - Vault inventory struct used for unmarshaling data
type VaultInventory struct {
VaultARN string `json:"VaultARN"`
InventoryDate time.Time `json:"InventoryDate"`
ArchiveList []struct {
ArchiveID string `json:"ArchiveId"`
ArchiveDescription string `json:"ArchiveDescription"`
CreationDate time.Time `json:"CreationDate"`
Size int `json:"Size"`
SHA256TreeHash string `json:"SHA256TreeHash"`
} `json:"ArchiveList"`
}
// GetVaultLock - Retrieve vault lock-policy related attributes that are set on a vault
func GetVaultLock(awsRegion, vaultName string) {
svc := glacier.New(session.New(), aws.NewConfig().WithRegion(awsRegion))
input := &glacier.GetVaultLockInput{
AccountId: aws.String("-"),
VaultName: aws.String(vaultName),
}
result, err := svc.GetVaultLock(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case glacier.ErrCodeResourceNotFoundException:
fmt.Println(glacier.ErrCodeResourceNotFoundException, aerr.Error())
case glacier.ErrCodeInvalidParameterValueException:
fmt.Println(glacier.ErrCodeInvalidParameterValueException, aerr.Error())
case glacier.ErrCodeMissingParameterValueException:
fmt.Println(glacier.ErrCodeMissingParameterValueException, aerr.Error())
case glacier.ErrCodeServiceUnavailableException:
fmt.Println(glacier.ErrCodeServiceUnavailableException, aerr.Error())
default:
fmt.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
}
return
}
fmt.Println(result)
}
// GetVaultAccessPolicy - Get the access-policy set on the vault
func GetVaultAccessPolicy(awsRegion, vaultName string) {
svc := glacier.New(session.New(), aws.NewConfig().WithRegion(awsRegion))
input := &glacier.GetVaultAccessPolicyInput{
AccountId: aws.String("-"),
VaultName: aws.String(vaultName),
}
result, err := svc.GetVaultAccessPolicy(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case glacier.ErrCodeResourceNotFoundException:
fmt.Println(glacier.ErrCodeResourceNotFoundException, aerr.Error())
case glacier.ErrCodeInvalidParameterValueException:
fmt.Println(glacier.ErrCodeInvalidParameterValueException, aerr.Error())
case glacier.ErrCodeMissingParameterValueException:
fmt.Println(glacier.ErrCodeMissingParameterValueException, aerr.Error())
case glacier.ErrCodeServiceUnavailableException:
fmt.Println(glacier.ErrCodeServiceUnavailableException, aerr.Error())
default:
fmt.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
}
return
}
fmt.Println(result)
}
// DeleteArchive - Delete archive
func DeleteArchive(awsRegion, vaultName, ArchiveID string) {
svc := glacier.New(session.New(), aws.NewConfig().WithRegion(awsRegion))
input := &glacier.DeleteArchiveInput{
AccountId: aws.String("-"),
ArchiveId: aws.String(ArchiveID),
VaultName: aws.String(vaultName),
}
result, err := svc.DeleteArchive(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case glacier.ErrCodeResourceNotFoundException:
fmt.Println(glacier.ErrCodeResourceNotFoundException, aerr.Error())
case glacier.ErrCodeInvalidParameterValueException:
fmt.Println(glacier.ErrCodeInvalidParameterValueException, aerr.Error())
case glacier.ErrCodeMissingParameterValueException:
fmt.Println(glacier.ErrCodeMissingParameterValueException, aerr.Error())
case glacier.ErrCodeServiceUnavailableException:
fmt.Println(glacier.ErrCodeServiceUnavailableException, aerr.Error())
default:
fmt.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
}
return
}
fmt.Println(result)
}
// InitInventoryRetrieval - Initiate an inventory-retrieval job based on vault name
func InitInventoryRetrieval(awsRegion, vaultName, jobDescription string) {
svc := glacier.New(session.New(), aws.NewConfig().WithRegion(awsRegion))
input := &glacier.InitiateJobInput{
AccountId: aws.String("-"),
JobParameters: &glacier.JobParameters{
Description: aws.String(jobDescription),
Type: aws.String("inventory-retrieval"),
},
VaultName: aws.String(vaultName),
}
result, err := svc.InitiateJob(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case glacier.ErrCodeResourceNotFoundException:
fmt.Println(glacier.ErrCodeResourceNotFoundException, aerr.Error())
case glacier.ErrCodePolicyEnforcedException:
fmt.Println(glacier.ErrCodePolicyEnforcedException, aerr.Error())
case glacier.ErrCodeInvalidParameterValueException:
fmt.Println(glacier.ErrCodeInvalidParameterValueException, aerr.Error())
case glacier.ErrCodeMissingParameterValueException:
fmt.Println(glacier.ErrCodeMissingParameterValueException, aerr.Error())
case glacier.ErrCodeInsufficientCapacityException:
fmt.Println(glacier.ErrCodeInsufficientCapacityException, aerr.Error())
case glacier.ErrCodeServiceUnavailableException:
fmt.Println(glacier.ErrCodeServiceUnavailableException, aerr.Error())
default:
fmt.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
}
return
}
fmt.Println(result)
}
// InitArchiveRetrieval - Initiate an archive-retrieval job based on vault name
func InitArchiveRetrieval(awsRegion, vaultName, jobDescription, archiveID string) {
svc := glacier.New(session.New(), aws.NewConfig().WithRegion(awsRegion))
input := &glacier.InitiateJobInput{
AccountId: aws.String("-"),
JobParameters: &glacier.JobParameters{
ArchiveId: aws.String(archiveID),
Description: aws.String(jobDescription),
Type: aws.String("archive-retrieval"),
},
VaultName: aws.String(vaultName),
}
result, err := svc.InitiateJob(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case glacier.ErrCodeResourceNotFoundException:
fmt.Println(glacier.ErrCodeResourceNotFoundException, aerr.Error())
case glacier.ErrCodePolicyEnforcedException:
fmt.Println(glacier.ErrCodePolicyEnforcedException, aerr.Error())
case glacier.ErrCodeInvalidParameterValueException:
fmt.Println(glacier.ErrCodeInvalidParameterValueException, aerr.Error())
case glacier.ErrCodeMissingParameterValueException:
fmt.Println(glacier.ErrCodeMissingParameterValueException, aerr.Error())
case glacier.ErrCodeInsufficientCapacityException:
fmt.Println(glacier.ErrCodeInsufficientCapacityException, aerr.Error())
case glacier.ErrCodeServiceUnavailableException:
fmt.Println(glacier.ErrCodeServiceUnavailableException, aerr.Error())
default:
fmt.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
}
return
}
fmt.Println(result)
}
// InitJobInput - Initiate an inventory-retrieval job based on vault name
// Resource - https://docs.aws.amazon.com/sdk-for-go/api/service/glacier/#example_Glacier_InitiateJob_shared00
// This operation initiates a job of the specified type, which can be a select, an archival retrieval, or a vault retrieval.
func InitJobInput(awsRegion, vaultName string) {
svc := glacier.New(session.New(), aws.NewConfig().WithRegion(awsRegion))
input := &glacier.InitiateJobInput{
AccountId: aws.String("-"),
JobParameters: &glacier.JobParameters{
Description: aws.String("My inventory job"),
//Format: aws.String("CSV"),
//SNSTopic: aws.String("arn:aws:sns:us-west-2:111111111111:Glacier-InventoryRetrieval-topic-Example"),
// archive-retrieval - Retrieve an archive
// inventory-retrieval - Inventory a vault
Type: aws.String("archive-retrieval"),
},
VaultName: aws.String(vaultName),
}
result, err := svc.InitiateJob(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case glacier.ErrCodeResourceNotFoundException:
fmt.Println(glacier.ErrCodeResourceNotFoundException, aerr.Error())
case glacier.ErrCodePolicyEnforcedException:
fmt.Println(glacier.ErrCodePolicyEnforcedException, aerr.Error())
case glacier.ErrCodeInvalidParameterValueException:
fmt.Println(glacier.ErrCodeInvalidParameterValueException, aerr.Error())
case glacier.ErrCodeMissingParameterValueException:
fmt.Println(glacier.ErrCodeMissingParameterValueException, aerr.Error())
case glacier.ErrCodeInsufficientCapacityException:
fmt.Println(glacier.ErrCodeInsufficientCapacityException, aerr.Error())
case glacier.ErrCodeServiceUnavailableException:
fmt.Println(glacier.ErrCodeServiceUnavailableException, aerr.Error())
default:
fmt.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
}
return
}
fmt.Println(result)
}
// GetVautlInventory - Get the output of a previously initiated job for inventory retrieval that is identified by the job ID
func GetVautlInventory(awsRegion, vaultName, jobID string) {
svc := glacier.New(session.New(), aws.NewConfig().WithRegion(awsRegion))
input := &glacier.GetJobOutputInput{
AccountId: aws.String("-"),
JobId: aws.String(jobID),
Range: aws.String(""),
VaultName: aws.String(vaultName),
}
result, err := svc.GetJobOutput(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case glacier.ErrCodeResourceNotFoundException:
fmt.Println(glacier.ErrCodeResourceNotFoundException, aerr.Error())
case glacier.ErrCodeInvalidParameterValueException:
fmt.Println(glacier.ErrCodeInvalidParameterValueException, aerr.Error())
case glacier.ErrCodeMissingParameterValueException:
fmt.Println(glacier.ErrCodeMissingParameterValueException, aerr.Error())
case glacier.ErrCodeServiceUnavailableException:
fmt.Println(glacier.ErrCodeServiceUnavailableException, aerr.Error())
default:
fmt.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
}
return
}
//fmt.Println(result)
body, err := ioutil.ReadAll(result.Body)
var inventory VaultInventory
err1 := json.Unmarshal(body, &inventory)
if err1 != nil {
fmt.Println("error:", err)
}
t := inventory.ArchiveList
fmt.Printf("VaultARN: %s\n", inventory.VaultARN)
fmt.Printf("InventoryDate: %s\n", inventory.InventoryDate)
for _, j := range t {
fmt.Printf("ArchiveID: %s\n", j.ArchiveID)
fmt.Printf("ArchiveDescription %s\n", j.ArchiveDescription)
fmt.Printf("CreationDate %s\n", j.CreationDate)
fmt.Printf("Size: %v\n", j.Size)
fmt.Printf("SHA256TreeHash: %v\n", j.SHA256TreeHash)
}
}
/* TODO: In the case of an archive retrieval job, depending on the byte range you specify,
Amazon Glacier returns the checksum for the portion of the data.
To ensure the portion you downloaded is the correct data, compute the checksum on the client,
verify that the values match, and verify that the size is what you expected.
*/
// GetVaultArchive - Get the output of a previously initiated job for archive retrieval
// GetJobOutput - Get the output of a previously initiated job, for instance inventory retrieval job that is identified by the job ID
// https://docs.aws.amazon.com/amazonglacier/latest/dev/api-job-output-get.html
func GetVaultArchive(awsRegion, vaultName, jobID, fileName string) {
svc := glacier.New(session.New(), aws.NewConfig().WithRegion(awsRegion))
input := &glacier.GetJobOutputInput{
AccountId: aws.String("-"),
JobId: aws.String(jobID),
Range: aws.String(""),
VaultName: aws.String(vaultName),
}
result, err := svc.GetJobOutput(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case glacier.ErrCodeResourceNotFoundException:
fmt.Println(glacier.ErrCodeResourceNotFoundException, aerr.Error())
case glacier.ErrCodeInvalidParameterValueException:
fmt.Println(glacier.ErrCodeInvalidParameterValueException, aerr.Error())
case glacier.ErrCodeMissingParameterValueException:
fmt.Println(glacier.ErrCodeMissingParameterValueException, aerr.Error())
case glacier.ErrCodeServiceUnavailableException:
fmt.Println(glacier.ErrCodeServiceUnavailableException, aerr.Error())
default:
fmt.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
}
return
}
fmt.Println(result)
outFile, err1 := os.Create(fileName)
if err1 != nil {
panic(err)
}
defer outFile.Close()
_, err1 = io.Copy(outFile, result.Body)
}
// ListJobs - List all pending jobs per vault
func ListJobs(awsRegion, vaultName string) {
svc := glacier.New(session.New(), aws.NewConfig().WithRegion(awsRegion))
input := &glacier.ListJobsInput{
AccountId: aws.String("-"),
VaultName: aws.String(vaultName),
}
result, err := svc.ListJobs(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case glacier.ErrCodeResourceNotFoundException:
fmt.Println(glacier.ErrCodeResourceNotFoundException, aerr.Error())
case glacier.ErrCodeInvalidParameterValueException:
fmt.Println(glacier.ErrCodeInvalidParameterValueException, aerr.Error())
case glacier.ErrCodeMissingParameterValueException:
fmt.Println(glacier.ErrCodeMissingParameterValueException, aerr.Error())
case glacier.ErrCodeServiceUnavailableException:
fmt.Println(glacier.ErrCodeServiceUnavailableException, aerr.Error())
default:
fmt.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
}
return
}
fmt.Println(result)
}
// DescribeJob - Get information about a previously initiated job, specified by the job ID.
func DescribeJob(awsRegion, vaultName, jobID string) {
svc := glacier.New(session.New(), aws.NewConfig().WithRegion(awsRegion))
input := &glacier.DescribeJobInput{
AccountId: aws.String("-"),
JobId: aws.String(jobID),
VaultName: aws.String(vaultName),
}
result, err := svc.DescribeJob(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case glacier.ErrCodeResourceNotFoundException:
fmt.Println(glacier.ErrCodeResourceNotFoundException, aerr.Error())
case glacier.ErrCodeInvalidParameterValueException:
fmt.Println(glacier.ErrCodeInvalidParameterValueException, aerr.Error())
case glacier.ErrCodeMissingParameterValueException:
fmt.Println(glacier.ErrCodeMissingParameterValueException, aerr.Error())
case glacier.ErrCodeServiceUnavailableException:
fmt.Println(glacier.ErrCodeServiceUnavailableException, aerr.Error())
default:
fmt.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
}
return
}
fmt.Println(result)
}
// DescriveVault - Retrieve information about a vault
func DescriveVault(awsRegion, vaultName string) {
svc := glacier.New(session.New(), aws.NewConfig().WithRegion(awsRegion))
input := &glacier.DescribeVaultInput{
AccountId: aws.String("-"),
VaultName: aws.String(vaultName),
}
result, err := svc.DescribeVault(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case glacier.ErrCodeResourceNotFoundException:
fmt.Println(glacier.ErrCodeResourceNotFoundException, aerr.Error())
case glacier.ErrCodeInvalidParameterValueException:
fmt.Println(glacier.ErrCodeInvalidParameterValueException, aerr.Error())
case glacier.ErrCodeMissingParameterValueException:
fmt.Println(glacier.ErrCodeMissingParameterValueException, aerr.Error())
case glacier.ErrCodeServiceUnavailableException:
fmt.Println(glacier.ErrCodeServiceUnavailableException, aerr.Error())
default:
fmt.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
}
return
}
fmt.Println(result)
}
// UploadArchive - Upload archive to vault
// Reference - https://docs.aws.amazon.com/amazonglacier/latest/dev/api-archive-post.html
// More - https://docs.aws.amazon.com/amazonglacier/latest/dev/uploading-an-archive.html
func UploadArchive(awsRegion, vaultName, fileUpload string) {
svc := glacier.New(session.New(), aws.NewConfig().WithRegion(awsRegion))
input := &glacier.UploadArchiveInput{
AccountId: aws.String("-"),
ArchiveDescription: aws.String(getFilename(fileUpload)),
Body: aws.ReadSeekCloser(strings.NewReader(isFile(fileUpload))),
Checksum: aws.String(""),
VaultName: aws.String(vaultName),
}
result, err := svc.UploadArchive(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case glacier.ErrCodeResourceNotFoundException:
fmt.Println(glacier.ErrCodeResourceNotFoundException, aerr.Error())
case glacier.ErrCodeInvalidParameterValueException:
fmt.Println(glacier.ErrCodeInvalidParameterValueException, aerr.Error())
case glacier.ErrCodeMissingParameterValueException:
fmt.Println(glacier.ErrCodeMissingParameterValueException, aerr.Error())
case glacier.ErrCodeRequestTimeoutException:
fmt.Println(glacier.ErrCodeRequestTimeoutException, aerr.Error())
case glacier.ErrCodeServiceUnavailableException:
fmt.Println(glacier.ErrCodeServiceUnavailableException, aerr.Error())
default:
fmt.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
}
return
}
fmt.Println(result)
}
// DeleteVault - Delete vault based on name and region
func DeleteVault(awsRegion, vaultName string) {
svc := glacier.New(session.New(), aws.NewConfig().WithRegion(awsRegion))
input := &glacier.DeleteVaultInput{
AccountId: aws.String("-"),
VaultName: aws.String(vaultName),
}
result, err := svc.DeleteVault(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case glacier.ErrCodeResourceNotFoundException:
fmt.Println(glacier.ErrCodeResourceNotFoundException, aerr.Error())
case glacier.ErrCodeInvalidParameterValueException:
fmt.Println(glacier.ErrCodeInvalidParameterValueException, aerr.Error())
case glacier.ErrCodeMissingParameterValueException:
fmt.Println(glacier.ErrCodeMissingParameterValueException, aerr.Error())
case glacier.ErrCodeServiceUnavailableException:
fmt.Println(glacier.ErrCodeServiceUnavailableException, aerr.Error())
default:
fmt.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
}
return
}
fmt.Println(result)
}
// CreateVault - Create new vault based on name and region
func CreateVault(awsRegion, vaultName string) {
svc := glacier.New(session.New(), aws.NewConfig().WithRegion(awsRegion))
input := &glacier.CreateVaultInput{
AccountId: aws.String("-"),
VaultName: aws.String(vaultName),
//Limit: aws.String(""),
//Marker: aws.String(""),
}
result, err := svc.CreateVault(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case glacier.ErrCodeInvalidParameterValueException:
fmt.Println(glacier.ErrCodeInvalidParameterValueException, aerr.Error())
case glacier.ErrCodeMissingParameterValueException:
fmt.Println(glacier.ErrCodeMissingParameterValueException, aerr.Error())
case glacier.ErrCodeServiceUnavailableException:
fmt.Println(glacier.ErrCodeServiceUnavailableException, aerr.Error())
case glacier.ErrCodeLimitExceededException:
fmt.Println(glacier.ErrCodeLimitExceededException, aerr.Error())
default:
fmt.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
}
return
}
fmt.Println(result)
}
// ListVault - List all vaults based on region
func ListVault(awsRegion string) {
svc := glacier.New(session.New(), aws.NewConfig().WithRegion(awsRegion))
input := &glacier.ListVaultsInput{
AccountId: aws.String("-"),
//Limit: aws.String(""),
//Marker: aws.String(""),
}
result, err := svc.ListVaults(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case glacier.ErrCodeResourceNotFoundException:
fmt.Println(glacier.ErrCodeResourceNotFoundException, aerr.Error())
case glacier.ErrCodeInvalidParameterValueException:
fmt.Println(glacier.ErrCodeInvalidParameterValueException, aerr.Error())
case glacier.ErrCodeMissingParameterValueException:
fmt.Println(glacier.ErrCodeMissingParameterValueException, aerr.Error())
case glacier.ErrCodeServiceUnavailableException:
fmt.Println(glacier.ErrCodeServiceUnavailableException, aerr.Error())
default:
fmt.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
}
return
}
fmt.Println(result)
}
// GetRetrievalPolicy - Get the current data retrieval policy for an account
func GetRetrievalPolicy(awsRegion string) {
svc := glacier.New(session.New(), aws.NewConfig().WithRegion(awsRegion))
input := &glacier.GetDataRetrievalPolicyInput{
AccountId: aws.String("-"),
}
result, err := svc.GetDataRetrievalPolicy(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case glacier.ErrCodeInvalidParameterValueException:
fmt.Println(glacier.ErrCodeInvalidParameterValueException, aerr.Error())
case glacier.ErrCodeMissingParameterValueException:
fmt.Println(glacier.ErrCodeMissingParameterValueException, aerr.Error())
case glacier.ErrCodeServiceUnavailableException:
fmt.Println(glacier.ErrCodeServiceUnavailableException, aerr.Error())
default:
fmt.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
}
return
}
fmt.Println(result)
}
// TODO SetDataRetrievalPolicyFreeTier and SetDataRetrievalPolicy needs testing
// SetDataRetrievalPolicyFreeTier - Set FreeTier retrieval policy
func SetDataRetrievalPolicyFreeTier(awsRegion string) {
svc := glacier.New(session.New(), aws.NewConfig().WithRegion(awsRegion))
input := &glacier.SetDataRetrievalPolicyInput{
Policy: &glacier.DataRetrievalPolicy{
Rules: []*glacier.DataRetrievalRule{
{
Strategy: aws.String("FreeTier"),
},
},
},
AccountId: aws.String("-"),
}
result, err := svc.SetDataRetrievalPolicy(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case glacier.ErrCodeInvalidParameterValueException:
fmt.Println(glacier.ErrCodeInvalidParameterValueException, aerr.Error())
case glacier.ErrCodeMissingParameterValueException:
fmt.Println(glacier.ErrCodeMissingParameterValueException, aerr.Error())
case glacier.ErrCodeServiceUnavailableException:
fmt.Println(glacier.ErrCodeServiceUnavailableException, aerr.Error())
default:
fmt.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
}
return
}
fmt.Println(result)
}
// SetDataRetrievalPolicy - Set and then enact a data retrieval policy
func SetDataRetrievalPolicy(awsRegion, strategyPolicy string, bytesPerHour int64) {
svc := glacier.New(session.New(), aws.NewConfig().WithRegion(awsRegion))
input := &glacier.SetDataRetrievalPolicyInput{
Policy: &glacier.DataRetrievalPolicy{
Rules: []*glacier.DataRetrievalRule{
{
BytesPerHour: aws.Int64(bytesPerHour),
Strategy: aws.String(strategyPolicy),
},
},
},
AccountId: aws.String("-"),
}
result, err := svc.SetDataRetrievalPolicy(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case glacier.ErrCodeInvalidParameterValueException:
fmt.Println(glacier.ErrCodeInvalidParameterValueException, aerr.Error())
case glacier.ErrCodeMissingParameterValueException:
fmt.Println(glacier.ErrCodeMissingParameterValueException, aerr.Error())
case glacier.ErrCodeServiceUnavailableException:
fmt.Println(glacier.ErrCodeServiceUnavailableException, aerr.Error())
default:
fmt.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
}
return
}
fmt.Println(result)
}
// InitiateVaultLock - Installing a vault lock policy on the specified vault.
// Setting the lock state of vault lock to InProgress.
// Returning a lock ID, which is used to complete the vault locking process.
func InitiateVaultLock(awsRegion, vaultName, vaultPolicy string) {
svc := glacier.New(session.New(), aws.NewConfig().WithRegion(awsRegion))
input := &glacier.InitiateVaultLockInput{
AccountId: aws.String("-"),
Policy: &glacier.VaultLockPolicy{
Policy: aws.String(vaultPolicy),
//Policy: aws.String("{\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"Define-vault-lock\",\"Effect\":\"Deny\",\"Principal\":{\"AWS\":\"*\"},\"Action\":\"glacier:DeleteArchive\",\"Resource\":\"arn:aws:glacier:us-east-2:757758175257:vaults/my-vault\",\"Condition\":{\"NumericLessThanEquals\":{\"glacier:ArchiveAgeinDays\":\"365\"}}}]}"),
},
VaultName: aws.String(vaultName),
}
result, err := svc.InitiateVaultLock(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case glacier.ErrCodeResourceNotFoundException:
fmt.Println(glacier.ErrCodeResourceNotFoundException, aerr.Error())
case glacier.ErrCodeInvalidParameterValueException:
fmt.Println(glacier.ErrCodeInvalidParameterValueException, aerr.Error())
case glacier.ErrCodeMissingParameterValueException:
fmt.Println(glacier.ErrCodeMissingParameterValueException, aerr.Error())
case glacier.ErrCodeServiceUnavailableException:
fmt.Println(glacier.ErrCodeServiceUnavailableException, aerr.Error())
default:
fmt.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
}
return
}
fmt.Println(result)
}
// AbortVaultLock - This operation aborts the vault locking process if the vault lock is not in the Locked state.
// If the vault lock is in the Locked state when this operation is requested, the operation returns an AccessDeniedException error.
// Aborting the vault locking process removes the vault lock policy from the specified vault.
func AbortVaultLock(awsRegion, vaultName string) {
svc := glacier.New(session.New(), aws.NewConfig().WithRegion(awsRegion))
input := &glacier.AbortVaultLockInput{
AccountId: aws.String("-"),
VaultName: aws.String(vaultName),
}
_, err := svc.AbortVaultLock(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case glacier.ErrCodeResourceNotFoundException:
fmt.Println(glacier.ErrCodeResourceNotFoundException, aerr.Error())
case glacier.ErrCodeInvalidParameterValueException:
fmt.Println(glacier.ErrCodeInvalidParameterValueException, aerr.Error())
case glacier.ErrCodeMissingParameterValueException:
fmt.Println(glacier.ErrCodeMissingParameterValueException, aerr.Error())
case glacier.ErrCodeServiceUnavailableException:
fmt.Println(glacier.ErrCodeServiceUnavailableException, aerr.Error())
default:
fmt.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
}
return
}
fmt.Printf("Vault lock aborted on %s \n", vaultName)
}
// CompleteVaultLock - This operation completes the vault locking process by transitioning the vault lock
// from the InProgress state to the Locked state, which causes the vault lock policy to become unchangeable.
func CompleteVaultLock(awsRegion, vaultName, lockID string) {
svc := glacier.New(session.New(), aws.NewConfig().WithRegion(awsRegion))
input := &glacier.CompleteVaultLockInput{
AccountId: aws.String("-"),
LockId: aws.String(lockID),
VaultName: aws.String(vaultName),
}
result, err := svc.CompleteVaultLock(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case glacier.ErrCodeResourceNotFoundException:
fmt.Println(glacier.ErrCodeResourceNotFoundException, aerr.Error())
case glacier.ErrCodeInvalidParameterValueException:
fmt.Println(glacier.ErrCodeInvalidParameterValueException, aerr.Error())
case glacier.ErrCodeMissingParameterValueException:
fmt.Println(glacier.ErrCodeMissingParameterValueException, aerr.Error())
case glacier.ErrCodeServiceUnavailableException:
fmt.Println(glacier.ErrCodeServiceUnavailableException, aerr.Error())
default:
fmt.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
}
return
}
fmt.Println(result)
}