mc retention: add --recursive and --bypass flags. (#3100)
--bypass flag allows overriding governance if user has override permissions
This commit is contained in:
+1
-1
@@ -946,7 +946,7 @@ func (f *fsClient) GetAccessRules() (map[string]string, *probe.Error) {
|
||||
}
|
||||
|
||||
// Set object retention for a given object.
|
||||
func (f *fsClient) PutObjectRetention(mode *minio.RetentionMode, retainUntilDate *time.Time) *probe.Error {
|
||||
func (f *fsClient) PutObjectRetention(mode *minio.RetentionMode, retainUntilDate *time.Time, bypassGovernance bool) *probe.Error {
|
||||
return probe.NewError(APINotImplemented{
|
||||
API: "PutObjectRetention",
|
||||
APIType: "filesystem",
|
||||
|
||||
+4
-3
@@ -2074,12 +2074,13 @@ func (c *s3Client) SetObjectLockConfig(mode *minio.RetentionMode, validity *uint
|
||||
}
|
||||
|
||||
// Set object retention for a given object.
|
||||
func (c *s3Client) PutObjectRetention(mode *minio.RetentionMode, retainUntilDate *time.Time) *probe.Error {
|
||||
func (c *s3Client) PutObjectRetention(mode *minio.RetentionMode, retainUntilDate *time.Time, bypassGovernance bool) *probe.Error {
|
||||
bucket, object := c.url2BucketAndObject()
|
||||
|
||||
opts := minio.PutObjectRetentionOptions{
|
||||
RetainUntilDate: retainUntilDate,
|
||||
Mode: mode,
|
||||
RetainUntilDate: retainUntilDate,
|
||||
Mode: mode,
|
||||
GovernanceBypass: bypassGovernance,
|
||||
}
|
||||
err := c.api.PutObjectRetention(bucket, object, opts)
|
||||
if err != nil {
|
||||
|
||||
+1
-1
@@ -68,7 +68,7 @@ type Client interface {
|
||||
Get(sse encrypt.ServerSide) (reader io.ReadCloser, err *probe.Error)
|
||||
Put(ctx context.Context, reader io.Reader, size int64, metadata map[string]string, progress io.Reader, sse encrypt.ServerSide) (n int64, err *probe.Error)
|
||||
// Object Locking related API
|
||||
PutObjectRetention(mode *minio.RetentionMode, retainUntilDate *time.Time) *probe.Error
|
||||
PutObjectRetention(mode *minio.RetentionMode, retainUntilDate *time.Time, bypassGovernance bool) *probe.Error
|
||||
|
||||
// I/O operations with expiration
|
||||
ShareDownload(expires time.Duration) (string, *probe.Error)
|
||||
|
||||
@@ -234,7 +234,7 @@ func putTargetRetention(ctx context.Context, alias string, urlStr string, metada
|
||||
retainUntilDate = t.UTC()
|
||||
}
|
||||
}
|
||||
if err := targetClnt.PutObjectRetention(&lockMode, &retainUntilDate); err != nil {
|
||||
if err := targetClnt.PutObjectRetention(&lockMode, &retainUntilDate, false); err != nil {
|
||||
return err.Trace(alias, urlStr)
|
||||
}
|
||||
return nil
|
||||
|
||||
+26
-9
@@ -31,12 +31,24 @@ import (
|
||||
"github.com/minio/minio/pkg/console"
|
||||
)
|
||||
|
||||
var (
|
||||
rFlags = []cli.Flag{
|
||||
cli.BoolFlag{
|
||||
Name: "recursive, r",
|
||||
Usage: "apply retention recursively",
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "bypass",
|
||||
Usage: "bypass governance",
|
||||
},
|
||||
}
|
||||
)
|
||||
var retentionCmd = cli.Command{
|
||||
Name: "retention",
|
||||
Usage: "set object retention for objects with a given prefix",
|
||||
Action: mainRetention,
|
||||
Before: setGlobalsFromContext,
|
||||
Flags: globalFlags,
|
||||
Flags: append(rFlags, globalFlags...),
|
||||
CustomHelpTemplate: `NAME:
|
||||
{{.HelpName}} - {{.Usage}}
|
||||
|
||||
@@ -50,9 +62,13 @@ VALIDITY:
|
||||
This argument must be formatted like Nd or Ny where 'd' denotes days and 'y' denotes years e.g. 10d, 3y.
|
||||
|
||||
EXAMPLES:
|
||||
1. Set object retention for objects in a given prefix
|
||||
$ {{.HelpName}} myminio/mybucket/prefix compliance 30d
|
||||
`,
|
||||
1. Set object retention for a specific object
|
||||
$ {{.HelpName}} myminio/mybucket/prefix/obj.csv compliance 30d
|
||||
|
||||
2. Set object retention for objects in a given prefix
|
||||
$ {{.HelpName}} myminio/mybucket/prefix compliance 30d --recursive
|
||||
|
||||
`,
|
||||
}
|
||||
|
||||
// Structured message depending on the type of console.
|
||||
@@ -80,7 +96,7 @@ func (m retentionCmdMessage) JSON() string {
|
||||
}
|
||||
|
||||
// setRetention - Set Retention for all objects within a given prefix.
|
||||
func setRetention(urlStr string, mode *minio.RetentionMode, validity *uint, unit *minio.ValidityUnit) error {
|
||||
func setRetention(urlStr string, mode *minio.RetentionMode, validity *uint, unit *minio.ValidityUnit, bypassGovernance, isRecursive bool) error {
|
||||
clnt, err := newClient(urlStr)
|
||||
if err != nil {
|
||||
fatalIf(err.Trace(), "Cannot parse the provided url.")
|
||||
@@ -93,6 +109,7 @@ func setRetention(urlStr string, mode *minio.RetentionMode, validity *uint, unit
|
||||
}
|
||||
|
||||
alias, _, _ := mustExpandAlias(urlStr)
|
||||
|
||||
retainUntilDate := func() (time.Time, error) {
|
||||
if validity == nil {
|
||||
return timeSentinel, fmt.Errorf("invalid validity '%v'", validity)
|
||||
@@ -128,7 +145,7 @@ func setRetention(urlStr string, mode *minio.RetentionMode, validity *uint, unit
|
||||
|
||||
var cErr error
|
||||
errorsFound := false
|
||||
for content := range clnt.List(true, false, false, DirNone) {
|
||||
for content := range clnt.List(isRecursive, false, false, DirNone) {
|
||||
if content.Err != nil {
|
||||
errorIf(content.Err.Trace(clnt.GetURL().String()), "Unable to list folder.")
|
||||
cErr = exitStatus(globalErrorExitStatus) // Set the exit status.
|
||||
@@ -144,7 +161,7 @@ func setRetention(urlStr string, mode *minio.RetentionMode, validity *uint, unit
|
||||
errorIf(content.Err.Trace(clnt.GetURL().String()), "Invalid URL")
|
||||
continue
|
||||
}
|
||||
probeErr := newClnt.PutObjectRetention(mode, &retainUntil)
|
||||
probeErr := newClnt.PutObjectRetention(mode, &retainUntil, bypassGovernance)
|
||||
if probeErr != nil {
|
||||
errorsFound = true
|
||||
printMsg(retentionCmdMessage{
|
||||
@@ -169,7 +186,7 @@ func setRetention(urlStr string, mode *minio.RetentionMode, validity *uint, unit
|
||||
if errorsFound {
|
||||
console.Print(console.Colorize("RetentionPartialFailure", fmt.Sprintf("Errors found while setting retention on objects with prefix `%s`.\n", urlStr)))
|
||||
} else {
|
||||
console.Print(console.Colorize("RetentionSuccess", fmt.Sprintf("Object retention successfully set for prefix `%s`.\n", urlStr)))
|
||||
console.Print(console.Colorize("RetentionSuccess", fmt.Sprintf("Object retention successfully set for `%s`.\n", urlStr)))
|
||||
}
|
||||
}
|
||||
return cErr
|
||||
@@ -221,5 +238,5 @@ func mainRetention(ctx *cli.Context) error {
|
||||
default:
|
||||
cli.ShowCommandHelpAndExit(ctx, "retention", 1)
|
||||
}
|
||||
return setRetention(urlStr, mode, validity, unit)
|
||||
return setRetention(urlStr, mode, validity, unit, ctx.Bool("bypass"), ctx.Bool("recursive"))
|
||||
}
|
||||
|
||||
@@ -603,15 +603,17 @@ USAGE:
|
||||
mc retention [FLAGS] TARGET [governance | compliance] [VALIDITY]
|
||||
|
||||
FLAGS:
|
||||
--bypass bypass governance
|
||||
--recursive, -r apply retention recursively
|
||||
--json enable JSON formatted output
|
||||
--help, -h show help
|
||||
```
|
||||
|
||||
*Example: Set governance for 30 days for objects with prefix `prefix` on bucket `mybucket`*
|
||||
*Example: Set governance for 30 days for object `prefix` on bucket `mybucket`*
|
||||
|
||||
```
|
||||
mc retention myminio/mybucket/prefix governance 30d
|
||||
Object retention successfully set for prefix `myminio/mybucket/prefix`.
|
||||
mc retention myminio/mybucket/prefix governance 30d -r
|
||||
Object retention successfully set for objects with prefix `myminio/mybucket/prefix`.
|
||||
|
||||
```
|
||||
*Objects created with prefix `prefix` in the above bucket `mybucket` cannot be deleted until the compliance period is over*
|
||||
|
||||
Reference in New Issue
Block a user