-
Notifications
You must be signed in to change notification settings - Fork 1
/
wpauswiftcommons.go
44 lines (39 loc) · 995 Bytes
/
wpauswiftcommons.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
package wpauswiftcommons
import (
"context"
"crypto/md5"
"encoding/hex"
"fmt"
"os"
"path/filepath"
swift "github.com/ncw/swift/v2"
)
func CreatePublicContainer(ctx context.Context, name string, c swift.Connection) {
headers := map[string]string{
"X-Container-Read": ".r:*",
}
c.ContainerCreate(ctx, name, headers)
}
func UploadFile(ctx context.Context, container string, prefix string, path string, c swift.Connection) {
dat, err := os.ReadFile(path)
if err != nil {
println(err.Error())
} else {
name := filepath.Base(path)
if len(prefix) > 0 {
name = prefix + "-" + name
}
ext := filepath.Ext(path)
hasher := md5.New()
hasher.Write(dat)
md5hash := hex.EncodeToString(hasher.Sum(nil))
fmt.Printf("Uploading %s to container %s\n", name, container)
file, err := c.ObjectCreate(ctx, container, name, false, md5hash, ext, nil)
if err != nil {
fmt.Printf("Error uploading file %s. %s", name, err)
} else {
file.Write(dat)
}
file.Close()
}
}