seafile_recovery/s3_walker.go

78 lines
1.8 KiB
Go
Raw Normal View History

2021-04-26 15:21:03 +00:00
package main
import (
2021-04-26 16:39:54 +00:00
"context"
"log"
"mime"
"net/url"
"path/filepath"
2021-04-26 17:46:42 +00:00
"strings"
2021-04-26 16:39:54 +00:00
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
2021-04-26 15:21:03 +00:00
)
2021-04-26 16:39:54 +00:00
type S3Walker struct {
ctx context.Context
config *configCollect
mc *minio.Client
bucket string
2021-04-26 17:46:42 +00:00
pathPrefix string
2021-04-26 16:39:54 +00:00
}
func NewS3Walker(config *configCollect) *S3Walker {
sw := new(S3Walker)
sw.ctx = context.Background()
sw.config = config
u, err := url.Parse(config.Dest)
if err != nil {
log.Fatal(err)
}
2021-04-26 17:46:42 +00:00
if u.Scheme != "s3" { log.Fatal("URL must be of the following form: s3://ACCESS_KEY:SECRET_KEY@ENDPOINT/REGION/BUCKET[/PREFIX") }
2021-04-26 16:39:54 +00:00
accessKeyID := u.User.Username()
secretAccessKey, _ := u.User.Password()
endpoint := u.Host
2021-04-26 17:46:42 +00:00
splittedPath := strings.SplitN(u.Path, "/", 4)
if len(splittedPath) < 3 {
log.Fatal("Bucket or region not found")
}
region := splittedPath[1]
sw.bucket = splittedPath[2]
if len(splittedPath) > 3 {
sw.pathPrefix = splittedPath[3]
}
2021-04-26 16:39:54 +00:00
useSSL := true
// Initialize minio client object.
sw.mc, err = minio.New(endpoint, &minio.Options{
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
Secure: useSSL,
2021-04-26 17:46:42 +00:00
Region: region,
2021-04-26 16:39:54 +00:00
})
if err != nil {
log.Fatal(err)
}
2021-04-26 17:46:42 +00:00
//sw.mc.TraceOn(nil)
2021-04-26 16:39:54 +00:00
return sw
}
2021-04-26 15:21:03 +00:00
func (sw* S3Walker) onDir(dn *DirNode) {
}
func (sw* S3Walker) onFile(fn *FileNode) {
2021-04-26 16:39:54 +00:00
fn.Parse()
2021-04-26 17:46:42 +00:00
path := filepath.Join(sw.pathPrefix, fn.AbsolutePath)
contentType := mime.TypeByExtension(filepath.Ext(path))
info, err := sw.mc.PutObject(sw.ctx, sw.bucket, path, fn, int64(fn.Elem.FileSize), minio.PutObjectOptions{ ContentType: contentType })
2021-04-26 16:39:54 +00:00
if err != nil {
log.Fatalln(err)
} else if info.Size != int64(fn.Elem.FileSize) {
log.Fatal("Uploaded",info.Size,"bytes but expected", fn.Elem.FileSize,"bytes")
}
log.Println(fn.String())
2021-04-26 15:21:03 +00:00
}