Improvements to blobstorage binding

Spin-off from PR adding contexts to input bindings

Signed-off-by: ItalyPaleAle <43508+ItalyPaleAle@users.noreply.github.com>
This commit is contained in:
ItalyPaleAle 2022-06-29 19:55:02 +00:00
parent 7033c8f266
commit 14a9103bec
2 changed files with 11 additions and 15 deletions

View File

@ -14,17 +14,19 @@ limitations under the License.
package blobstorage
import (
"bytes"
"context"
b64 "encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"net/url"
"strconv"
"time"
"github.com/Azure/azure-storage-blob-go/azblob"
"github.com/google/uuid"
"github.com/mitchellh/mapstructure"
azauth "github.com/dapr/components-contrib/authentication/azure"
"github.com/dapr/components-contrib/bindings"
@ -143,8 +145,9 @@ func (a *AzureBlobStorage) Init(metadata bindings.Metadata) error {
containerURL = azblob.NewContainerURL(*URL, p)
}
ctx := context.Background()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
_, err = containerURL.Create(ctx, azblob.Metadata{}, m.PublicAccessLevel)
cancel()
// Don't return error, container might already exist
a.logger.Debugf("error creating container: %w", err)
a.containerURL = containerURL
@ -153,14 +156,8 @@ func (a *AzureBlobStorage) Init(metadata bindings.Metadata) error {
}
func (a *AzureBlobStorage) parseMetadata(metadata bindings.Metadata) (*blobStorageMetadata, error) {
connInfo := metadata.Properties
b, err := json.Marshal(connInfo)
if err != nil {
return nil, err
}
var m blobStorageMetadata
err = json.Unmarshal(b, &m)
err := mapstructure.WeakDecode(metadata.Properties, &m)
if err != nil {
return nil, err
}
@ -286,8 +283,7 @@ func (a *AzureBlobStorage) get(ctx context.Context, req *bindings.InvokeRequest)
bodyStream := resp.Body(azblob.RetryReaderOptions{MaxRetryRequests: a.metadata.GetBlobRetryCount})
b := bytes.Buffer{}
_, err = b.ReadFrom(bodyStream)
data, err := io.ReadAll(bodyStream)
if err != nil {
return nil, fmt.Errorf("error reading az blob body: %w", err)
}
@ -308,7 +304,7 @@ func (a *AzureBlobStorage) get(ctx context.Context, req *bindings.InvokeRequest)
}
return &bindings.InvokeResponse{
Data: b.Bytes(),
Data: data,
Metadata: metadata,
}, nil
}

View File

@ -78,7 +78,7 @@ func TestGetOption(t *testing.T) {
t.Run("return error if blobName is missing", func(t *testing.T) {
r := bindings.InvokeRequest{}
_, err := blobStorage.get(context.TODO(), &r)
_, err := blobStorage.get(context.Background(), &r)
if assert.Error(t, err) {
assert.Equal(t, ErrMissingBlobName, err)
}
@ -90,7 +90,7 @@ func TestDeleteOption(t *testing.T) {
t.Run("return error if blobName is missing", func(t *testing.T) {
r := bindings.InvokeRequest{}
_, err := blobStorage.delete(context.TODO(), &r)
_, err := blobStorage.delete(context.Background(), &r)
if assert.Error(t, err) {
assert.Equal(t, ErrMissingBlobName, err)
}
@ -102,7 +102,7 @@ func TestDeleteOption(t *testing.T) {
"blobName": "foo",
"deleteSnapshots": "invalid",
}
_, err := blobStorage.delete(context.TODO(), &r)
_, err := blobStorage.delete(context.Background(), &r)
assert.Error(t, err)
})
}