|
| 1 | +package blobstorage |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "net/url" |
| 8 | + |
| 9 | + "github.com/google/uuid" |
| 10 | + |
| 11 | + log "github.com/Sirupsen/logrus" |
| 12 | + |
| 13 | + "github.com/Azure/azure-storage-blob-go/azblob" |
| 14 | + "github.com/actionscore/components-contrib/bindings" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + blobName = "blobName" |
| 19 | +) |
| 20 | + |
| 21 | +// AzureBlobStorage allows saving blobs to an Azure Blob Storage account |
| 22 | +type AzureBlobStorage struct { |
| 23 | + metadata *blobStorageMetadata |
| 24 | + containerURL azblob.ContainerURL |
| 25 | +} |
| 26 | + |
| 27 | +type blobStorageMetadata struct { |
| 28 | + StorageAccount string `json:"storageAccount"` |
| 29 | + StorageAccessKey string `json:"storageAccessKey"` |
| 30 | + Container string `json:"container"` |
| 31 | +} |
| 32 | + |
| 33 | +// NewAzureBlobStorage returns a new CosmosDB instance |
| 34 | +func NewAzureBlobStorage() *AzureBlobStorage { |
| 35 | + return &AzureBlobStorage{} |
| 36 | +} |
| 37 | + |
| 38 | +// Init performs metadata parsing |
| 39 | +func (a *AzureBlobStorage) Init(metadata bindings.Metadata) error { |
| 40 | + m, err := a.parseMetadata(metadata) |
| 41 | + if err != nil { |
| 42 | + return err |
| 43 | + } |
| 44 | + a.metadata = m |
| 45 | + credential, err := azblob.NewSharedKeyCredential(m.StorageAccount, m.StorageAccessKey) |
| 46 | + if err != nil { |
| 47 | + return fmt.Errorf("Invalid credentials with error: %s", err.Error()) |
| 48 | + } |
| 49 | + p := azblob.NewPipeline(credential, azblob.PipelineOptions{}) |
| 50 | + |
| 51 | + containerName := a.metadata.Container |
| 52 | + URL, _ := url.Parse( |
| 53 | + fmt.Sprintf("https://%s.blob.core.windows.net/%s", m.StorageAccount, containerName)) |
| 54 | + containerURL := azblob.NewContainerURL(*URL, p) |
| 55 | + |
| 56 | + ctx := context.Background() |
| 57 | + _, err = containerURL.Create(ctx, azblob.Metadata{}, azblob.PublicAccessNone) |
| 58 | + // Don't return error, container might already exist |
| 59 | + log.Debugf("error creating container: %s", err) |
| 60 | + a.containerURL = containerURL |
| 61 | + return nil |
| 62 | +} |
| 63 | + |
| 64 | +func (a *AzureBlobStorage) parseMetadata(metadata bindings.Metadata) (*blobStorageMetadata, error) { |
| 65 | + connInfo := metadata.Properties |
| 66 | + b, err := json.Marshal(connInfo) |
| 67 | + if err != nil { |
| 68 | + return nil, err |
| 69 | + } |
| 70 | + |
| 71 | + var m blobStorageMetadata |
| 72 | + err = json.Unmarshal(b, &m) |
| 73 | + if err != nil { |
| 74 | + return nil, err |
| 75 | + } |
| 76 | + return &m, nil |
| 77 | +} |
| 78 | + |
| 79 | +func (a *AzureBlobStorage) Write(req *bindings.WriteRequest) error { |
| 80 | + name := "" |
| 81 | + if val, ok := req.Metadata[blobName]; ok && val != "" { |
| 82 | + name = val |
| 83 | + } else { |
| 84 | + name = uuid.New().String() |
| 85 | + } |
| 86 | + blobURL := a.containerURL.NewBlockBlobURL(name) |
| 87 | + _, err := azblob.UploadBufferToBlockBlob(context.Background(), req.Data, blobURL, azblob.UploadToBlockBlobOptions{ |
| 88 | + Parallelism: 16, |
| 89 | + }) |
| 90 | + return err |
| 91 | +} |
0 commit comments