mirror of
https://github.com/hanzoai/s3.git
synced 2026-11-03 03:07:41 +00:00
hanzoai/s3 was an AGPL-3.0 MinIO fork whose network-copyleft blocks embedding
and reselling as a managed service. Replace it wholesale with a fork of
SeaweedFS (github.com/seaweedfs/seaweedfs, Apache-2.0, Go) which ships a
built-in S3 gateway + Filer.
- License: AGPL-3.0 -> Apache-2.0 (upstream LICENSE kept verbatim, incl.
"Copyright 2025 Chris Lu"); NOTICE added crediting SeaweedFS. No AGPL/BUSL/
SSPL/GPL or enterprise ("ee") files remain.
- Module: go.mod path set to github.com/hanzoai/s3 with a self-replace
(replace github.com/seaweedfs/seaweedfs => ./) so the ~1800 internal Go files
keep their upstream import paths with zero churn. Internal identifiers and
import paths are intentionally NOT renamed.
- CLI rebrand: the binary is built as `s3` (was `weed`); root usage, per-command
help/usage/flag strings, shell autocomplete program name, and the product name
in CLI output now read "Hanzo S3" / `s3`. Users never see `weed`.
(e.g. `s3 server`, `s3 filer`, `s3 master`, `s3 volume`, `s3 s3`, `s3 mount`.)
- Build: `make` (or `go build -o s3 ./weed`) builds the `s3` binary green;
`cmd/weed-db` and `cmd/weed-sql` also build; `go vet ./weed/command/` clean.
- README/LLM.md rewritten for Hanzo S3; AGENTS.md/CLAUDE.md symlinks preserved.
- Dropped a 33MB prebuilt Mach-O `stress` binary upstream had committed; `.gitignore`
updated to ignore the `s3` build output.
155 lines
4.2 KiB
Go
155 lines
4.2 KiB
Go
package gcssink
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
"cloud.google.com/go/storage"
|
|
"github.com/seaweedfs/seaweedfs/weed/replication/repl_util"
|
|
"golang.org/x/oauth2"
|
|
"golang.org/x/oauth2/google"
|
|
"google.golang.org/api/option"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/replication/sink"
|
|
"github.com/seaweedfs/seaweedfs/weed/replication/source"
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
|
)
|
|
|
|
type GcsSink struct {
|
|
client *storage.Client
|
|
bucket string
|
|
dir string
|
|
filerSource *source.FilerSource
|
|
isIncremental bool
|
|
}
|
|
|
|
func init() {
|
|
sink.Sinks = append(sink.Sinks, &GcsSink{})
|
|
}
|
|
|
|
func (g *GcsSink) GetName() string {
|
|
return "google_cloud_storage"
|
|
}
|
|
|
|
func (g *GcsSink) GetSinkToDirectory() string {
|
|
return g.dir
|
|
}
|
|
|
|
func (g *GcsSink) IsIncremental() bool {
|
|
return g.isIncremental
|
|
}
|
|
|
|
func (g *GcsSink) Initialize(configuration util.Configuration, prefix string) error {
|
|
g.isIncremental = configuration.GetBool(prefix + "is_incremental")
|
|
return g.initialize(
|
|
configuration.GetString(prefix+"google_application_credentials"),
|
|
configuration.GetString(prefix+"bucket"),
|
|
configuration.GetString(prefix+"directory"),
|
|
)
|
|
}
|
|
|
|
func (g *GcsSink) SetSourceFiler(s *source.FilerSource) {
|
|
g.filerSource = s
|
|
}
|
|
|
|
func (g *GcsSink) initialize(google_application_credentials, bucketName, dir string) error {
|
|
g.bucket = bucketName
|
|
g.dir = dir
|
|
|
|
// Creates a client.
|
|
var clientOpts []option.ClientOption
|
|
if google_application_credentials != "" {
|
|
var data []byte
|
|
var err error
|
|
if strings.HasPrefix(google_application_credentials, "{") {
|
|
data = []byte(google_application_credentials)
|
|
} else {
|
|
googleCredentialsPath := util.ResolvePath(google_application_credentials)
|
|
data, err = os.ReadFile(googleCredentialsPath)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to read credentials file %s: %v", googleCredentialsPath, err)
|
|
}
|
|
}
|
|
creds, err := google.CredentialsFromJSON(context.Background(), data, storage.ScopeFullControl)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to parse credentials: %v", err)
|
|
}
|
|
httpClient := oauth2.NewClient(context.Background(), creds.TokenSource)
|
|
clientOpts = append(clientOpts, option.WithHTTPClient(httpClient), option.WithoutAuthentication())
|
|
}
|
|
client, err := storage.NewClient(context.Background(), clientOpts...)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to create client with credentials \"%s\" env \"%s\": %v",
|
|
google_application_credentials, os.Getenv("GOOGLE_APPLICATION_CREDENTIALS"), err)
|
|
}
|
|
|
|
g.client = client
|
|
|
|
return nil
|
|
}
|
|
|
|
func (g *GcsSink) DeleteEntry(key string, isDirectory, deleteIncludeChunks bool, signatures []int32) error {
|
|
|
|
if isDirectory {
|
|
key = key + "/"
|
|
}
|
|
|
|
if err := g.client.Bucket(g.bucket).Object(key).Delete(context.Background()); err != nil {
|
|
return fmt.Errorf("gcs delete %s/%s: %v", g.bucket, key, err)
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (g *GcsSink) CreateEntry(key string, entry *filer_pb.Entry, signatures []int32) error {
|
|
|
|
if entry.IsDirectory {
|
|
return nil
|
|
}
|
|
|
|
totalSize := filer.FileSize(entry)
|
|
chunkViews := filer.ViewFromChunks(context.Background(), g.filerSource.LookupFileId, entry.GetChunks(), 0, int64(totalSize))
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
|
|
wc := g.client.Bucket(g.bucket).Object(key).NewWriter(ctx)
|
|
|
|
writeFunc := func(data []byte) error {
|
|
_, writeErr := wc.Write(data)
|
|
return writeErr
|
|
}
|
|
|
|
var writeErr error
|
|
if len(entry.Content) > 0 {
|
|
content, decErr := repl_util.MaybeDecryptContent(entry.Content, entry)
|
|
if decErr != nil {
|
|
writeErr = fmt.Errorf("decrypt inline SSE content: %w", decErr)
|
|
} else {
|
|
writeErr = writeFunc(content)
|
|
}
|
|
} else {
|
|
writeErr = repl_util.CopyFromChunkViews(chunkViews, g.filerSource, writeFunc, entry)
|
|
}
|
|
|
|
if writeErr != nil {
|
|
// Cancel the context to abort the GCS upload without touching
|
|
// any existing object at this key.
|
|
cancel()
|
|
wc.Close()
|
|
return writeErr
|
|
}
|
|
|
|
return wc.Close()
|
|
|
|
}
|
|
|
|
func (g *GcsSink) UpdateEntry(key string, oldEntry *filer_pb.Entry, newParentPath string, newEntry *filer_pb.Entry, deleteIncludeChunks bool, signatures []int32) (foundExistingEntry bool, err error) {
|
|
return true, g.CreateEntry(key, newEntry, signatures)
|
|
}
|