Skip to content

Commit

Permalink
add arg builder to objectRetention API
Browse files Browse the repository at this point in the history
  • Loading branch information
sinhaashish committed May 24, 2020
1 parent a92c2db commit a9e13be
Show file tree
Hide file tree
Showing 8 changed files with 414 additions and 197 deletions.
2 changes: 1 addition & 1 deletion api/src/main/java/io/minio/GetObjectRetentionArgs.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package io.minio;

/** Argument class of MinioClient.getDefaultRetention(). */
/** Argument class of MinioClient.getObjectRetention(). */
public class GetObjectRetentionArgs extends ObjectArgs {
public static Builder builder() {
return new Builder();
Expand Down
43 changes: 29 additions & 14 deletions api/src/main/java/io/minio/MinioClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -3811,7 +3811,7 @@ public ObjectLockConfiguration getDefaultRetention(String bucketName)
* @param objectName Object name in the bucket.
* @param versionId Version ID of the object.
* @param config Object retention configuration.
* @param bypassGovernanceRetention Bypass Governance retention.
* @param bypassGovernanceMode Bypass Governance retention.
* @throws ErrorResponseException thrown to indicate S3 service returned an error response.
* @throws IllegalArgumentException throws to indicate invalid argument passed.
* @throws InsufficientDataException thrown to indicate not enough data available in InputStream.
Expand All @@ -3830,7 +3830,7 @@ public void setObjectRetention(
String objectName,
String versionId,
Retention config,
boolean bypassGovernanceRetention)
boolean bypassGovernanceMode)
throws ErrorResponseException, IllegalArgumentException, InsufficientDataException,
InternalException, InvalidBucketNameException, InvalidKeyException,
InvalidResponseException, IOException, NoSuchAlgorithmException, XmlParserException {
Expand All @@ -3841,17 +3841,23 @@ public void setObjectRetention(
.object(objectName)
.versionId(versionId)
.config(config)
.bypassGovernanceRetention(bypassGovernanceRetention)
.bypassGovernanceMode(bypassGovernanceMode)
.build());
}

/**
* Sets retention configuration to an object.
*
* <pre>Example:{@code
* Retention retention =
* new Retention(RetentionMode.COMPLIANCE, ZonedDateTime.now().plusYears(1));
* minioClient.setObjectRetention(SetObjectRetentionArgs args);
* Retention retention = new Retention(
* RetentionMode.COMPLIANCE, ZonedDateTime.now().plusYears(1));
* minioClient.setObjectRetention(
* SetObjectRetentionArgs.builder()
* .bucket("my-bucketname")
* .object("my-objectname")
* .config(config)
* .bypassGovernanceMode(true)
* .build());
* }</pre>
*
* @param args {@link SetObjectRetentionArgs} object.
Expand All @@ -3871,24 +3877,24 @@ public void setObjectRetention(SetObjectRetentionArgs args)
throws ErrorResponseException, IllegalArgumentException, InsufficientDataException,
InternalException, InvalidBucketNameException, InvalidKeyException,
InvalidResponseException, IOException, NoSuchAlgorithmException, XmlParserException {
if (args.config() == null) {
throw new IllegalArgumentException("null value is not allowed in config.");
if (args == null) {
throw new IllegalArgumentException("null arguments");
}

Map<String, String> queryParamMap = new HashMap<>();
queryParamMap.put("retention", "");

if (args.versionId() != null && !args.versionId().isEmpty()) {
if (args.versionId() != null) {
queryParamMap.put("versionId", args.versionId());
}

Map<String, String> headerMap = new HashMap<>();
if (args.bypassGovernanceRetention()) {
if (args.bypassGovernanceMode()) {
headerMap.put("x-amz-bypass-governance-retention", "True");
}

Response response =
executePut(args.bucket(), args.objectName(), headerMap, queryParamMap, args.config(), 0);
executePut(args.bucket(), args.object(), headerMap, queryParamMap, args.config(), 0);
response.body().close();
}

Expand Down Expand Up @@ -3918,6 +3924,7 @@ public void setObjectRetention(SetObjectRetentionArgs args)
* @throws NoSuchAlgorithmException thrown to indicate missing of MD5 or SHA-256 digest library.
* @throws XmlParserException thrown to indicate XML parsing error.
*/
@Deprecated
public Retention getObjectRetention(String bucketName, String objectName, String versionId)
throws ErrorResponseException, IllegalArgumentException, InsufficientDataException,
InternalException, InvalidBucketNameException, InvalidKeyException,
Expand All @@ -3935,7 +3942,11 @@ public Retention getObjectRetention(String bucketName, String objectName, String
*
* <pre>Example:{@code
* Retention retention =
* minioClient.getObjectRetention(GetObjectRetentionArgs args);
* minioClient.getObjectRetention(GetObjectRetentionArgs.builder()
* .bucket(bucketName)
* .object(objectName)
* .versionId(versionId)
* .build()););
* System.out.println(
* "mode: " + retention.mode() + "until: " + retention.retainUntilDate());
* }</pre>
Expand All @@ -3958,14 +3969,18 @@ public Retention getObjectRetention(GetObjectRetentionArgs args)
throws ErrorResponseException, IllegalArgumentException, InsufficientDataException,
InternalException, InvalidBucketNameException, InvalidKeyException,
InvalidResponseException, IOException, NoSuchAlgorithmException, XmlParserException {
if (args == null) {
throw new IllegalArgumentException("null arguments");
}

Map<String, String> queryParamMap = new HashMap<>();
queryParamMap.put("retention", "");

if (args.versionId() != null && !args.versionId().isEmpty()) {
if (args.versionId() != null) {
queryParamMap.put("versionId", args.versionId());
}

try (Response response = executeGet(args.bucket(), args.objectName(), null, queryParamMap)) {
try (Response response = executeGet(args.bucket(), args.object(), null, queryParamMap)) {
Retention retention = Xml.unmarshal(Retention.class, response.body().charStream());
return retention;
} catch (ErrorResponseException e) {
Expand Down
15 changes: 6 additions & 9 deletions api/src/main/java/io/minio/SetObjectRetentionArgs.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@

import io.minio.messages.Retention;

/** Argument class of MinioClient.setObjectRetention(). */
public class SetObjectRetentionArgs extends ObjectArgs {
private Retention config;
private boolean bypassGovernanceRetention;
private boolean bypassGovernanceMode;

public Retention config() {
return config;
}

public boolean bypassGovernanceRetention() {
return bypassGovernanceRetention;
public boolean bypassGovernanceMode() {
return bypassGovernanceMode;
}

public static Builder builder() {
Expand All @@ -37,16 +38,12 @@ public static Builder builder() {
/** Argument builder of {@link SetObjectRetentionArgs}. */
public static final class Builder extends ObjectArgs.Builder<Builder, SetObjectRetentionArgs> {
public Builder config(Retention config) {
if (config == null) {
throw new IllegalArgumentException("null object retention configuration");
}

operations.add(args -> args.config = config);
return this;
}

public Builder bypassGovernanceRetention(boolean bypassGovernanceRetention) {
operations.add(args -> args.bypassGovernanceRetention = bypassGovernanceRetention);
public Builder bypassGovernanceMode(boolean bypassGovernanceMode) {
operations.add(args -> args.bypassGovernanceMode = bypassGovernanceMode);
return this;
}
}
Expand Down
24 changes: 8 additions & 16 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -1064,26 +1064,18 @@ Gets retention configuration of an object.

__Parameters__

| Parameter | Type | Description |
|:---------------|:---------------------------|:---------------------------|
| ``args`` | _[GetObjectRetentionArgs]_ | Object retention arguments |
| Parameter | Type | Description |
|:---------------|:---------------------------|:--------------|
| ``args`` | _[GetObjectRetentionArgs]_ | Arguments. |

| Returns |
|:------------------------------------------------|
| _[Retention]_ - Object retention configuration. |

__Example__
```java
// Object without version
Retention retention =
minioClient.getObjectRetention(
GetObjectRetentionArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
.build());
System.out.println("mode: " + retention.mode() + "until: " + retention.retainUntilDate());

// Object with version
// Object with version id.
Retention retention =
minioClient.getObjectRetention(
GetObjectRetentionArgs.builder()
Expand Down Expand Up @@ -1473,9 +1465,9 @@ Sets retention configuration to an object.

__Parameters__

| Parameter | Type | Description |
|:-----------------|:---------------------------|:-----------------------------------|
| ``args`` | _[SetObjectRetentionArgs]_ | Arguments to set object retention. |
| Parameter | Type | Description |
|:-----------------|:---------------------------|:-------------|
| ``args`` | _[SetObjectRetentionArgs]_ | Arguments. |

__Example__
```java
Expand All @@ -1485,7 +1477,7 @@ minioClient.setObjectRetention(
.bucket("my-bucketname")
.object("my-objectname")
.config(retention)
.bypassGovernanceRetention(true)
.bypassGovernanceMode(true)
.build());
```

Expand Down
52 changes: 52 additions & 0 deletions examples/GetObjectRetention.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* MinIO Java SDK for Amazon S3 Compatible Cloud Storage, (C) 2020 MinIO, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import io.minio.GetObjectRetentionArgs;
import io.minio.MinioClient;
import io.minio.errors.MinioException;
import io.minio.messages.Retention;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public class GetObjectRetention {
/** MinioClient.getObjectRetention() example. */
public static void main(String[] args)
throws IOException, NoSuchAlgorithmException, InvalidKeyException, IllegalArgumentException {
try {

/* play.min.io for test and development. */
MinioClient minioClient =
new MinioClient(
"https://play.min.io",
"Q3AM3UQ867SPQQA43P2F",
"zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG");

// Get object lock retention
Retention retention =
minioClient.getObjectRetention(
GetObjectRetentionArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
.build());

System.out.println("Mode: " + retention.mode());
System.out.println("Retainuntil Date: " + retention.retainUntilDate());
} catch (MinioException e) {
System.out.println("Error occurred: " + e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,20 @@
*/

import io.minio.GetObjectRetentionArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.PutObjectOptions;
import io.minio.SetObjectRetentionArgs;
import io.minio.errors.ErrorResponseException;
import io.minio.errors.InsufficientDataException;
import io.minio.errors.InternalException;
import io.minio.errors.InvalidBucketNameException;
import io.minio.errors.InvalidEndpointException;
import io.minio.errors.InvalidPortException;
import io.minio.errors.InvalidResponseException;
import io.minio.errors.MinioException;
import io.minio.errors.RegionConflictException;
import io.minio.messages.Retention;
import io.minio.messages.RetentionMode;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.time.ZonedDateTime;

public class GetObjectRetentionConfig {
/** MinioClient.getObjectRetention() example. */
public class SetObjectRetention {
/** MinioClient.setObjectRetention() example. */
public static void main(String[] args)
throws IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException,
InsufficientDataException, InternalException, ErrorResponseException,
InvalidBucketNameException, InvalidPortException, InvalidEndpointException,
RegionConflictException, IllegalArgumentException {
throws IOException, NoSuchAlgorithmException, InvalidKeyException, IllegalArgumentException {
try {

/* play.min.io for test and development. */
Expand All @@ -52,37 +38,6 @@ public static void main(String[] args)
"Q3AM3UQ867SPQQA43P2F",
"zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG");

// Create bucket if it doesn't exist.
boolean found = minioClient.bucketExists("my-bucketname");
if (found) {
System.out.println("my-bucketname already exists");
} else {
// Create bucket 'my-bucketname' with object lock functionality enabled
minioClient.makeBucket(
MakeBucketArgs.builder().bucket("my-bucketname").objectLock(true).build());
System.out.println(
"my-bucketname is created successfully with object lock functionality enabled.");
}

StringBuilder builder = new StringBuilder();
for (int i = 0; i < 100; i++) {
builder.append(
"Sphinx of black quartz, judge my vow: Used by Adobe InDesign to display font samples. ");
builder.append("(29 letters)\n");
builder.append(
"Jackdaws love my big sphinx of quartz: Similarly, used by Windows XP for some fonts. ");
builder.append("---\n");
}

// Create a InputStream for object upload.
ByteArrayInputStream bais = new ByteArrayInputStream(builder.toString().getBytes("UTF-8"));

// Create object 'my-objectname' in 'my-bucketname' with content from the input stream.
minioClient.putObject(
"my-bucketname", "my-objectname", bais, new PutObjectOptions(bais.available(), -1));
bais.close();
System.out.println("my-objectname is uploaded successfully");

// Declaring config with Retention mode as Compliance and
// retain until one year to current date.
ZonedDateTime retentionUntil = ZonedDateTime.now().plusYears(1);
Expand All @@ -94,7 +49,7 @@ public static void main(String[] args)
.bucket("my-bucketname")
.object("my-objectname")
.config(config)
.bypassGovernanceRetention(true)
.bypassGovernanceMode(true)
.build());

// Get object lock retention
Expand Down
Loading

0 comments on commit a9e13be

Please sign in to comment.