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 recorderstorage
import (
"context"
"fmt"
"net/url"
"os"
"github.com/Azure/azure-storage-blob-go/azblob"
"github.com/jumpserver/koko/pkg/logger"
)
type AzureReplayStorage struct {
AccountName string
AccountKey string
ContainerName string
EndpointSuffix string
}
func (a *AzureReplayStorage) Upload(gZipFilePath, target string) (err error) {
file, err := os.Open(gZipFilePath)
if err != nil {
return
}
credential, err := azblob.NewSharedKeyCredential(a.AccountName, a.AccountKey)
if err != nil {
logger.Error("Invalid credentials with error: " + err.Error())
}
p := azblob.NewPipeline(credential, azblob.PipelineOptions{})
endpoint := fmt.Sprintf("https://%s.blob.%s/%s", a.AccountName, a.EndpointSuffix, a.ContainerName)
URL, _ := url.Parse(endpoint)
containerURL := azblob.NewContainerURL(*URL, p)
blobURL := containerURL.NewBlockBlobURL(target)
_, err = azblob.UploadFileToBlockBlob(context.TODO(), file, blobURL, azblob.UploadToBlockBlobOptions{
BlockSize: 4 * 1024 * 1024,
Parallelism: 16})
if err != nil {
logger.Error(err.Error())
}
return
}