forked from gomods/athens
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blob_store_client.go
53 lines (46 loc) · 1.85 KB
/
blob_store_client.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
package azurecdn
import (
"context"
"io"
"net/url"
"github.com/opentracing/opentracing-go"
"github.com/Azure/azure-storage-blob-go/2017-07-29/azblob"
)
type azureBlobStoreClient struct {
containerURL *azblob.ContainerURL
}
func newBlobStoreClient(accountURL *url.URL, accountName, accountKey, containerName string) (*azureBlobStoreClient, error) {
cred := azblob.NewSharedKeyCredential(accountName, accountKey)
pipe := azblob.NewPipeline(cred, azblob.PipelineOptions{})
serviceURL := azblob.NewServiceURL(*accountURL, pipe)
// rules on container names:
// https://docs.microsoft.com/en-us/rest/api/storageservices/naming-and-referencing-containers--blobs--and-metadata#container-names
//
// This container must exist
containerURL := serviceURL.NewContainerURL(containerName)
cl := &azureBlobStoreClient{containerURL: &containerURL}
return cl, nil
}
func (c *azureBlobStoreClient) UploadWithContext(ctx context.Context, path, contentType string, content io.Reader) error {
sp, ctx := opentracing.StartSpanFromContext(ctx, "storage.azurecdn.UploadWithContext")
defer sp.Finish()
blobURL := c.containerURL.NewBlockBlobURL(path)
emptyMeta := map[string]string{}
emptyBlobAccessCond := azblob.BlobAccessConditions{}
httpHeaders := func(contentType string) azblob.BlobHTTPHeaders {
return azblob.BlobHTTPHeaders{
ContentType: contentType,
}
}
bufferSize := 1 * 1024 * 1024 // Size of the rotating buffers that are used when uploading
maxBuffers := 3 // Number of rotating buffers that are used when uploading
uploadStreamOpts := azblob.UploadStreamToBlockBlobOptions{
BufferSize: bufferSize,
MaxBuffers: maxBuffers,
BlobHTTPHeaders: httpHeaders(contentType),
Metadata: emptyMeta,
AccessConditions: emptyBlobAccessCond,
}
_, err := azblob.UploadStreamToBlockBlob(ctx, content, blobURL, uploadStreamOpts)
return err
}