forked from awsdocs/aws-doc-sdk-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UploadArchive.go
98 lines (82 loc) · 3.03 KB
/
UploadArchive.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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0
// snippet-start:[glacier.go.upload_archive]
package main
// snippet-start:[glacier.go.upload_archive.imports]
import (
"bytes"
"flag"
"fmt"
"os"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/glacier"
"github.com/aws/aws-sdk-go/service/glacier/glacieriface"
)
// snippet-end:[glacier.go.upload_archive.imports]
// ArchiveFile archives a file in an Amazon Simple Storage Service Glacier (Amazon S3 Glacier) vault.
// Inputs:
// svc is an Amazon S3 Glacier service client
// vaultName is the name of the vault
// fileName is the name of the file to upload
// Output:
// If success, information about the archive and nil
// Otherwise, nil and an error from the call to Open or UploadArchive
func ArchiveFile(svc glacieriface.GlacierAPI, vaultName, fileName *string) (*glacier.ArchiveCreationOutput, error) {
// snippet-start:[glacier.go.upload_archive.open]
file, err := os.Open(*fileName)
// snippet-end:[glacier.go.upload_archive.open]
if err != nil {
return nil, err
}
// snippet-start:[glacier.go.upload_archive.read]
defer file.Close()
fileInfo, _ := file.Stat()
var size int64 = fileInfo.Size()
buffer := make([]byte, size)
// read file content to buffer
_, err = file.Read(buffer)
// snippet-end:[glacier.go.upload_archive.read]
if err != nil {
return nil, err
}
// snippet-start:[glacier.go.upload_archive.call]
fileBytes := bytes.NewReader(buffer)
result, err := svc.UploadArchive(&glacier.UploadArchiveInput{
AccountId: aws.String("-"), // use current account
VaultName: vaultName,
Body: fileBytes,
})
// snippet-end:[glacier.go.upload_archive.call]
if err != nil {
return nil, err
}
return result, nil
}
func main() {
// snippet-start:[glacier.go.upload_archive.args]
vaultName := flag.String("v", "", "The name of the vault")
fileName := flag.String("f", "", "The name of the file to archive")
flag.Parse()
if *vaultName == "" || *fileName == "" {
fmt.Println("You must supply a vault name and file name")
fmt.Println("-v VAULT-NAME -f FILE-NAME")
return
}
// snippet-end:[glacier.go.upload_archive.args]
// snippet-start:[glacier.go.upload_archive.session]
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
svc := glacier.New(sess)
// snippet-end:[glacier.go.upload_archive.session]
result, err := ArchiveFile(svc, vaultName, fileName)
if err != nil {
fmt.Println("Got an error archiving " + *fileName)
return
}
// snippet-start:[glacier.go.upload_archive.display]
fmt.Println("Uploaded to archive with ID " + *result.ArchiveId)
// snippet-end:[glacier.go.upload_archive.display]
}
// snippet-end:[glacier.go.upload_archive]