cloudwatcher

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 13, 2021 License: GPL-3.0 Imports: 19 Imported by: 1

README

Cloudwatcher

GitHub

File system notification for Cloud platforms (and not) in Golang.

cloudwatcher is a file system notification library for cloud platforms (and not) in Go. Currently it implements the watchers for the following services:

  • Amazon S3
  • Google Drive
  • Dropbox
  • local filesystem (fsnotify/polling)

It is possible specify the directory to monitor and the polling time (how much often the watcher should check that directory), and every time a new file is added, removed or changed, an event is generated and sent to the Events channel.

Usage

package main

import (
	"fmt"
	"time"

	"github.com/Matrix86/cloudwatcher"
)

func main() {
	s, err := cloudwatcher.New("local", "/home/user/tests", time.Second)
	if err != nil {
		fmt.Printf("ERROR: %s", err)
		return
	}

	config := map[string]string{
		"disable_fsnotify": "false",
	}

	err = s.SetConfig(config)
	if err != nil {
		fmt.Printf("ERROR: %s", err)
		return
	}

	err = s.Start()
	defer s.Close()
	for {
		select {
		case v := <-s.GetEvents():
			fmt.Printf("EVENT: %s %s\n", v.Key, v.TypeString())

		case e := <-s.GetErrors():
			fmt.Printf("ERROR: %s\n", e)
		}
	}
}

The channel returned by GetEvents() function, will return an Event struct that contains the event type, the Key with the name of the file that generates the event and the object itself.

⚠ check the Event.Object field before use it...in some cases it could be nil (FileDelete event with fsnotify)

Amazon S3

The config of the S3 watcher is the following:

config := map[string]string{
    "bucket_name": "storage",
    "endpoint":   "s3-us-west-2.amazonaws.com",
    "access_key":  "user",
    "secret_key":  "secret",
    "token":      "",
    "region":     "us-west-2",
    "ssl_enabled": "true",
}

An example can be found here.

💎 minio can be used for testing purposes

Google Drive

In order to use it you need to enable the drive API from here . After that you can specify the client-id and the client-secret on the config file. The logic to retrieve the token has to be handled outside the library and the token field should contain the json.

You can find an example in the examples directory.

config := map[string]string{
    "debug":         "true",
    "token":         Token,
    "client_id":     ClientId,
    "client_secret": ClientSecret,
}

Dropbox

First, you need to register a new app from the developer console. Use the client-id and the client-secret to retrieve the user token and use it on the token field of the config.

You can find an example in the examples directory.

config := map[string]string{
    "debug": "true",
    "token": Token,
}

Local filesystem

It is based on fsnotify library, so it is cross platform: Windows, Linux, BSD and macOS. It is not mandatory to call the SetConfig() function, and the polling time argument of cloudwatcher.New is not used.

Setting disable_fsnotify parameter on config to "true" the watcher doesn't use fsnotify and use the listing approach instead.

⚠ not set disable_fsnotify to "true" if you plan to use it on a big directory!!! It could increase the I/O on disk

Documentation

Index

Constants

View Source
const (
	FileCreated = iota
	FileChanged
	FileDeleted
	TagsChanged
)

event's types

Variables

This section is empty.

Functions

This section is empty.

Types

type Bool

type Bool bool

Bool is an alias for the standard boolean type with Unmarshal/Marshal functions

func (Bool) MarshalJSON

func (b Bool) MarshalJSON() ([]byte, error)

MarshalJSON allows the conversion from bool to string

func (*Bool) UnmarshalJSON

func (b *Bool) UnmarshalJSON(s []byte) error

UnmarshalJSON allows to unmarshal from string to bool

type DropboxObject

type DropboxObject struct {
	Key          string
	Size         int64
	LastModified time.Time
	Hash         string
}

DropboxObject is the object that contains the info of the file

type DropboxWatcher

type DropboxWatcher struct {
	WatcherBase
	// contains filtered or unexported fields
}

DropboxWatcher is the specialized watcher for Dropbox service

func (*DropboxWatcher) Close

func (w *DropboxWatcher) Close()

Close stop the polling process

func (*DropboxWatcher) SetConfig

func (w *DropboxWatcher) SetConfig(m map[string]string) error

SetConfig is used to configure the DropboxWatcher

func (*DropboxWatcher) Start

func (w *DropboxWatcher) Start() error

Start launches the polling process

type Event

type Event struct {
	Key    string      // Path of file
	Type   Op          // File operation
	Object interface{} // Object pointer
}

Event is the struct that contains the info about the changed file

func (*Event) TypeString

func (e *Event) TypeString() string

TypeString returns a text version of the event's type

type GDriveObject

type GDriveObject struct {
	ID           string
	Key          string
	Size         int64
	LastModified time.Time
	Hash         string
}

GDriveObject is the object that contains the info of the file

type GDriveWatcher

type GDriveWatcher struct {
	WatcherBase
	// contains filtered or unexported fields
}

GDriveWatcher is the specialized watcher for Google Drive service

func (*GDriveWatcher) Close

func (w *GDriveWatcher) Close()

Close stop the polling process

func (*GDriveWatcher) SetConfig

func (w *GDriveWatcher) SetConfig(m map[string]string) error

SetConfig is used to configure the GDriveWatcher

func (*GDriveWatcher) Start

func (w *GDriveWatcher) Start() error

Start launches the polling process

type IMinio

type IMinio interface {
	BucketExists(ctx context.Context, bucketName string) (bool, error)
	GetObjectTagging(ctx context.Context, bucketName, objectName string, opts minio.GetObjectTaggingOptions) (*tags.Tags, error)
	ListObjects(ctx context.Context, bucketName string, opts minio.ListObjectsOptions) <-chan minio.ObjectInfo
}

IMinio is an interface I used only for gomock

type LocalObject

type LocalObject struct {
	Key          string
	Size         int64
	LastModified time.Time
	FileMode     os.FileMode
}

LocalObject is the object that contains the info of the file

type LocalWatcher

type LocalWatcher struct {
	WatcherBase
	// contains filtered or unexported fields
}

LocalWatcher is the specialized watcher for the local FS

func (*LocalWatcher) Close

func (w *LocalWatcher) Close()

Close stop the polling process

func (*LocalWatcher) SetConfig

func (w *LocalWatcher) SetConfig(m map[string]string) error

SetConfig is used to configure the LocalWatcher

func (*LocalWatcher) Start

func (w *LocalWatcher) Start() error

Start launches the polling process

type Op

type Op uint32

Op defines the event's type

type S3Object

type S3Object struct {
	Key          string
	Etag         string
	Size         int64
	Tags         map[string]string
	LastModified time.Time
}

S3Object is the object that contains the info of the file

type S3Watcher

type S3Watcher struct {
	WatcherBase
	// contains filtered or unexported fields
}

S3Watcher is the specialized watcher for Amazon S3 service

func (*S3Watcher) Close

func (u *S3Watcher) Close()

Close stop the polling process

func (*S3Watcher) SetConfig

func (u *S3Watcher) SetConfig(m map[string]string) error

SetConfig is used to configure the S3Watcher

func (*S3Watcher) Start

func (u *S3Watcher) Start() error

Start launches the polling process

type Watcher

type Watcher interface {
	Start() error
	SetConfig(c map[string]string) error
	Close()
	GetEvents() chan Event
	GetErrors() chan error
}

Watcher has to be implemented by all the watchers

func New

func New(serviceName string, dir string, interval time.Duration) (Watcher, error)

New creates a new instance of a watcher

type WatcherBase

type WatcherBase struct {
	Events chan Event
	Errors chan error
	// contains filtered or unexported fields
}

WatcherBase is the struct included in all the specialized watchers

func (*WatcherBase) GetErrors

func (w *WatcherBase) GetErrors() chan error

GetErrors returns a chan of error

func (*WatcherBase) GetEvents

func (w *WatcherBase) GetEvents() chan Event

GetEvents returns a chan of Event

Directories

Path Synopsis
examples
s3
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL