fix read for s3, add test for read

This commit is contained in:
Shane C. 2024-11-18 16:39:35 -05:00
parent 17896c3cc4
commit 41e7d6070d
Signed by: Shane C.
GPG key ID: E46B5FEA35B22FF9
2 changed files with 18 additions and 4 deletions

View file

@ -6,6 +6,7 @@ import (
"errors" "errors"
"github.com/minio/minio-go/v7" "github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials" "github.com/minio/minio-go/v7/pkg/credentials"
"io"
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
@ -136,10 +137,11 @@ func (s *Storage) Read(name string) ([]byte, error) {
} }
} }
var objectBytes []byte objectBytes, err := io.ReadAll(object)
if _, err := object.Read(objectBytes); err != nil { if err != nil {
return nil, err return nil, err
} }
return objectBytes, nil return objectBytes, nil
} else { } else {
return os.ReadFile(filepath.Join(s.config.Path, name)) return os.ReadFile(filepath.Join(s.config.Path, name))

View file

@ -1,6 +1,7 @@
package fsys package fsys
import ( import (
"fmt"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"io" "io"
"testing" "testing"
@ -18,12 +19,17 @@ func TestStorageLocal(t *testing.T) {
err = stor.Write("test.txt", []byte("hi"), WriteOptions{}) err = stor.Write("test.txt", []byte("hi"), WriteOptions{})
assert.NoError(t, err) assert.NoError(t, err)
t.Log("== Read ==")
fileContent, err := stor.Read("test.txt")
assert.NoError(t, err)
assert.Equal(t, []byte("hi"), fileContent)
t.Log("== Open ==") t.Log("== Open ==")
file, err := stor.Open("test.txt") file, err := stor.Open("test.txt")
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, "test.txt", file.Name) assert.Equal(t, "test.txt", file.Name)
fileContent, err := io.ReadAll(file) fileContent, err = io.ReadAll(file)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, []byte("hi"), fileContent) assert.Equal(t, []byte("hi"), fileContent)
@ -47,12 +53,18 @@ func TestStorageS3(t *testing.T) {
err = stor.Write("test.txt", []byte("hi"), WriteOptions{}) err = stor.Write("test.txt", []byte("hi"), WriteOptions{})
assert.NoError(t, err) assert.NoError(t, err)
t.Log("== Read ==")
fileContent, err := stor.Read("test.txt")
assert.NoError(t, err)
fmt.Println(fileContent)
assert.Equal(t, []byte("hi"), fileContent)
t.Log("== Open ==") t.Log("== Open ==")
file, err := stor.Open("test.txt") file, err := stor.Open("test.txt")
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, "test.txt", file.Name) assert.Equal(t, "test.txt", file.Name)
fileContent, err := io.ReadAll(file) fileContent, err = io.ReadAll(file)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, []byte("hi"), fileContent) assert.Equal(t, []byte("hi"), fileContent)