-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUpload2Swift.go
56 lines (44 loc) · 1.52 KB
/
Upload2Swift.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
package main
import (
"fmt"
"github.com/ncw/swift"
"os"
"path/filepath"
"strings"
)
// if input file starts with swift:// copy it to the container/folder/object address
// otherwise it will upload it to swift in address container/folder/filename address
func Upload2Swift(address string, container string, folder string, conn swift.Connection) {
address = strings.TrimSpace(address)
if strings.HasPrefix(strings.ToLower(address), "swift://") { // if address starts with swift://
inSwiftAddress :=address[8:] // remove "swift://" from beginning of the address
parts := strings.Split(inSwiftAddress, "/")
srcContainer := parts[0]
srcObject:=parts[len(parts)-1]
srcFolderObject := strings.TrimLeft(inSwiftAddress, srcContainer+"/")
conn.ObjectCopy(srcContainer,srcFolderObject,container,folder+"/"+srcObject, nil)
fmt.Println("\nObject "+srcObject+" was copied from swift://"+srcContainer+"/"+srcFolderObject+" to swift://"+
container+"/"+folder+"/"+srcObject)
} else {
filename := filepath.Base(address)
err := conn.ContainerCreate(container, nil)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
content, err := os.Open(address)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer content.Close()
fmt.Println("Uploading input file to swift: ", address)
_, err = conn.ObjectPut(container, folder+"/"+filename, content, false, "", "application/zip", nil)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("\nInput file uploaded to swift storage.", address)
}
wg.Done()
}