Handle object not found error in bindings: S3, Azure Blob Storage, GCP Bucket, Huawei OBS (#3223)
Signed-off-by: sadath-12 <sadathsadu2002@gmail.com> Signed-off-by: Alessandro (Ale) Segala <43508+ItalyPaleAle@users.noreply.github.com> Co-authored-by: Alessandro (Ale) Segala <43508+ItalyPaleAle@users.noreply.github.com>
This commit is contained in:
parent
dd8d2ba185
commit
95690ac052
|
@ -18,6 +18,7 @@ import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
b64 "encoding/base64"
|
b64 "encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -27,8 +28,10 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/aws/aws-sdk-go/aws"
|
"github.com/aws/aws-sdk-go/aws"
|
||||||
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
||||||
"github.com/aws/aws-sdk-go/aws/session"
|
"github.com/aws/aws-sdk-go/aws/session"
|
||||||
"github.com/aws/aws-sdk-go/service/s3"
|
"github.com/aws/aws-sdk-go/service/s3"
|
||||||
|
|
||||||
"github.com/aws/aws-sdk-go/service/s3/s3manager"
|
"github.com/aws/aws-sdk-go/service/s3/s3manager"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
|
||||||
|
@ -300,6 +303,10 @@ func (s *AWSS3) get(ctx context.Context, req *bindings.InvokeRequest) (*bindings
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
var awsErr awserr.Error
|
||||||
|
if errors.As(err, &awsErr) && awsErr.Code() == s3.ErrCodeNoSuchKey {
|
||||||
|
return nil, fmt.Errorf("object not found")
|
||||||
|
}
|
||||||
return nil, fmt.Errorf("s3 binding error: error downloading S3 object: %w", err)
|
return nil, fmt.Errorf("s3 binding error: error downloading S3 object: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -331,6 +338,10 @@ func (s *AWSS3) delete(ctx context.Context, req *bindings.InvokeRequest) (*bindi
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
var awsErr awserr.Error
|
||||||
|
if errors.As(err, &awsErr) && awsErr.Code() == s3.ErrCodeNoSuchKey {
|
||||||
|
return nil, fmt.Errorf("object not found")
|
||||||
|
}
|
||||||
return nil, fmt.Errorf("s3 binding error: delete operation failed: %w", err)
|
return nil, fmt.Errorf("s3 binding error: delete operation failed: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -25,6 +25,7 @@ import (
|
||||||
|
|
||||||
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
|
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
|
||||||
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/blob"
|
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/blob"
|
||||||
|
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/bloberror"
|
||||||
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/blockblob"
|
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/blockblob"
|
||||||
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/container"
|
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/container"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
@ -190,6 +191,9 @@ func (a *AzureBlobStorage) get(ctx context.Context, req *bindings.InvokeRequest)
|
||||||
|
|
||||||
blobDownloadResponse, err := blockBlobClient.DownloadStream(ctx, &downloadOptions)
|
blobDownloadResponse, err := blockBlobClient.DownloadStream(ctx, &downloadOptions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if bloberror.HasCode(err, bloberror.BlobNotFound) {
|
||||||
|
return nil, fmt.Errorf("blob not found")
|
||||||
|
}
|
||||||
return nil, fmt.Errorf("error downloading az blob: %w", err)
|
return nil, fmt.Errorf("error downloading az blob: %w", err)
|
||||||
}
|
}
|
||||||
reader := blobDownloadResponse.Body
|
reader := blobDownloadResponse.Body
|
||||||
|
@ -256,6 +260,10 @@ func (a *AzureBlobStorage) delete(ctx context.Context, req *bindings.InvokeReque
|
||||||
blockBlobClient = a.containerClient.NewBlockBlobClient(val)
|
blockBlobClient = a.containerClient.NewBlockBlobClient(val)
|
||||||
_, err := blockBlobClient.Delete(ctx, &deleteOptions)
|
_, err := blockBlobClient.Delete(ctx, &deleteOptions)
|
||||||
|
|
||||||
|
if bloberror.HasCode(err, bloberror.BlobNotFound) {
|
||||||
|
return nil, fmt.Errorf("blob not found")
|
||||||
|
}
|
||||||
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,14 +18,17 @@ import (
|
||||||
"context"
|
"context"
|
||||||
b64 "encoding/base64"
|
b64 "encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"cloud.google.com/go/storage"
|
"cloud.google.com/go/storage"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
"google.golang.org/api/googleapi"
|
||||||
"google.golang.org/api/iterator"
|
"google.golang.org/api/iterator"
|
||||||
"google.golang.org/api/option"
|
"google.golang.org/api/option"
|
||||||
|
|
||||||
|
@ -213,6 +216,11 @@ func (g *GCPStorage) get(ctx context.Context, req *bindings.InvokeRequest) (*bin
|
||||||
var rc io.ReadCloser
|
var rc io.ReadCloser
|
||||||
rc, err = g.client.Bucket(g.metadata.Bucket).Object(key).NewReader(ctx)
|
rc, err = g.client.Bucket(g.metadata.Bucket).Object(key).NewReader(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
var apiErr *googleapi.Error
|
||||||
|
if errors.As(err, &apiErr) && apiErr.Code == http.StatusNotFound {
|
||||||
|
return nil, errors.New("object not found")
|
||||||
|
}
|
||||||
|
|
||||||
return nil, fmt.Errorf("gcp bucketgcp bucket binding error: error downloading bucket object: %w", err)
|
return nil, fmt.Errorf("gcp bucketgcp bucket binding error: error downloading bucket object: %w", err)
|
||||||
}
|
}
|
||||||
defer rc.Close()
|
defer rc.Close()
|
||||||
|
@ -245,6 +253,11 @@ func (g *GCPStorage) delete(ctx context.Context, req *bindings.InvokeRequest) (*
|
||||||
|
|
||||||
err := object.Delete(ctx)
|
err := object.Delete(ctx)
|
||||||
|
|
||||||
|
var apiErr *googleapi.Error
|
||||||
|
if errors.As(err, &apiErr) && apiErr.Code == http.StatusNotFound {
|
||||||
|
return nil, errors.New("object not found")
|
||||||
|
}
|
||||||
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,8 +17,10 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"net/http"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
@ -219,6 +221,10 @@ func (o *HuaweiOBS) get(ctx context.Context, req *bindings.InvokeRequest) (*bind
|
||||||
|
|
||||||
out, err := o.service.GetObject(ctx, input)
|
out, err := o.service.GetObject(ctx, input)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
var obsErr obs.ObsError
|
||||||
|
if errors.As(err, &obsErr) && obsErr.StatusCode == http.StatusNotFound {
|
||||||
|
return nil, errors.New("object not found")
|
||||||
|
}
|
||||||
return nil, fmt.Errorf("obs binding error. error getting obs object: %w", err)
|
return nil, fmt.Errorf("obs binding error. error getting obs object: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -255,6 +261,10 @@ func (o *HuaweiOBS) delete(ctx context.Context, req *bindings.InvokeRequest) (*b
|
||||||
|
|
||||||
out, err := o.service.DeleteObject(ctx, input)
|
out, err := o.service.DeleteObject(ctx, input)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
var obsErr obs.ObsError
|
||||||
|
if errors.As(err, &obsErr) && obsErr.StatusCode == http.StatusNotFound {
|
||||||
|
return nil, errors.New("object not found")
|
||||||
|
}
|
||||||
return nil, fmt.Errorf("obs binding error. error deleting obs object: %w", err)
|
return nil, fmt.Errorf("obs binding error. error deleting obs object: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -43,6 +43,7 @@ import (
|
||||||
const (
|
const (
|
||||||
sidecarName = "bindings-s3-sidecar"
|
sidecarName = "bindings-s3-sidecar"
|
||||||
bindingsMetadataName = "s3-cert-tests"
|
bindingsMetadataName = "s3-cert-tests"
|
||||||
|
objNotFound = "object not found"
|
||||||
)
|
)
|
||||||
|
|
||||||
var bucketName = "bucketName"
|
var bucketName = "bucketName"
|
||||||
|
@ -207,7 +208,7 @@ func S3SBasic(t *testing.T) {
|
||||||
// confirm the deletion.
|
// confirm the deletion.
|
||||||
_, invokeSecondGetErr := getObjectRequest(ctx, client, objectName, false)
|
_, invokeSecondGetErr := getObjectRequest(ctx, client, objectName, false)
|
||||||
assert.Error(t, invokeSecondGetErr)
|
assert.Error(t, invokeSecondGetErr)
|
||||||
assert.Contains(t, invokeSecondGetErr.Error(), "error downloading S3 object")
|
assert.Contains(t, invokeSecondGetErr.Error(), objNotFound)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -270,7 +271,7 @@ func S3SForcePathStyle(t *testing.T) {
|
||||||
// confirm the deletion.
|
// confirm the deletion.
|
||||||
_, invokeSecondGetErr := getObjectRequest(ctx, client, objectName, false)
|
_, invokeSecondGetErr := getObjectRequest(ctx, client, objectName, false)
|
||||||
assert.Error(t, invokeSecondGetErr)
|
assert.Error(t, invokeSecondGetErr)
|
||||||
assert.Contains(t, invokeSecondGetErr.Error(), "error downloading S3 object")
|
assert.Contains(t, invokeSecondGetErr.Error(), objNotFound)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,7 +46,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
sidecarName = "blobstorage-sidecar"
|
sidecarName = "blobstorage-sidecar"
|
||||||
|
blobNotFound = "blob not found"
|
||||||
)
|
)
|
||||||
|
|
||||||
// getBlobRequest is used to make a common binding request for the get operation.
|
// getBlobRequest is used to make a common binding request for the get operation.
|
||||||
|
@ -198,12 +199,12 @@ func TestBlobStorage(t *testing.T) {
|
||||||
// confirm the deletion.
|
// confirm the deletion.
|
||||||
_, invokeSecondGetErr := getBlobRequest(ctx, client, blobName, false)
|
_, invokeSecondGetErr := getBlobRequest(ctx, client, blobName, false)
|
||||||
assert.Error(t, invokeSecondGetErr)
|
assert.Error(t, invokeSecondGetErr)
|
||||||
assert.Contains(t, invokeSecondGetErr.Error(), bloberror.BlobNotFound)
|
assert.Contains(t, invokeSecondGetErr.Error(), blobNotFound)
|
||||||
|
|
||||||
// deleting the key again should fail.
|
// deleting the key again should fail.
|
||||||
_, invokeDeleteErr2 := deleteBlobRequest(ctx, client, blobName, nil)
|
_, invokeDeleteErr2 := deleteBlobRequest(ctx, client, blobName, nil)
|
||||||
assert.Error(t, invokeDeleteErr2)
|
assert.Error(t, invokeDeleteErr2)
|
||||||
assert.Contains(t, invokeDeleteErr2.Error(), bloberror.BlobNotFound)
|
assert.Contains(t, invokeDeleteErr2.Error(), blobNotFound)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -285,7 +286,7 @@ func TestBlobStorage(t *testing.T) {
|
||||||
// confirm the deletion.
|
// confirm the deletion.
|
||||||
_, invokeSecondGetErr := getBlobRequest(ctx, client, blobName, false)
|
_, invokeSecondGetErr := getBlobRequest(ctx, client, blobName, false)
|
||||||
assert.Error(t, invokeSecondGetErr)
|
assert.Error(t, invokeSecondGetErr)
|
||||||
assert.Contains(t, invokeSecondGetErr.Error(), bloberror.BlobNotFound)
|
assert.Contains(t, invokeSecondGetErr.Error(), blobNotFound)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -425,7 +426,7 @@ func TestBlobStorage(t *testing.T) {
|
||||||
// confirm the deletion.
|
// confirm the deletion.
|
||||||
_, invokeSecondGetErr := getBlobRequest(ctx, client, "filename.txt", false)
|
_, invokeSecondGetErr := getBlobRequest(ctx, client, "filename.txt", false)
|
||||||
assert.Error(t, invokeSecondGetErr)
|
assert.Error(t, invokeSecondGetErr)
|
||||||
assert.Contains(t, invokeSecondGetErr.Error(), bloberror.BlobNotFound)
|
assert.Contains(t, invokeSecondGetErr.Error(), blobNotFound)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue