Skip to content

Commit

Permalink
fix: fix aws s3 get - debug3
Browse files Browse the repository at this point in the history
  • Loading branch information
aldor007 committed May 21, 2023
1 parent a61bd22 commit 74fb562
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 9 deletions.
3 changes: 2 additions & 1 deletion cmd/mort/mort.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package main
import (
"flag"
"fmt"
mortMiddleware "github.com/aldor007/mort/pkg/middleware"
"net/http"
"time"

mortMiddleware "github.com/aldor007/mort/pkg/middleware"

"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/httplog"
Expand Down
10 changes: 6 additions & 4 deletions pkg/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func Get(obj *object.FileObject) *response.Response {
client := instance.container
if err != nil {
monitoring.Log().Info("Storage/Get get client", obj.LogData(zap.Error(err))...)
return response.NewError(503, err)
return response.NewError(503, fmt.Errorf("unable to get client %s, err: %v", obj.Key, err))
}

item, err := client.Item(key)
Expand All @@ -80,7 +80,7 @@ func Get(obj *object.FileObject) *response.Response {
}

monitoring.Log().Info("Storage/Get item response", obj.LogData(zap.Error(err))...)
return response.NewError(500, err)
return response.NewError(500, fmt.Errorf("get item %s, err %v", obj.Key, err))
}

if isDir(item) {
Expand All @@ -103,7 +103,7 @@ func Get(obj *object.FileObject) *response.Response {
}
if err != nil {
monitoring.Log().Warn("Storage/Get open item", obj.LogData(zap.Int("statusCode", 500), zap.Error(err))...)
return response.NewError(500, err)
return response.NewError(500, fmt.Errorf("unable to open item %s err: %v", obj.Key, err))
}
resData.stream = responseStream
return prepareResponse(obj, resData)
Expand Down Expand Up @@ -420,6 +420,8 @@ func getClient(obj *object.FileObject) (storageClient, error) {
azureStorage.ConfigAccount: storageCfg.AzureAccount,
azureStorage.ConfigKey: storageCfg.AzureKey,
}
default:
return storageClient{}, fmt.Errorf("unknown kind %s", storageCfg.Kind)

}

Expand Down Expand Up @@ -475,7 +477,7 @@ func prepareResponse(obj *object.FileObject, resData responseData) *response.Res

if err != nil {
monitoring.Log().Warn("Storage/prepareResponse read metadata error", obj.LogData(zap.Int("statusCode", 500), zap.Error(err))...)
return response.NewError(500, err)
return response.NewError(500, fmt.Errorf("metadata read err %v", err))
}

parseMetadata(obj, metadata, res)
Expand Down
28 changes: 24 additions & 4 deletions pkg/storage/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ package storage
import (
"bytes"
"fmt"
"github.com/aldor007/mort/pkg/config"
"github.com/aldor007/mort/pkg/object"
"github.com/aldor007/mort/pkg/response"
"github.com/stretchr/testify/assert"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"testing"

"github.com/aldor007/mort/pkg/config"
"github.com/aldor007/mort/pkg/object"
"github.com/aldor007/mort/pkg/response"
"github.com/stretchr/testify/assert"
)

func TestMain(m *testing.M) {
Expand Down Expand Up @@ -243,6 +244,25 @@ func TestGetClientAllStorage(t *testing.T) {
}
}

func TestGetS3(t *testing.T) {
if os.Getenv("S3_ACCESS_KEY") == "" {
t.Skip()
}
mortConfig := config.Config{}
mortConfig.Load("testdata/config_s3.yml")
obj, err := object.NewFileObjectFromPath("/files/sources/2022/Lizbona_2_e38d7c5cac.jpg", &mortConfig)
assert.NoError(t, err)
res := Get(obj)
assert.NoError(t, res.Error())
assert.Equal(t, 200, res.StatusCode)

obj, err = object.NewFileObjectFromPath("/images/transform/ZmlsZXMvc291cmNlcy8yMDIyL0xpemJvbmFfMl9lMzhkN2M1Y2FjLmpwZw/photo_Lizbona-2-jpg_big300.jpg", &mortConfig)
assert.NoError(t, err)
res = Get(obj)
assert.NoError(t, res.Error())
assert.Equal(t, 200, res.StatusCode)
}

func BenchmarkGet(b *testing.B) {
mortConfig := config.Config{}
mortConfig.Load("testdata/all-storages.yml")
Expand Down
19 changes: 19 additions & 0 deletions pkg/storage/testdata/config_s3.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
buckets:
files:
storages:
basic:
kind: "s3"
pathPrefix: "files"
accessKey: "${S3_ACCESS_KEY}"
secretAccessKey: "${S3_SECRET_ACCESS_KEY}"
region: "eu-central-1"
bucket: "${S3_BUCKET}"
images:
storages:
basic:
kind: "s3"
pathPrefix: "images"
accessKey: "${S3_ACCESS_KEY}"
secretAccessKey: "${S3_SECRET_ACCESS_KEY}"
region: "eu-central-1"
bucket: "${S3_BUCKET}"

0 comments on commit 74fb562

Please # to comment.