Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help with running embedded with S3 Sync #2018

Open
heynemann opened this issue Sep 2, 2024 · 3 comments
Open

Help with running embedded with S3 Sync #2018

heynemann opened this issue Sep 2, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@heynemann
Copy link

What would you like to be added or enhanced

I'm trying to understand how to run in embedded mode with S3 storage sync. Haven't found any examples in doc and been trying to understand the codebase for it, but it's been hard. I've managed to get the store to work locally and managed to create an S3 Remote Storage but haven't found how to plug em.

Why is this needed

To run immudb embedded with S3 sync.

Additional context

@heynemann heynemann added the enhancement New feature or request label Sep 2, 2024
@ostafen
Copy link
Collaborator

ostafen commented Sep 2, 2024

Hi, @heynemann, thanks for the feedback.
I'll get back to you shortly with a code snippet for running immudb with remote S3 storage.

@heynemann
Copy link
Author

Any news @ostafen ?

@ostafen
Copy link
Collaborator

ostafen commented Sep 12, 2024

Hi, @heynemann, the following snippet should fit your scenario:

package main

import (
	"context"
	"log"
	"os"
	"path/filepath"
	"strings"

	"github.com/codenotary/immudb/embedded/appendable"
	"github.com/codenotary/immudb/embedded/appendable/multiapp"
	"github.com/codenotary/immudb/embedded/appendable/remoteapp"
	"github.com/codenotary/immudb/embedded/remotestorage/s3"
	"github.com/codenotary/immudb/embedded/store"
)

func getS3RemotePath(rootPath, subPath string) string {
	fsPath := filepath.Join(rootPath, subPath)

	return strings.ReplaceAll(
		fsPath+"/",
		string(filepath.Separator), "/",
	)
}

type Options struct {
	S3Endpoint            string
	S3RoleEnabled         bool
	S3Role                string
	S3AccessKeyID         string
	S3SecretKey           string
	S3BucketName          string
	S3Location            string
	S3PathPrefix          string
	S3InstanceMetadataURL string
}

func main() {
	var s3Opts Options // set your s3 options

	remoteStorage, err := s3.Open(
		s3Opts.S3Endpoint,
		s3Opts.S3RoleEnabled,
		s3Opts.S3Role,
		s3Opts.S3AccessKeyID,
		s3Opts.S3SecretKey,
		s3Opts.S3BucketName,
		s3Opts.S3Location,
		s3Opts.S3PathPrefix,
		s3Opts.S3InstanceMetadataURL,
	)
	if err != nil {
		panic(err)
	}

	opts := store.DefaultOptions()

	opts.WithAppFactory(func(rootPath, subPath string, opts *multiapp.Options) (appendable.Appendable, error) {
		remoteAppOpts := remoteapp.DefaultOptions()
		remoteAppOpts.Options = *opts

		s3Path := getS3RemotePath(rootPath, subPath)
		return remoteapp.Open(
			filepath.Join(rootPath, subPath),
			s3Path,
			remoteStorage,
			remoteAppOpts,
		)
	}).
		WithFileSize(1 << 20). // Reduce file size for better cache granularity
		WithAppRemoveFunc(func(rootPath, subPath string) error {
			s3Path := getS3RemotePath(rootPath, subPath)

			err = os.RemoveAll(filepath.Join(rootPath, subPath))
			if err != nil {
				return err
			}
			return remoteStorage.RemoveAll(context.Background(), s3Path)
		})

	store, err := store.Open("./data", opts)
	if err != nil {
		log.Fatal(err)
	}
	defer store.Close()
       // use your store
}

Let me know if you need further assistance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants