Browse Source

fix not found error check in azure blob client

Signed-off-by: r2k1 <yokree@gmail.com>
r2k1 3 years ago
parent
commit
20a26d916a
1 changed files with 3 additions and 27 deletions
  1. 3 27
      pkg/storage/azurestorage.go

+ 3 - 27
pkg/storage/azurestorage.go

@@ -12,7 +12,6 @@ import (
 	"net"
 	"net/http"
 	"net/url"
-	"regexp"
 	"strings"
 	"sync"
 	"time"
@@ -32,8 +31,6 @@ const (
 	azureDefaultEndpoint = "blob.core.windows.net"
 )
 
-var errorCodeRegex = regexp.MustCompile(`X-Ms-Error-Code:\D*\[(\w+)\]`)
-
 // Set default retry values to default Azure values. 0 = use Default Azure.
 var defaultAzureConfig = AzureConfig{
 	PipelineConfig: PipelineConfig{
@@ -332,12 +329,13 @@ func (b *AzureStorage) Remove(name string) error {
 func (b *AzureStorage) Exists(name string) (bool, error) {
 	name = trimLeading(name)
 	ctx := context.Background()
-
 	blobURL := getBlobURL(name, b.containerURL)
 	if _, err := blobURL.GetProperties(ctx, blob.BlobAccessConditions{}, blob.ClientProvidedKeyOptions{}); err != nil {
-		if b.isObjNotFoundErr(err) {
+		var se blob.StorageError
+		if errors.As(err, &se) && se.ServiceCode() == blob.ServiceCodeBlobNotFound {
 			return false, nil
 		}
+
 		return false, errors.Wrapf(err, "cannot get properties for Azure blob, address: %s", name)
 	}
 
@@ -456,20 +454,6 @@ func (b *AzureStorage) ListDirectories(path string) ([]*StorageInfo, error) {
 	return stats, nil
 }
 
-// IsObjNotFoundErr returns true if error means that object is not found. Relevant to Get operations.
-func (b *AzureStorage) isObjNotFoundErr(err error) bool {
-	if err == nil {
-		return false
-	}
-
-	errorCode := parseError(err.Error())
-	if errorCode == "InvalidUri" || errorCode == "BlobNotFound" {
-		return true
-	}
-
-	return false
-}
-
 func (b *AzureStorage) getBlobReader(ctx context.Context, name string, offset, length int64) (io.ReadCloser, error) {
 	log.Debugf("Getting blob: %s, offset: %d, length: %d", name, offset, length)
 	if name == "" {
@@ -681,11 +665,3 @@ func createContainer(ctx context.Context, conf AzureConfig) (blob.ContainerURL,
 func getBlobURL(blobName string, c blob.ContainerURL) blob.BlockBlobURL {
 	return c.NewBlockBlobURL(blobName)
 }
-
-func parseError(errorCode string) string {
-	match := errorCodeRegex.FindStringSubmatch(errorCode)
-	if len(match) == 2 {
-		return match[1]
-	}
-	return errorCode
-}