/
main.go
134 lines (113 loc) · 2.9 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"bytes"
"fmt"
"github.com/Azure/azure-storage-blob-go/azblob"
"github.com/gin-gonic/gin"
"io"
"log"
"net/url"
)
const (
accountName = "ストレージアカウント名"
accountKey = "ストレージキー"
containerName = "コンテナ名"
)
type blobItem struct {
Name string
Size int64
}
var containerURL azblob.ContainerURL
func main() {
credential, err := azblob.NewSharedKeyCredential(accountName, accountKey)
if err != nil {
log.Fatal(err)
}
URL, _ := url.Parse(fmt.Sprintf("https://%s.blob.core.windows.net/%s", accountName, containerName))
containerURL = azblob.NewContainerURL(*URL, azblob.NewPipeline(credential, azblob.PipelineOptions{}))
router := gin.Default()
router.Use(errorMiddleware())
router.LoadHTMLGlob("templates/*.html")
router.GET("/", home)
router.POST("/upload", upload)
router.POST("/delete", delete)
router.Run()
}
func home(ctx *gin.Context) {
var items []blobItem
for marker := (azblob.Marker{}); marker.NotDone(); {
listBlob, err := containerURL.ListBlobsFlatSegment(ctx, marker, azblob.ListBlobsSegmentOptions{})
if err != nil {
ctx.Error(err).SetType(gin.ErrorTypePublic)
return
}
marker = listBlob.NextMarker
for _, blobInfo := range listBlob.Segment.BlobItems {
items = append(items, blobItem{
Name: blobInfo.Name,
Size: *blobInfo.Properties.ContentLength,
})
}
}
ctx.HTML(200, "index.html", gin.H{
"items": items,
})
}
func upload(ctx *gin.Context) {
form, err := ctx.MultipartForm()
if err != nil {
ctx.Error(err).SetType(gin.ErrorTypePublic)
return
}
formFiles := form.File["file1"]
if formFiles == nil {
ctx.Redirect(302, "/")
return
}
formFile := formFiles[0]
fileName := formFile.Filename
blobURL := containerURL.NewBlockBlobURL(fileName)
file, err := formFile.Open()
defer file.Close()
if err != nil {
ctx.Error(err).SetType(gin.ErrorTypePublic)
return
}
buf := bytes.NewBuffer(nil)
if _, err := io.Copy(buf, file); err != nil {
ctx.Error(err).SetType(gin.ErrorTypePublic)
return
}
// BLOBにファイルをアップロード
_, err = azblob.UploadBufferToBlockBlob(ctx, buf.Bytes(), blobURL, azblob.UploadToBlockBlobOptions{
BlockSize: 4 * 1024 * 1024,
Parallelism: 16})
if err != nil {
ctx.Error(err).SetType(gin.ErrorTypePublic)
return
}
ctx.Redirect(302, "/")
}
func delete(ctx *gin.Context) {
name := ctx.PostForm("name")
blobURL := containerURL.NewBlobURL(name)
// Delete the blob we created earlier.
_, err := blobURL.Delete(ctx, azblob.DeleteSnapshotsOptionNone, azblob.BlobAccessConditions{})
if err != nil {
ctx.Error(err).SetType(gin.ErrorTypePublic)
return
}
ctx.Redirect(302, "/")
}
func errorMiddleware() gin.HandlerFunc {
return func(ctx *gin.Context) {
ctx.Next()
err := ctx.Errors.ByType(gin.ErrorTypePublic).Last()
if err != nil {
log.Print(err.Err)
ctx.HTML(500, "error.html", gin.H{
"error": err.Error(),
})
}
}
}