recursive and directory based sharing

This commit is contained in:
Anand Babu (AB) Periasamy
2015-11-17 20:32:40 -08:00
parent 330cfe65bb
commit 2db255c276
5 changed files with 83 additions and 89 deletions
-29
View File
@@ -183,32 +183,3 @@ func getNewClient(urlStr string, auth hostConfig) (client.Client, *probe.Error)
}
return nil, errInitClient(urlStr).Trace()
}
// url2Client - convenience wrapper for getNewClient
func url2Client(urlStr string) (client.Client, *probe.Error) {
urlConfig, err := getHostConfig(urlStr)
if err != nil {
return nil, err.Trace(urlStr)
}
client, err := getNewClient(urlStr, urlConfig)
if err != nil {
return nil, err.Trace(urlStr)
}
return client, nil
}
func prefixExists(urlStr string) bool {
clnt, err := url2Client(urlStr)
if err != nil {
return false
}
isRecursive := true
isIncomplete := false
for entry := range clnt.List(isRecursive, isIncomplete) {
if entry.Err != nil {
return false
}
return true
}
return false
}
+31 -32
View File
@@ -17,8 +17,6 @@
package main
import (
"fmt"
"strings"
"time"
"github.com/minio/cli"
@@ -30,6 +28,10 @@ var (
Name: "help, h",
Usage: "Help of share download",
}
shareFlagRecursive = cli.BoolFlag{
Name: "recursive, r",
Usage: "Share all objects recursively.",
}
)
// Share documents via URL.
@@ -37,7 +39,7 @@ var shareDownload = cli.Command{
Name: "download",
Usage: "Generate URLs for download access.",
Action: mainShareDownload,
Flags: []cli.Flag{shareFlagExpire, shareFlagDownloadHelp},
Flags: []cli.Flag{shareFlagRecursive, shareFlagExpire, shareFlagDownloadHelp},
CustomHelpTemplate: `NAME:
mc share {{.Name}} - {{.Usage}}
@@ -48,14 +50,17 @@ OPTIONS:
{{range .Flags}}{{.}}
{{end}}
EXAMPLES:
1. Generate URL for sharing, with a default expiry of 7 days.
1. Share this object with 7 days default expiry.
$ mc share {{.Name}} https://s3.amazonaws.com/backup/2006-Mar-1/backup.tar.gz
2. Generate URL for sharing, with an expiry of 10 minutes.
2. Share this object with 10 minutes expiry.
$ mc share {{.Name}} --expire=10m https://s3.amazonaws.com/backup/2006-Mar-1/backup.tar.gz
3. Generate list of URLs for sharing a folder recursively, with an expiry of 5 days each.
$ mc share {{.Name}} --expire=120h https://s3.amazonaws.com/backup...
3. Share all objects under this folder with 5 days expiry.
$ mc share {{.Name}} --expire=120h https://s3.amazonaws.com/backup/
4. Share all objects under this folder and all its sub-folders with 5 days expiry.
$ mc share {{.Name}} --recursive --expire=120h https://s3.amazonaws.com/backup/
`,
}
@@ -66,16 +71,6 @@ func checkShareDownloadSyntax(ctx *cli.Context) {
cli.ShowCommandHelpAndExit(ctx, "download", 1) // last argument is exit code.
}
// Validate each argument.
for _, arg := range ctx.Args() {
if !isURLRecursive(arg) {
// Check if any folder arg requires recursive operator.
if strings.HasSuffix(arg, "/") {
fatalIf(errDummy().Trace(), fmt.Sprintf("To grant access to an entire folder, you may use ‘%s’.", arg+recursiveSeparator))
}
}
}
// Parse expiry.
expiry := shareDefaultExpiry
expireArg := ctx.String("expire")
@@ -92,10 +87,18 @@ func checkShareDownloadSyntax(ctx *cli.Context) {
if expiry.Seconds() > 604800 {
fatalIf(errDummy().Trace(expiry.String()), "Expiry cannot be larger than 7 days.")
}
for _, arg := range ctx.Args() {
config := mustGetMcConfig()
url := getAliasURL(arg, config.Aliases) // Expand alias.
_, _, err := url2Stat(url)
fatalIf(err.Trace(url), "Unable to stat ‘"+arg+"’.")
}
}
// doShareURL share files from target
func doShareDownloadURL(targetURL string, recursive bool, expiry time.Duration) *probe.Error {
func doShareDownloadURL(targetURL string, isRecursive bool, expiry time.Duration) *probe.Error {
clnt, err := url2Client(targetURL)
if err != nil {
return err.Trace()
@@ -111,7 +114,7 @@ func doShareDownloadURL(targetURL string, recursive bool, expiry time.Duration)
// Generate share URL for each target.
incomplete := false
for content := range clnt.List(recursive, incomplete) {
for content := range clnt.List(isRecursive, incomplete) {
if content.Err != nil {
return content.Err.Trace(clnt.GetURL().String())
}
@@ -160,21 +163,17 @@ func mainShareDownload(ctx *cli.Context) {
// Extract arguments.
config := mustGetMcConfig()
isRecursive := ctx.Bool("recursive")
expiry := shareDefaultExpiry
if ctx.String("expire") != "" {
var e error
expiry, e = time.ParseDuration(ctx.String("expire"))
fatalIf(probe.NewError(e), "Unable to parse expire=‘"+ctx.String("expire")+"’.")
}
for _, arg := range ctx.Args() {
// if recursive strip off the "..."
url := stripRecursiveURL(arg)
isRecursive := isURLRecursive(arg)
expiry := shareDefaultExpiry
expireArg := ctx.String("expire")
if expireArg != "" {
var e error
expiry, e = time.ParseDuration(expireArg)
fatalIf(probe.NewError(e), "Unable to parse expire=‘"+expireArg+"’.")
}
for _, url := range ctx.Args() {
targetURL := getAliasURL(url, config.Aliases) // Expand alias.
err := doShareDownloadURL(targetURL, isRecursive, expiry)
errorIf(err.Trace(targetURL), "Unable to share target ‘"+arg+"’.")
errorIf(err.Trace(targetURL), "Unable to share target ‘"+url+"’.")
}
}
+22 -27
View File
@@ -18,7 +18,6 @@ package main
import (
"fmt"
"strings"
"time"
"github.com/minio/cli"
@@ -67,19 +66,10 @@ func checkShareUploadSyntax(ctx *cli.Context) {
cli.ShowCommandHelpAndExit(ctx, "upload", 1) // last argument is exit code.
}
// Validate each argument.
for _, arg := range ctx.Args() {
if !isURLRecursive(arg) {
// Check if any folder arg requires recursive operator.
if strings.HasSuffix(arg, "/") {
fatalIf(errDummy().Trace(), fmt.Sprintf("To grant access to an entire folder, you may use ‘%s’.", arg+recursiveSeparator))
}
}
}
// Parse expiry.
expireArg := ctx.String("expire")
// isRecursive := ctx.Bool("recursive")
expiry := shareDefaultExpiry
expireArg := ctx.String("expire")
if expireArg != "" {
var e error
expiry, e = time.ParseDuration(expireArg)
@@ -93,6 +83,14 @@ func checkShareUploadSyntax(ctx *cli.Context) {
if expiry.Seconds() > 604800 {
fatalIf(errDummy().Trace(expiry.String()), "Expiry cannot be larger than 7 days.")
}
for _, arg := range ctx.Args() {
config := mustGetMcConfig()
url := getAliasURL(arg, config.Aliases) // Expand alias.
_, _, err := url2Stat(url)
fatalIf(err.Trace(url), "Unable to stat ‘"+arg+"’.")
}
}
// makeCurlCmd constructs curl command-line.
@@ -169,23 +167,20 @@ func mainShareUpload(ctx *cli.Context) {
// check input arguments.
checkShareUploadSyntax(ctx)
for _, arg := range ctx.Args() {
config := mustGetMcConfig()
// if recursive strip off the "..."
url := stripRecursiveURL(arg)
isRecursive := isURLRecursive(arg)
expireArg := ctx.String("expire")
expiry := shareDefaultExpiry
contentType := ctx.String("content-type")
if expireArg != "" {
var e error
expiry, e = time.ParseDuration(expireArg)
fatalIf(probe.NewError(e), "Unable to parse expire=‘"+expireArg+"’.")
}
isRecursive := ctx.Bool("recursive")
config := mustGetMcConfig()
expireArg := ctx.String("expire")
expiry := shareDefaultExpiry
contentType := ctx.String("content-type")
if expireArg != "" {
var e error
expiry, e = time.ParseDuration(expireArg)
fatalIf(probe.NewError(e), "Unable to parse expire=‘"+expireArg+"’.")
}
for _, url := range ctx.Args() {
targetURL := getAliasURL(url, config.Aliases)
err := doShareUploadURL(targetURL, isRecursive, expiry, contentType)
fatalIf(err.Trace(targetURL), "Unable to generate curl command for upload ‘"+arg+"’.")
fatalIf(err.Trace(targetURL), "Unable to generate curl command for upload ‘"+url+"’.")
}
}
-1
View File
@@ -42,7 +42,6 @@ var (
Name: "content-type, T",
Usage: "Speific content-type to allow.",
}
shareFlagExpire = cli.StringFlag{
Name: "expire, E",
Value: "168h",
+30
View File
@@ -68,6 +68,19 @@ func args2URLs(args []string) ([]string, *probe.Error) {
return URLs, nil
}
// url2Client - convenience wrapper for getNewClient
func url2Client(urlStr string) (client.Client, *probe.Error) {
urlConfig, err := getHostConfig(urlStr)
if err != nil {
return nil, err.Trace(urlStr)
}
client, err := getNewClient(urlStr, urlConfig)
if err != nil {
return nil, err.Trace(urlStr)
}
return client, nil
}
// url2Stat returns stat info for URL.
func url2Stat(urlStr string) (client client.Client, content *client.Content, err *probe.Error) {
client, err = url2Client(urlStr)
@@ -105,3 +118,20 @@ func url2DirContent(urlStr string) (content *client.Content, err *probe.Error) {
content.Type = os.ModeDir
return content, nil
}
// Check if object key prefix exists
func prefixExists(urlStr string) bool {
clnt, err := url2Client(urlStr)
if err != nil {
return false
}
isRecursive := true
isIncomplete := false
for entry := range clnt.List(isRecursive, isIncomplete) {
if entry.Err != nil {
return false
}
return true
}
return false
}