Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle object not found error in bindings: S3, Azure Blob Storage, GCP Bucket, Huawei OBS #3223

Merged
merged 4 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions bindings/aws/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"crypto/tls"
b64 "encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
Expand All @@ -27,8 +28,10 @@ import (
"time"

"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/service/s3"

"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/google/uuid"

Expand Down Expand Up @@ -300,6 +303,10 @@ func (s *AWSS3) get(ctx context.Context, req *bindings.InvokeRequest) (*bindings
},
)
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)
}

Expand Down Expand Up @@ -331,6 +338,10 @@ func (s *AWSS3) delete(ctx context.Context, req *bindings.InvokeRequest) (*bindi
},
)
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)
}

Expand Down
9 changes: 9 additions & 0 deletions bindings/azure/blobstorage/blobstorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

"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/bloberror"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/blockblob"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/container"
"github.com/google/uuid"
Expand Down Expand Up @@ -189,7 +190,11 @@
}

blobDownloadResponse, err := blockBlobClient.DownloadStream(ctx, &downloadOptions)

Check failure on line 193 in bindings/azure/blobstorage/blobstorage.go

View workflow job for this annotation

GitHub Actions / Build linux_amd64 binaries

File is not `gofumpt`-ed (gofumpt)
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)
}
reader := blobDownloadResponse.Body
Expand Down Expand Up @@ -256,6 +261,10 @@
blockBlobClient = a.containerClient.NewBlockBlobClient(val)
_, err := blockBlobClient.Delete(ctx, &deleteOptions)

if bloberror.HasCode(err, bloberror.BlobNotFound) {
return nil, fmt.Errorf("blob not found")
}

return nil, err
}

Expand Down
10 changes: 10 additions & 0 deletions bindings/gcp/bucket/bucket.go
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are almost done - you still need to format this file properly - that is done with the gofumpt tool. There are other files that need to be formatted as well - see the linter error (make lint to run the linter locally)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @berndverst 🙂

Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,22 @@
"bytes"
"context"
b64 "encoding/base64"
"encoding/json"

Check failure on line 20 in bindings/gcp/bucket/bucket.go

View workflow job for this annotation

GitHub Actions / Build linux_amd64 binaries

File is not `gofmt`-ed with `-s` (gofmt)
"fmt"
"io"
"net/url"
"reflect"
"strconv"
"errors"
"net/http"


"cloud.google.com/go/storage"
"github.com/google/uuid"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
"google.golang.org/api/googleapi"


"github.com/dapr/components-contrib/bindings"
"github.com/dapr/components-contrib/metadata"
Expand Down Expand Up @@ -213,6 +218,11 @@
var rc io.ReadCloser
rc, err = g.client.Bucket(g.metadata.Bucket).Object(key).NewReader(ctx)
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)
}
defer rc.Close()
Expand Down
6 changes: 6 additions & 0 deletions bindings/huawei/obs/obs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"

Check failure on line 22 in bindings/huawei/obs/obs.go

View workflow job for this annotation

GitHub Actions / Build linux_amd64 binaries

File is not `gofmt`-ed with `-s` (gofmt)
"reflect"
"strconv"
"net/http"

"github.com/google/uuid"
"github.com/huaweicloud/huaweicloud-sdk-go-obs/obs"
Expand Down Expand Up @@ -219,6 +221,10 @@

out, err := o.service.GetObject(ctx, input)
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)
}

Expand Down
5 changes: 3 additions & 2 deletions tests/certification/bindings/aws/s3/s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
const (
sidecarName = "bindings-s3-sidecar"
bindingsMetadataName = "s3-cert-tests"
objNotFound = "object not found"
)

var bucketName = "bucketName"
Expand Down Expand Up @@ -207,7 +208,7 @@ func S3SBasic(t *testing.T) {
// confirm the deletion.
_, invokeSecondGetErr := getObjectRequest(ctx, client, objectName, false)
assert.Error(t, invokeSecondGetErr)
assert.Contains(t, invokeSecondGetErr.Error(), "error downloading S3 object")
assert.Contains(t, invokeSecondGetErr.Error(), objNotFound)

return nil
}
Expand Down Expand Up @@ -270,7 +271,7 @@ func S3SForcePathStyle(t *testing.T) {
// confirm the deletion.
_, invokeSecondGetErr := getObjectRequest(ctx, client, objectName, false)
assert.Error(t, invokeSecondGetErr)
assert.Contains(t, invokeSecondGetErr.Error(), "error downloading S3 object")
assert.Contains(t, invokeSecondGetErr.Error(), objNotFound)

return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import (

const (
sidecarName = "blobstorage-sidecar"
blobNotFound = "blob not found"
)

// getBlobRequest is used to make a common binding request for the get operation.
Expand Down Expand Up @@ -198,12 +199,12 @@ func TestBlobStorage(t *testing.T) {
// confirm the deletion.
_, invokeSecondGetErr := getBlobRequest(ctx, client, blobName, false)
assert.Error(t, invokeSecondGetErr)
assert.Contains(t, invokeSecondGetErr.Error(), bloberror.BlobNotFound)
assert.Contains(t, invokeSecondGetErr.Error(), blobNotFound)

// deleting the key again should fail.
_, invokeDeleteErr2 := deleteBlobRequest(ctx, client, blobName, nil)
assert.Error(t, invokeDeleteErr2)
assert.Contains(t, invokeDeleteErr2.Error(), bloberror.BlobNotFound)
assert.Contains(t, invokeDeleteErr2.Error(), blobNotFound)

return nil
}
Expand Down Expand Up @@ -285,7 +286,7 @@ func TestBlobStorage(t *testing.T) {
// confirm the deletion.
_, invokeSecondGetErr := getBlobRequest(ctx, client, blobName, false)
assert.Error(t, invokeSecondGetErr)
assert.Contains(t, invokeSecondGetErr.Error(), bloberror.BlobNotFound)
assert.Contains(t, invokeSecondGetErr.Error(), blobNotFound)

return nil
}
Expand Down Expand Up @@ -425,7 +426,7 @@ func TestBlobStorage(t *testing.T) {
// confirm the deletion.
_, invokeSecondGetErr := getBlobRequest(ctx, client, "filename.txt", false)
assert.Error(t, invokeSecondGetErr)
assert.Contains(t, invokeSecondGetErr.Error(), bloberror.BlobNotFound)
assert.Contains(t, invokeSecondGetErr.Error(), blobNotFound)

return nil
}
Expand Down