forked from imgproxy/imgproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathazure_transport.go
49 lines (38 loc) · 1.18 KB
/
azure_transport.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
package main
import (
"context"
"fmt"
"net/http"
"net/url"
"strings"
"github.com/Azure/azure-storage-blob-go/azblob"
)
type azureTransport struct {
serviceURL *azblob.ServiceURL
}
func newAzureTransport() (http.RoundTripper, error) {
credential, err := azblob.NewSharedKeyCredential(conf.ABSName, conf.ABSKey)
if err != nil {
return nil, err
}
pipeline := azblob.NewPipeline(credential, azblob.PipelineOptions{})
endpoint := conf.ABSEndpoint
if len(endpoint) == 0 {
endpoint = fmt.Sprintf("https://%s.blob.core.windows.net", conf.ABSName)
}
endpointURL, err := url.Parse(endpoint)
if err != nil {
return nil, err
}
serviceURL := azblob.NewServiceURL(*endpointURL, pipeline)
return azureTransport{&serviceURL}, nil
}
func (t azureTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
containerURL := t.serviceURL.NewContainerURL(strings.ToLower(req.URL.Host))
blobURL := containerURL.NewBlockBlobURL(strings.TrimPrefix(req.URL.Path, "/"))
get, err := blobURL.Download(context.Background(), 0, azblob.CountToEnd, azblob.BlobAccessConditions{}, false, azblob.ClientProvidedKeyOptions{})
if err != nil {
return nil, err
}
return get.Response(), nil
}