This repository has been archived by the owner on Jun 21, 2022. It is now read-only.
/
s3.go
69 lines (65 loc) · 2.29 KB
/
s3.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
package remotes
// StoreInS3 handles storing ReShifter archive in S3 compatible storage.
import (
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/mhausenblas/reshifter/pkg/types"
"github.com/mhausenblas/reshifter/pkg/util"
minio "github.com/minio/minio-go"
)
// StoreInS3 stores backup with backupid (in directory target)
// in bucket in an S3 compatible storage, using s3endpoint.
func StoreInS3(s3endpoint, bucket, target, backupid string) error {
target += ".zip"
accesskey, secret, err := util.S3CredFromEnv()
if err != nil {
return fmt.Errorf("No S3 credentials found: %s", err)
}
object := backupid + ".zip"
mc, err := minio.New(s3endpoint, accesskey, secret, true)
if err != nil {
return fmt.Errorf(fmt.Sprintf("%s ", err))
}
// Note: we don't care about the error, that is,
// if the bucket already exists, we ignore that fact.
// Also, the region doesn't matter, it's a global resource.
_ = mc.MakeBucket(bucket, "us-east-1")
nbytes, err := mc.FPutObject(bucket, object, target, types.ContentTypeZip)
if err != nil {
return fmt.Errorf(fmt.Sprintf("%s", err))
}
log.WithFields(log.Fields{"func": "remotes.StoreInS3"}).Debug(fmt.Sprintf("Successfully stored %s/%s (%d Bytes) in S3 compatible remote storage %s", bucket, object, nbytes, s3endpoint))
return nil
}
// ListObjectsInS3Bucket lists all backup IDs from a
// bucket in an S3 compatible storage, using s3endpoint.
func ListObjectsInS3Bucket(s3endpoint, bucket string) ([]string, error) {
if s3endpoint == "" || bucket == "" {
return nil, fmt.Errorf("No remote or bucket provided. Aborting …")
}
accesskey, secret, err := util.S3CredFromEnv()
if err != nil {
return nil, fmt.Errorf("No S3 credentials found: %s", err)
}
mc, err := minio.New(s3endpoint, accesskey, secret, true)
if err != nil {
return nil, fmt.Errorf(fmt.Sprintf("%s ", err))
}
exists, err := mc.BucketExists(bucket)
if err != nil {
return nil, fmt.Errorf(fmt.Sprintf("%s ", err))
}
if !exists {
return nil, fmt.Errorf(fmt.Sprintf("Bucket %s does not exist. Aborting …", bucket))
}
done := make(chan struct{})
defer close(done)
var backupIDs []string
recursive := false
for msg := range mc.ListObjects(bucket, "", recursive, done) {
fn := msg.Key
bid := fn[0 : len(fn)-len(".zip")]
backupIDs = append(backupIDs, bid)
}
return backupIDs, nil
}