-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
117 lines (95 loc) · 2.64 KB
/
main.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"context"
"fmt"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"strings"
"time"
"github.com/Azure/azure-storage-blob-go/azblob"
"github.com/gofrs/uuid"
)
func main() {
files, errW := walkDir(".")
if errW != nil {
fmt.Println("Error has occured:", errW)
} else {
var fFiles []string
for _, fName := range files {
if strings.Contains(fName, "jpg") {
fFiles = append(fFiles, fName)
}
}
m := make(map[string][]byte)
// Read file contents into memory
for _, fName := range fFiles {
fmt.Println("Found file:", fName)
dat, errR := ReadFile(fName)
if errR != nil {
fmt.Println("Error reading file:", fName, "Error:", errR)
} else {
fmt.Println("Finished reading bytes for file:", fName)
m[fName] = dat
}
}
// push file contents from memory to Azure
for _, fName := range fFiles {
fmt.Println("Started uploading: ", fName)
u, errU := UploadBytesToBlob(m[fName])
if errU != nil {
fmt.Println("Error during upload: ", errU)
}
fmt.Println("Finished uploading to: ", u)
fmt.Println("==========================================================")
}
}
}
func walkDir(root string) ([]string, error) {
var files []string
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
files = append(files, path)
}
return nil
})
return files, err
}
func ReadFile(filePath string) ([]byte, error) {
dat, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, err
} else {
return dat, nil
}
}
func UploadBytesToBlob(b []byte) (string, error) {
azrKey, accountName, endPoint, container := GetAccountInfo()
u, _ := url.Parse(fmt.Sprint(endPoint, container, "/", GetBlobName()))
credential, errC := azblob.NewSharedKeyCredential(accountName, azrKey)
if errC != nil {
return "", errC
}
blockBlobUrl := azblob.NewBlockBlobURL(*u, azblob.NewPipeline(credential, azblob.PipelineOptions{}))
ctx := context.Background()
o := azblob.UploadToBlockBlobOptions{
BlobHTTPHeaders: azblob.BlobHTTPHeaders{
ContentType: "image/jpg",
},
}
_, errU := azblob.UploadBufferToBlockBlob(ctx, b, blockBlobUrl, o)
return blockBlobUrl.String(), errU
}
func GetAccountInfo() (string, string, string, string) {
azrKey := "your_azure_access_key"
azrBlobAccountName := "mytechblog"
azrPrimaryBlobServiceEndpoint := fmt.Sprintf("https://%s.blob.core.windows.net/", azrBlobAccountName)
azrBlobContainer := "blog-photos"
return azrKey, azrBlobAccountName, azrPrimaryBlobServiceEndpoint, azrBlobContainer
}
func GetBlobName() string {
t := time.Now()
uuid, _ := uuid.NewV4()
return fmt.Sprintf("%s-%v.jpg", t.Format("20060102"), uuid)
}