/
vultr_store.go
91 lines (66 loc) · 2.03 KB
/
vultr_store.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
//go:build vultr || !onlyprovider
package vultr
import (
"net/url"
"os"
"time"
"github.com/minio/minio-go"
"github.com/nanovms/ops/log"
"github.com/nanovms/ops/types"
)
// Objects provides Vultr Object Storage related operations
type Objects struct{}
func (s *Objects) getSignedURL(key string, bucket string, region string) string {
accessKey := os.Getenv("VULTR_ACCESS")
secKey := os.Getenv("VULTR_SECRET")
if accessKey == "" || secKey == "" {
log.Fatalf("danger will robinson - can not find VULTR_ACCESS || VULTR_SECRET env vars")
}
endpoint := region + ".vultrobjects.com"
ssl := true
client, err := minio.New(endpoint, accessKey, secKey, ssl)
if err != nil {
log.Fatal(err)
}
reqParams := make(url.Values)
reqParams.Set("response-content-disposition", "attachment; filename=\""+key+"\"")
presignedURL, err := client.PresignedGetObject(bucket, key, time.Second*5*60, reqParams)
if err != nil {
log.Error(err)
return ""
}
return presignedURL.String()
}
// CopyToBucket copies archive to bucket
func (s *Objects) CopyToBucket(config *types.Config, archPath string) error {
bucket := config.CloudConfig.BucketName
zone := config.CloudConfig.Zone
file, err := os.Open(archPath)
if err != nil {
return err
}
defer file.Close()
accessKey := os.Getenv("VULTR_ACCESS")
secKey := os.Getenv("VULTR_SECRET")
endpoint := zone + ".vultrobjects.com"
ssl := true
client, err := minio.New(endpoint, accessKey, secKey, ssl)
if err != nil {
log.Fatal(err)
}
stat, err := file.Stat()
if err != nil {
log.Fatal(err)
}
n, err := client.PutObject(bucket, config.CloudConfig.ImageName, file, stat.Size(), minio.PutObjectOptions{ContentType: "application/octet-stream"})
if err != nil {
log.Fatal(err)
}
log.Info("Uploaded", "my-objectname", " of size: ", n, "Successfully.")
log.Infof("Successfully uploaded %q to %q\n", config.CloudConfig.ImageName, bucket)
return nil
}
// DeleteFromBucket deletes key from config's bucket
func (s *Objects) DeleteFromBucket(config *types.Config, key string) error {
return nil
}