forked from versity/versitygw
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add optional sidecar files for metadata
This adds the option to store metadata for objects and buckets within a hidden folder at the top level: bucket: .vgw_meta/<bucket>/.meta/<attribute> object: .vgw_meta/bucket/<object>/.meta/<attribute> Example invocation: ./versitygw -a myaccess -s mysecret posix --metadata sidecar /tmp/gw The attributes are stored by name within the hidden directory.
- Loading branch information
1 parent
80b316f
commit c380053
Showing
6 changed files
with
183 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package meta | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
// SideCar is a metadata storer that uses sidecar files to store metadata. | ||
type SideCar struct{} | ||
|
||
const ( | ||
sidecardir = ".vgw_meta" | ||
sidecarmeta = ".meta" | ||
) | ||
|
||
// RetrieveAttribute retrieves the value of a specific attribute for an object or a bucket. | ||
func (s SideCar) RetrieveAttribute(f *os.File, bucket, object, attribute string) ([]byte, error) { | ||
metadir := filepath.Join(sidecardir, bucket, object, sidecarmeta) | ||
if object == "" { | ||
metadir = filepath.Join(sidecardir, bucket, sidecarmeta) | ||
} | ||
attr := filepath.Join(metadir, attribute) | ||
|
||
value, err := os.ReadFile(attr) | ||
if errors.Is(err, os.ErrNotExist) { | ||
return nil, ErrNoSuchKey | ||
} | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to read attribute: %v", err) | ||
} | ||
|
||
return value, nil | ||
} | ||
|
||
// StoreAttribute stores the value of a specific attribute for an object or a bucket. | ||
func (s SideCar) StoreAttribute(f *os.File, bucket, object, attribute string, value []byte) error { | ||
metadir := filepath.Join(sidecardir, bucket, object, sidecarmeta) | ||
if object == "" { | ||
metadir = filepath.Join(sidecardir, bucket, sidecarmeta) | ||
} | ||
err := os.MkdirAll(metadir, 0777) | ||
if err != nil { | ||
return fmt.Errorf("failed to create metadata directory: %v", err) | ||
} | ||
|
||
attr := filepath.Join(metadir, attribute) | ||
err = os.WriteFile(attr, value, 0666) | ||
if err != nil { | ||
return fmt.Errorf("failed to write attribute: %v", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// DeleteAttribute removes the value of a specific attribute for an object or a bucket. | ||
func (s SideCar) DeleteAttribute(bucket, object, attribute string) error { | ||
metadir := filepath.Join(sidecardir, bucket, object, sidecarmeta) | ||
if object == "" { | ||
metadir = filepath.Join(sidecardir, bucket, sidecarmeta) | ||
} | ||
attr := filepath.Join(metadir, attribute) | ||
|
||
err := os.Remove(attr) | ||
if errors.Is(err, os.ErrNotExist) { | ||
return ErrNoSuchKey | ||
} | ||
if err != nil { | ||
return fmt.Errorf("failed to remove attribute: %v", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// ListAttributes lists all attributes for an object or a bucket. | ||
func (s SideCar) ListAttributes(bucket, object string) ([]string, error) { | ||
metadir := filepath.Join(sidecardir, bucket, object, sidecarmeta) | ||
if object == "" { | ||
metadir = filepath.Join(sidecardir, bucket, sidecarmeta) | ||
} | ||
|
||
ents, err := os.ReadDir(metadir) | ||
if errors.Is(err, os.ErrNotExist) { | ||
return []string{}, nil | ||
} | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to list attributes: %v", err) | ||
} | ||
|
||
var attrs []string | ||
for _, ent := range ents { | ||
attrs = append(attrs, ent.Name()) | ||
} | ||
|
||
return attrs, nil | ||
} | ||
|
||
// DeleteAttributes removes all attributes for an object or a bucket. | ||
func (s SideCar) DeleteAttributes(bucket, object string) error { | ||
metadir := filepath.Join(sidecardir, bucket, object, sidecarmeta) | ||
if object == "" { | ||
metadir = filepath.Join(sidecardir, bucket, sidecarmeta) | ||
} | ||
|
||
err := os.RemoveAll(metadir) | ||
if err != nil && !errors.Is(err, os.ErrNotExist) { | ||
return fmt.Errorf("failed to remove attributes: %v", err) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters