|
1 | 1 | package backup
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "context" |
| 5 | + "crypto/tls" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "net/url" |
| 9 | + "os" |
4 | 10 | "time"
|
5 | 11 |
|
| 12 | + "github.com/minio/minio-go/v7" |
| 13 | + "github.com/minio/minio-go/v7/pkg/credentials" |
| 14 | + |
6 | 15 | "github.com/lxc/incus/v6/internal/server/state"
|
| 16 | + "github.com/lxc/incus/v6/shared/api" |
7 | 17 | )
|
8 | 18 |
|
9 | 19 | // WorkingDirPrefix is used when temporary working directories are needed.
|
@@ -40,3 +50,53 @@ func (b *CommonBackup) SetCompressionAlgorithm(compression string) {
|
40 | 50 | func (b *CommonBackup) OptimizedStorage() bool {
|
41 | 51 | return b.optimizedStorage
|
42 | 52 | }
|
| 53 | + |
| 54 | +// upload handles backup uploads. |
| 55 | +func (b *CommonBackup) upload(filePath string, req *api.BackupTarget) error { |
| 56 | + if req.Protocol != "s3" { |
| 57 | + return fmt.Errorf("Unsupported backup target protocol %q", req.Protocol) |
| 58 | + } |
| 59 | + |
| 60 | + // Set up an S3 client. |
| 61 | + uri, err := url.Parse(req.URL) |
| 62 | + if err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + |
| 66 | + creds := credentials.NewStaticV4(req.AccessKey, req.SecretKey, "") |
| 67 | + |
| 68 | + ts := &http.Transport{ |
| 69 | + MaxIdleConns: 10, |
| 70 | + IdleConnTimeout: 30 * time.Second, |
| 71 | + DisableCompression: true, |
| 72 | + TLSClientConfig: &tls.Config{ |
| 73 | + InsecureSkipVerify: true, |
| 74 | + MinVersion: tls.VersionTLS12, |
| 75 | + }, |
| 76 | + } |
| 77 | + |
| 78 | + client, err := minio.New(uri.Host, &minio.Options{ |
| 79 | + BucketLookup: minio.BucketLookupPath, |
| 80 | + Creds: creds, |
| 81 | + Secure: uri.Scheme == "https", |
| 82 | + Transport: ts, |
| 83 | + }) |
| 84 | + if err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + |
| 88 | + // Upload the object. |
| 89 | + tr, err := os.Open(filePath) |
| 90 | + if err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + |
| 94 | + defer tr.Close() |
| 95 | + |
| 96 | + _, err = client.PutObject(context.Background(), req.BucketName, req.Path, tr, -1, minio.PutObjectOptions{}) |
| 97 | + if err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + |
| 101 | + return nil |
| 102 | +} |
0 commit comments