2022-09-21 23:09:50 +08:00
package s3
import (
"context"
2023-11-14 00:40:34 +08:00
_ "embed"
2024-04-05 19:27:33 +08:00
"strings"
2022-09-21 23:09:50 +08:00
"github.com/rclone/rclone/cmd"
2024-07-17 22:14:08 +08:00
"github.com/rclone/rclone/cmd/serve/proxy/proxyflags"
"github.com/rclone/rclone/fs"
2022-09-21 23:09:50 +08:00
"github.com/rclone/rclone/fs/config/flags"
"github.com/rclone/rclone/fs/hash"
httplib "github.com/rclone/rclone/lib/http"
"github.com/rclone/rclone/vfs"
"github.com/rclone/rclone/vfs/vfsflags"
"github.com/spf13/cobra"
)
// DefaultOpt is the default values used for Options
var DefaultOpt = Options {
pathBucketMode : true ,
hashName : "MD5" ,
hashType : hash . MD5 ,
noCleanup : false ,
2024-07-17 22:14:08 +08:00
Auth : httplib . DefaultAuthCfg ( ) ,
2022-09-21 23:09:50 +08:00
HTTP : httplib . DefaultCfg ( ) ,
}
// Opt is options set by command line flags
var Opt = DefaultOpt
const flagPrefix = ""
func init ( ) {
flagSet := Command . Flags ( )
2024-07-17 22:14:08 +08:00
httplib . AddAuthFlagsPrefix ( flagSet , flagPrefix , & Opt . Auth )
2022-09-21 23:09:50 +08:00
httplib . AddHTTPFlagsPrefix ( flagSet , flagPrefix , & Opt . HTTP )
vfsflags . AddFlags ( flagSet )
2024-07-17 22:14:08 +08:00
proxyflags . AddFlags ( flagSet )
2023-06-23 12:07:50 +08:00
flags . BoolVarP ( flagSet , & Opt . pathBucketMode , "force-path-style" , "" , Opt . pathBucketMode , "If true use path style access if false use virtual hosted style (default true)" , "" )
flags . StringVarP ( flagSet , & Opt . hashName , "etag-hash" , "" , Opt . hashName , "Which hash to use for the ETag, or auto or blank for off" , "" )
2023-11-14 00:40:34 +08:00
flags . StringArrayVarP ( flagSet , & Opt . authPair , "auth-key" , "" , Opt . authPair , "Set key pair for v4 authorization: access_key_id,secret_access_key" , "" )
2023-06-23 12:07:50 +08:00
flags . BoolVarP ( flagSet , & Opt . noCleanup , "no-cleanup" , "" , Opt . noCleanup , "Not to cleanup empty folder after object is deleted" , "" )
2022-09-21 23:09:50 +08:00
}
2023-11-14 00:40:34 +08:00
//go:embed serve_s3.md
var serveS3Help string
2024-04-05 19:27:33 +08:00
// help returns the help string cleaned up to simplify appending
func help ( ) string {
return strings . TrimSpace ( serveS3Help ) + "\n\n"
}
2022-09-21 23:09:50 +08:00
// Command definition for cobra
var Command = & cobra . Command {
2023-11-14 00:40:34 +08:00
Annotations : map [ string ] string {
"versionIntroduced" : "v1.65" ,
"groups" : "Filter" ,
"status" : "Experimental" ,
} ,
2022-09-21 23:09:50 +08:00
Use : "s3 remote:path" ,
Short : ` Serve remote:path over s3. ` ,
2024-07-17 22:14:08 +08:00
Long : help ( ) + httplib . AuthHelp ( flagPrefix ) + httplib . Help ( flagPrefix ) + vfs . Help ( ) ,
2022-09-21 23:09:50 +08:00
RunE : func ( command * cobra . Command , args [ ] string ) error {
2024-07-17 22:14:08 +08:00
var f fs . Fs
if proxyflags . Opt . AuthProxy == "" {
cmd . CheckArgs ( 1 , 1 , command , args )
f = cmd . NewFsSrc ( args )
} else {
cmd . CheckArgs ( 0 , 0 , command , args )
}
2022-09-21 23:09:50 +08:00
if Opt . hashName == "auto" {
Opt . hashType = f . Hashes ( ) . GetOne ( )
} else if Opt . hashName != "" {
err := Opt . hashType . Set ( Opt . hashName )
if err != nil {
return err
}
}
cmd . Run ( false , false , command , func ( ) error {
s , err := newServer ( context . Background ( ) , f , & Opt )
if err != nil {
return err
}
2024-07-17 22:14:08 +08:00
router := s . server . Router ( )
2022-09-21 23:09:50 +08:00
s . Bind ( router )
2024-07-17 22:14:08 +08:00
err = s . Serve ( )
2023-09-12 18:50:13 +08:00
if err != nil {
return err
}
2024-07-17 22:14:08 +08:00
s . server . Wait ( )
2022-09-21 23:09:50 +08:00
return nil
} )
return nil
} ,
}