azure_transport.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "net/url"
  7. "strings"
  8. "github.com/Azure/azure-storage-blob-go/azblob"
  9. )
  10. type azureTransport struct {
  11. serviceURL *azblob.ServiceURL
  12. }
  13. func newAzureTransport() (http.RoundTripper, error) {
  14. credential, err := azblob.NewSharedKeyCredential(conf.ABSName, conf.ABSKey)
  15. if err != nil {
  16. return nil, err
  17. }
  18. pipeline := azblob.NewPipeline(credential, azblob.PipelineOptions{})
  19. endpoint := conf.ABSEndpoint
  20. if len(endpoint) == 0 {
  21. endpoint = fmt.Sprintf("https://%s.blob.core.windows.net", conf.ABSName)
  22. }
  23. endpointURL, err := url.Parse(endpoint)
  24. if err != nil {
  25. return nil, err
  26. }
  27. serviceURL := azblob.NewServiceURL(*endpointURL, pipeline)
  28. return azureTransport{&serviceURL}, nil
  29. }
  30. func (t azureTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
  31. containerURL := t.serviceURL.NewContainerURL(strings.ToLower(req.URL.Host))
  32. blobURL := containerURL.NewBlockBlobURL(strings.TrimPrefix(req.URL.Path, "/"))
  33. get, err := blobURL.Download(context.Background(), 0, azblob.CountToEnd, azblob.BlobAccessConditions{}, false, azblob.ClientProvidedKeyOptions{})
  34. if err != nil {
  35. return nil, err
  36. }
  37. return get.Response(), nil
  38. }