From 222b66c2f27252a01c4637c1a5d002888d98ada7 Mon Sep 17 00:00:00 2001 From: Shane C Date: Tue, 19 Nov 2024 09:20:53 -0500 Subject: [PATCH] add move func, fix test for copy --- storage.go | 20 ++++++++++++++++++++ storage_test.go | 24 ++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/storage.go b/storage.go index c4cf72d..51fcad2 100644 --- a/storage.go +++ b/storage.go @@ -273,3 +273,23 @@ func (s *Storage) Copy(name string, dest string) error { return nil } } + +func (s *Storage) Move(name string, dest string) error { + if s.s3Client != nil { + if _, err := s.s3Client.CopyObject(s.ctx, minio.CopyDestOptions{ + Bucket: s.config.S3BucketName, + Object: dest, + }, minio.CopySrcOptions{ + Bucket: s.config.S3BucketName, + Object: name, + }); err != nil { + return err + } + if err := s.s3Client.RemoveObject(s.ctx, s.config.S3BucketName, name, minio.RemoveObjectOptions{}); err != nil { + return err + } + return nil + } else { + return os.Rename(filepath.Join(s.config.Path, name), filepath.Join(s.config.Path, dest)) + } +} diff --git a/storage_test.go b/storage_test.go index 0429ff8..17062ff 100644 --- a/storage_test.go +++ b/storage_test.go @@ -39,7 +39,17 @@ func TestStorageLocal(t *testing.T) { assert.NoError(t, err) assert.Equal(t, fs.FileMode(0600), copyFileInfo.Mode) assert.Equal(t, "test2.txt", copyFileInfo.Name) - assert.Equal(t, int64(len("hi")), fileInfo.Size) + assert.Equal(t, int64(len("hi")), copyFileInfo.Size) + + t.Log("== Move ==") + err = stor.Move("test2.txt", "test3.txt") + assert.NoError(t, err) + + moveFileInfo, err := stor.Stat("test3.txt") + assert.NoError(t, err) + assert.Equal(t, fs.FileMode(0600), moveFileInfo.Mode) + assert.Equal(t, "test3.txt", moveFileInfo.Name) + assert.Equal(t, int64(len("hi")), moveFileInfo.Size) t.Log("== Open ==") file, err := stor.Open("test.txt") @@ -94,7 +104,17 @@ func TestStorageS3(t *testing.T) { assert.NoError(t, err) assert.Equal(t, fs.FileMode(0777), copyFileInfo.Mode) assert.Equal(t, "test2.txt", copyFileInfo.Name) - assert.Equal(t, int64(len("hi")), fileInfo.Size) + assert.Equal(t, int64(len("hi")), copyFileInfo.Size) + + t.Log("== Move ==") + err = stor.Move("test2.txt", "test3.txt") + assert.NoError(t, err) + + moveFileInfo, err := stor.Stat("test3.txt") + assert.NoError(t, err) + assert.Equal(t, fs.FileMode(0777), moveFileInfo.Mode) + assert.Equal(t, "test3.txt", moveFileInfo.Name) + assert.Equal(t, int64(len("hi")), moveFileInfo.Size) t.Log("== Open ==") file, err := stor.Open("test.txt")