Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 39 additions & 9 deletions alioss.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package storage

import (
"bytes"
"errors"
"io"
"net/url"
"os"
Expand All @@ -25,6 +26,27 @@ import (
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func wrapAliOSSError(err error) error {
if err == nil {
return nil
}
var svcErr oss.ServiceError
if errors.As(err, &svcErr) {
return &ErrorWithStatusCode{
Err: err,
StatusCode: svcErr.StatusCode,
}
}
var unexpectedErr oss.UnexpectedStatusCodeError
if errors.As(err, &unexpectedErr) {
return &ErrorWithStatusCode{
Err: err,
StatusCode: unexpectedErr.Got(),
}
}
return err
}

type aliOSSStorage struct {
conf *AliOSSConfig
bucket *oss.Bucket
Expand All @@ -50,7 +72,7 @@ func NewAliOSS(conf *AliOSSConfig) (Storage, error) {
func (s *aliOSSStorage) UploadData(data []byte, storagePath, contentType string) (string, int64, error) {
reader := bytes.NewBuffer(data)
if err := s.bucket.PutObject(storagePath, reader, oss.ContentType(contentType)); err != nil {
return "", 0, err
return "", 0, wrapAliOSSError(err)
}

return s.location(storagePath), int64(len(data)), nil
Expand All @@ -63,7 +85,7 @@ func (s *aliOSSStorage) UploadFile(filepath, storagePath, contentType string) (s
}

if err = s.bucket.PutObjectFromFile(storagePath, filepath, oss.ContentType(contentType)); err != nil {
return "", 0, err
return "", 0, wrapAliOSSError(err)
}

return s.location(storagePath), info.Size(), nil
Expand All @@ -82,7 +104,7 @@ func (s *aliOSSStorage) ListObjects(prefix string) ([]string, error) {
for {
lor, err := s.bucket.ListObjects(oss.Prefix(prefix), marker)
if err != nil {
return nil, err
return nil, wrapAliOSSError(err)
}

for _, object := range lor.Objects {
Expand All @@ -101,16 +123,20 @@ func (s *aliOSSStorage) ListObjects(prefix string) ([]string, error) {
func (s *aliOSSStorage) DownloadData(storagePath string) ([]byte, error) {
reader, err := s.bucket.GetObject(storagePath)
if err != nil {
return nil, err
return nil, wrapAliOSSError(err)
}
defer reader.Close()

return io.ReadAll(reader)
data, err := io.ReadAll(reader)
if err != nil {
return nil, wrapAliOSSError(err)
}
return data, nil
}

func (s *aliOSSStorage) DownloadFile(filepath, storagePath string) (int64, error) {
if err := s.bucket.GetObjectToFile(storagePath, filepath); err != nil {
return 0, err
return 0, wrapAliOSSError(err)
}

info, err := os.Stat(filepath)
Expand All @@ -122,14 +148,18 @@ func (s *aliOSSStorage) DownloadFile(filepath, storagePath string) (int64, error
}

func (s *aliOSSStorage) GeneratePresignedUrl(storagePath string, expiration time.Duration) (string, error) {
return s.bucket.SignURL(storagePath, oss.HTTPGet, int64(expiration.Seconds()))
u, err := s.bucket.SignURL(storagePath, oss.HTTPGet, int64(expiration.Seconds()))
if err != nil {
return "", wrapAliOSSError(err)
}
return u, nil
}

func (s *aliOSSStorage) DeleteObject(storagePath string) error {
return s.bucket.DeleteObject(storagePath)
return wrapAliOSSError(s.bucket.DeleteObject(storagePath))
}

func (s *aliOSSStorage) DeleteObjects(storagePaths []string) error {
_, err := s.bucket.DeleteObjects(storagePaths)
return err
return wrapAliOSSError(err)
}
31 changes: 23 additions & 8 deletions azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,28 @@ import (
"path"
"time"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"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/sas"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/service"
)

func wrapAzureError(err error) error {
if err == nil {
return nil
}
var respErr *azcore.ResponseError
if errors.As(err, &respErr) {
return &ErrorWithStatusCode{
Err: err,
StatusCode: respErr.StatusCode,
}
}
return err
}

const azureBlockSize = 4 * 1024 * 1024
const azureParallelism = 16

Expand Down Expand Up @@ -73,7 +88,7 @@ func (s *azureBLOBStorage) UploadData(data []byte, storagePath, contentType stri
Concurrency: azureParallelism,
})
if err != nil {
return "", 0, err
return "", 0, wrapAzureError(err)
}
return s.location(storagePath), int64(len(data)), nil
}
Expand All @@ -98,7 +113,7 @@ func (s *azureBLOBStorage) UploadFile(filepath, storagePath, contentType string)
Concurrency: azureParallelism,
})
if err != nil {
return "", 0, err
return "", 0, wrapAzureError(err)
}

return s.location(storagePath), stat.Size(), nil
Expand All @@ -113,7 +128,7 @@ func (s *azureBLOBStorage) ListObjects(prefix string) ([]string, error) {
for pager.More() {
page, err := pager.NextPage(context.Background())
if err != nil {
return nil, err
return nil, wrapAzureError(err)
}
for _, item := range page.Segment.BlobItems {
if item.Name != nil {
Expand All @@ -131,7 +146,7 @@ func (s *azureBLOBStorage) DownloadData(storagePath string) ([]byte, error) {

props, err := blobClient.GetProperties(ctx, nil)
if err != nil {
return nil, err
return nil, wrapAzureError(err)
}
if props.ContentLength == nil {
return nil, errors.New("azure: missing content length")
Expand All @@ -143,7 +158,7 @@ func (s *azureBLOBStorage) DownloadData(storagePath string) ([]byte, error) {
Concurrency: azureParallelism,
})
if err != nil {
return nil, err
return nil, wrapAzureError(err)
}
return buf, nil
}
Expand All @@ -160,7 +175,7 @@ func (s *azureBLOBStorage) DownloadFile(filepath, storagePath string) (int64, er
Concurrency: azureParallelism,
})
if err != nil {
return 0, err
return 0, wrapAzureError(err)
}

stat, err := file.Stat()
Expand Down Expand Up @@ -188,7 +203,7 @@ func (s *azureBLOBStorage) GeneratePresignedUrl(storagePath string, expiration t
Expiry: to.Ptr(exp.Format(sas.TimeFormat)),
}, nil)
if err != nil {
return "", err
return "", wrapAzureError(err)
}

qp, err := sas.BlobSignatureValues{
Expand All @@ -214,7 +229,7 @@ func (s *azureBLOBStorage) GeneratePresignedUrl(storagePath string, expiration t

func (s *azureBLOBStorage) DeleteObject(storagePath string) error {
_, err := s.client.DeleteBlob(context.Background(), s.conf.ContainerName, storagePath, nil)
return err
return wrapAzureError(err)
}

func (s *azureBLOBStorage) DeleteObjects(storagePaths []string) error {
Expand Down
18 changes: 18 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package storage

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

license header missing


import "fmt"

var _ error = (*ErrorWithStatusCode)(nil)

type ErrorWithStatusCode struct {
Err error
StatusCode int
}

func (e *ErrorWithStatusCode) Error() string {
return fmt.Sprintf("Err: %s, Status Code: %d", e.Err, e.StatusCode)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nit: error stings are usually not capitalized

}

func (e *ErrorWithStatusCode) UnWrap() error {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

should this be Unwrap instead?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

maybe we could add assertion on the wrapped error in tests as well

return e.Err
}
Loading
Loading