mirror of
https://github.com/minio/minio.git
synced 2025-12-07 08:12:37 -05:00
Refactoring file storage driver to fsstorage
This commit is contained in:
22
pkgs/storage/fsstorage/fs_storage.go
Normal file
22
pkgs/storage/fsstorage/fs_storage.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package fsstorage
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type FileSystemStorage struct {
|
||||
RootDir string
|
||||
}
|
||||
|
||||
func (storage FileSystemStorage) Get(objectPath string) ([]byte, error) {
|
||||
return ioutil.ReadFile(path.Join(storage.RootDir, objectPath))
|
||||
|
||||
}
|
||||
|
||||
func (storage FileSystemStorage) Put(objectPath string, object []byte) error {
|
||||
os.MkdirAll(filepath.Dir(path.Join(storage.RootDir, objectPath)), 0700)
|
||||
return ioutil.WriteFile(path.Join(storage.RootDir, objectPath), object, 0600)
|
||||
}
|
||||
51
pkgs/storage/fsstorage/fs_storage_test.go
Normal file
51
pkgs/storage/fsstorage/fs_storage_test.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package fsstorage
|
||||
|
||||
import (
|
||||
. "gopkg.in/check.v1"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
)
|
||||
|
||||
type FileSystemStorageSuite struct{}
|
||||
|
||||
var _ = Suite(&FileStorageSuite{})
|
||||
|
||||
func makeTempTestDir() (string, error) {
|
||||
return ioutil.TempDir("/tmp", "minio-test-")
|
||||
}
|
||||
|
||||
func (s *FileStorageSuite) TestFileStoragePutAtRootPath(c *C) {
|
||||
rootDir, err := makeTempTestDir()
|
||||
c.Assert(err, IsNil)
|
||||
defer os.RemoveAll(rootDir)
|
||||
|
||||
var storage ObjectStorage
|
||||
storage = FileStorage{
|
||||
RootDir: rootDir,
|
||||
}
|
||||
|
||||
storage.Put("path1", []byte("object1"))
|
||||
|
||||
// assert object1 was created in correct path
|
||||
object1, err := storage.Get("path1")
|
||||
c.Assert(err, IsNil)
|
||||
c.Assert(string(object1), Equals, "object1")
|
||||
}
|
||||
|
||||
func (s *FileStorageSuite) TestFileStoragePutDirPath(c *C) {
|
||||
rootDir, err := makeTempTestDir()
|
||||
c.Assert(err, IsNil)
|
||||
defer os.RemoveAll(rootDir)
|
||||
|
||||
var storage ObjectStorage
|
||||
storage = FileStorage{
|
||||
RootDir: rootDir,
|
||||
}
|
||||
|
||||
storage.Put("path1/path2/path3", []byte("object"))
|
||||
|
||||
// assert object1 was created in correct path
|
||||
object1, err := storage.Get("path1/path2/path3")
|
||||
c.Assert(err, IsNil)
|
||||
c.Assert(string(object1), Equals, "object")
|
||||
}
|
||||
Reference in New Issue
Block a user