-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvideostorage.go
79 lines (71 loc) · 1.99 KB
/
videostorage.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
package main
import (
"bytes"
"context"
"fmt"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
"github.com/gin-gonic/gin"
"log"
"mime/multipart"
"os"
)
var (
connectionString = os.Getenv("AZURE_STORAGE_CONNECTION_STRING")
serviceClient *azblob.Client
containerName = "videos"
)
func storageSetup() {
var err error
fmt.Println(connectionString)
serviceClient, err = azblob.NewClientFromConnectionString(connectionString, nil)
if err != nil {
log.Fatalf("Failed to create Storage Client: %v", err)
}
pager := serviceClient.NewListBlobsFlatPager(containerName, &azblob.ListBlobsFlatOptions{
Include: azblob.ListBlobsInclude{Snapshots: true, Versions: true},
})
fmt.Println("List blobs flat:")
for pager.More() {
resp, err := pager.NextPage(context.TODO())
if err != nil {
log.Fatalf("Failed to list blobs: %v", err)
}
for _, blob := range resp.Segment.BlobItems {
fmt.Println(*blob.Name)
}
}
}
func uploadVideo(filename string, video multipart.File) error {
_, err := serviceClient.UploadStream(context.Background(), containerName, filename, video, nil)
return err
}
func deleteVideo(filename string) error {
_, err := serviceClient.DeleteBlob(context.Background(), containerName, filename, nil)
return err
}
func getVideoRoutes() {
r.GET("/video/:filename", authMiddleware(), func(c *gin.Context) {
filename := c.Param("filename")
response, err := serviceClient.DownloadStream(context.Background(), containerName, filename, nil)
if err != nil {
fmt.Println(err)
c.String(500, "Failed to download video: %v", err)
return
}
video := bytes.Buffer{}
retryReader := response.NewRetryReader(context.Background(), &azblob.RetryReaderOptions{})
_, err = video.ReadFrom(retryReader)
if err != nil {
fmt.Println(err)
c.String(500, "Failed to read video: %v", err)
return
}
err = retryReader.Close()
if err != nil {
fmt.Println(err)
c.String(500, "Failed to close video: %v", err)
return
}
c.Data(200, "video/mp4", video.Bytes())
})
}