Skip to content

Commit 3f391ee

Browse files
sinhaashishnitisht
authored andcommitted
Add API to Set/Get/Delete bucket lifecycle (#743)
1 parent 700f700 commit 3f391ee

File tree

6 files changed

+454
-5
lines changed

6 files changed

+454
-5
lines changed

api/src/main/java/io/minio/MinioClient.java

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,10 @@ private Request createRequest(Method method, String bucketName, String objectNam
950950
String[] hashes = Digest.sha256Md5Hashes(data, len);
951951
sha256Hash = hashes[0];
952952
md5Hash = hashes[1];
953+
} else if (method == Method.PUT && queryParamMap != null && queryParamMap.containsKey("lifecycle")) {
954+
String[] hashes = Digest.sha256Md5Hashes(data, len);
955+
sha256Hash = hashes[0];
956+
md5Hash = hashes[1];
953957
} else {
954958
// Fix issue #567: Compute SHA256 hash only.
955959
sha256Hash = Digest.sha256Hash(data, len);
@@ -4228,6 +4232,125 @@ public void setBucketPolicy(String bucketName, String policy)
42284232
response.body().close();
42294233
}
42304234

4235+
/**
4236+
* Set XML string of LifeCycle on a given bucket.
4237+
* Delete the lifecycle of bucket in case a null is passed as lifeCycle.
4238+
* @param bucketName Bucket name.
4239+
* @param lifeCycle Bucket policy XML string.
4240+
*
4241+
* </p><b>Example:</b><br>
4242+
* <pre>{@code String lifecycle = "<LifecycleConfiguration><Rule><ID>expire-bucket</ID><Prefix></Prefix>"
4243+
* + "<Status>Enabled</Status><Expiration><Days>365</Days></Expiration></Rule></LifecycleConfiguration>";
4244+
*
4245+
* setBucketLifecycle("my-bucketname", lifecycle); }</pre>
4246+
* @throws InvalidBucketNameException upon invalid bucket name is given
4247+
* @throws NoSuchAlgorithmException upon requested algorithm was not found during
4248+
* signature calculation
4249+
* @throws InsufficientDataException upon getting EOFException while reading given
4250+
* InputStream even before reading given length
4251+
* @throws IOException upon connection error
4252+
* @throws InvalidKeyException upon an invalid access key or secret key
4253+
* @throws NoResponseException upon no response from server
4254+
* @throws XmlPullParserException upon parsing response xml
4255+
* @throws ErrorResponseException upon unsuccessful execution
4256+
* @throws InternalException upon internal library error
4257+
* @throws InvalidArgumentException upon invalid value is passed to a method.
4258+
*/
4259+
public void setBucketLifeCycle(String bucketName, String lifeCycle)
4260+
throws InvalidBucketNameException, NoSuchAlgorithmException,
4261+
InsufficientDataException, IOException, InvalidKeyException, NoResponseException,
4262+
XmlPullParserException, ErrorResponseException, InternalException,InvalidArgumentException {
4263+
if ((lifeCycle == null) || "".equals(lifeCycle)) {
4264+
throw new InvalidArgumentException("life cycle cannot be empty");
4265+
}
4266+
Map<String, String> headerMap = new HashMap<>();
4267+
headerMap.put("Content-Length", Integer.toString(lifeCycle.length()));
4268+
Map<String, String> queryParamMap = new HashMap<>();
4269+
queryParamMap.put("lifecycle", "");
4270+
HttpResponse response = executePut(bucketName, null, headerMap, queryParamMap, lifeCycle, 0);
4271+
response.body().close();
4272+
}
4273+
4274+
/**
4275+
* Delete the LifeCycle of bucket.
4276+
*
4277+
* @param bucketName Bucket name.
4278+
*
4279+
* </p><b>Example:</b><br>
4280+
* <pre>{@code deleteBucketLifeCycle("my-bucketname"); }</pre>
4281+
* @throws InvalidBucketNameException upon invalid bucket name is given
4282+
* @throws NoSuchAlgorithmException upon requested algorithm was not found during
4283+
* signature calculation
4284+
* @throws InsufficientDataException upon getting EOFException while reading given
4285+
* InputStream even before reading given length
4286+
* @throws IOException upon connection error
4287+
* @throws InvalidKeyException upon an invalid access key or secret key
4288+
* @throws NoResponseException upon no response from server
4289+
* @throws XmlPullParserException upon parsing response xml
4290+
* @throws ErrorResponseException upon unsuccessful execution
4291+
* @throws InternalException upon internal library error
4292+
*/
4293+
public void deleteBucketLifeCycle(String bucketName)
4294+
throws InvalidBucketNameException, NoSuchAlgorithmException,
4295+
InsufficientDataException, IOException, InvalidKeyException, NoResponseException,
4296+
XmlPullParserException, ErrorResponseException, InternalException {
4297+
Map<String,String> queryParamMap = new HashMap<>();
4298+
queryParamMap.put("lifecycle", "");
4299+
HttpResponse response = executeDelete(bucketName, "", queryParamMap);
4300+
response.body().close();
4301+
}
4302+
4303+
/** Get bucket life cycle configuration.
4304+
*
4305+
* @param bucketName Bucket name.
4306+
*
4307+
* </p><b>Example:</b><br>
4308+
* <pre>{@code String bucketLifeCycle = minioClient.getBucketLifecycle("my-bucketname");
4309+
* }</pre>
4310+
* @throws InvalidBucketNameException upon invalid bucket name is given
4311+
* @throws NoSuchAlgorithmException upon requested algorithm was not found during
4312+
* signature calculation
4313+
* @throws InsufficientDataException upon getting EOFException while reading given
4314+
* InputStream even before reading given length
4315+
* @throws IOException upon connection error
4316+
* @throws InvalidKeyException upon an invalid access key or secret key
4317+
* @throws NoResponseException upon no response from server
4318+
* @throws XmlPullParserException upon parsing response xml
4319+
* @throws ErrorResponseException upon unsuccessful execution
4320+
* @throws InternalException upon internal library error
4321+
*
4322+
*/
4323+
public String getBucketLifeCycle(String bucketName)
4324+
throws InvalidBucketNameException, NoSuchAlgorithmException,
4325+
InsufficientDataException, IOException, InvalidKeyException, NoResponseException,
4326+
XmlPullParserException, ErrorResponseException, InternalException {
4327+
Map<String,String> queryParamMap = new HashMap<>();
4328+
queryParamMap.put("lifecycle", "");
4329+
HttpResponse response = null;
4330+
String bodyContent = "";
4331+
Scanner scanner = null ;
4332+
try {
4333+
response = executeGet(bucketName, "", null, queryParamMap);
4334+
scanner = new Scanner(response.body().charStream());
4335+
// read entire body stream to string.
4336+
scanner.useDelimiter("\\A");
4337+
if (scanner.hasNext()) {
4338+
bodyContent = scanner.next();
4339+
}
4340+
} catch (ErrorResponseException e) {
4341+
if (e.errorResponse().errorCode() != ErrorCode.NO_SUCH_LIFECYCLE_CONFIGURATION) {
4342+
throw e;
4343+
}
4344+
} finally {
4345+
if (response != null && response.body() != null) {
4346+
response.body().close();
4347+
}
4348+
if (scanner != null) {
4349+
scanner.close();
4350+
}
4351+
}
4352+
return bodyContent;
4353+
}
42314354

42324355
/**
42334356
* Get bucket notification configuration

docs/API.md

Lines changed: 137 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ MinioClient minioClient = new MinioClient("https://play.minio.io:9000", "Q3AM3UQ
1515
MinioClient s3Client = new MinioClient("https://s3.amazonaws.com", "YOUR-ACCESSKEYID", "YOUR-SECRETACCESSKEY");
1616
```
1717

18-
| Bucket operations | Object operations | Presigned operations | Bucket Policy Operations
18+
| Bucket operations | Object operations | Presigned operations | Bucket Policy/LifeCycle Operations
1919
|:--- |:--- |:--- |:--- |
2020
| [`makeBucket`](#makeBucket) |[`getObject`](#getObject) |[`presignedGetObject`](#presignedGetObject) | [`getBucketPolicy`](#getBucketPolicy) |
2121
| [`listBuckets`](#listBuckets) | [`putObject`](#putObject) | [`presignedPutObject`](#presignedPutObject) | [`setBucketPolicy`](#setBucketPolicy) |
22-
| [`bucketExists`](#bucketExists) | [`copyObject`](#copyObject) | [`presignedPostPolicy`](#presignedPostPolicy) | |
23-
| [`removeBucket`](#removeBucket) | [`statObject`](#statObject) | | |
24-
| [`listObjects`](#listObjects) | [`removeObject`](#removeObject) | | |
22+
| [`bucketExists`](#bucketExists) | [`copyObject`](#copyObject) | [`presignedPostPolicy`](#presignedPostPolicy) | [`setBucketLifeCycle`](#setBucketLifeCycle) |
23+
| [`removeBucket`](#removeBucket) | [`statObject`](#statObject) | | [`getBucketLifeCycle`](#getBucketLifeCycle) |
24+
| [`listObjects`](#listObjects) | [`removeObject`](#removeObject) | | [`deleteBucketLifeCycle`](#deleteBucketLifeCycle) |
2525
| [`listIncompleteUploads`](#listIncompleteUploads) | [`removeIncompleteUpload`](#removeIncompleteUpload) | | |
2626
| [`listenBucketNotification`](#listenBucketNotification) | | | |
2727

@@ -647,6 +647,139 @@ try {
647647
}
648648
```
649649

650+
<a name="setBucketLifeCycle"></a>
651+
### setBucketLifeCycle(String bucketName, String lifeCycle)
652+
`public void setBucketLifeCycle(String bucketName, String lifeCycle)`
653+
654+
Set a life cydle on bucket.
655+
656+
[View Javadoc](http://minio.github.io/minio-java/io/minio/MinioClient.html#setBucketLifeCycle-java.lang.String-java.lang.String-io.minio.BucketLifeCycle-)
657+
658+
__Parameters__
659+
660+
|Param | Type | Description |
661+
|:--- |:--- |:--- |
662+
| ``bucketName`` | _String_ | Name of the bucket. |
663+
| ``lifeCycle`` | _String_ | Life cycle XML for the bucket. |
664+
665+
| Return Type | Exceptions |
666+
|:--- |:--- |
667+
| None | Listed Exceptions: |
668+
| | ``InvalidBucketNameException`` : upon invalid bucket name. |
669+
| | ``NoSuchAlgorithmException`` : upon requested algorithm was not found during signature calculation. |
670+
| | ``InsufficientDataException`` : Thrown to indicate that reading given InputStream gets EOFException before reading given length. |
671+
| | ``IOException`` : upon connection error. |
672+
| | ``InvalidKeyException`` : upon an invalid access key or secret key. |
673+
| | ``NoResponseException`` : upon no response from server. |
674+
| | ``org.xmlpull.v1.XmlPullParserException`` : upon parsing response XML. |
675+
| | ``ErrorResponseException`` : upon unsuccessful execution. |
676+
| | ``InternalException`` : upon internal library error. |
677+
| | ``InvalidArgumentException`` : upon invalid value is passed to a method. |
678+
679+
__Example__
680+
681+
682+
```java
683+
try {
684+
/* Amazon S3: */
685+
MinioClient minioClient = new MinioClient("https://s3.amazonaws.com", "YOUR-ACCESSKEYID",
686+
"YOUR-SECRETACCESSKEY");
687+
String lifeCycle = "<LifecycleConfiguration><Rule><ID>expire-bucket</ID><Prefix></Prefix>"
688+
+ "<Status>Enabled</Status><Expiration><Days>365</Days></Expiration>"
689+
+ "</Rule></LifecycleConfiguration>";
690+
691+
692+
minioClient.setBucketLifecycle("lifecycleminiotest", lifeCycle);
693+
} catch (MinioException e) {
694+
System.out.println("Error occurred: " + e);
695+
}
696+
```
697+
698+
<a name="getBucketLifeCycle"></a>
699+
### getBucketLifeCycle(String bucketName)
700+
`public String getBucketLifeCycle(String bucketName)`
701+
702+
Get the lifecycle of the bucket.
703+
704+
[View Javadoc](http://minio.github.io/minio-java/io/minio/MinioClient.html#getBucketLifeCycle-java.lang.String-)
705+
706+
__Parameters__
707+
708+
|Param | Type | Description |
709+
|:--- |:--- |:--- |
710+
| ``bucketName`` | _String_ | Name of the bucket. |
711+
712+
| Return Type | Exceptions |
713+
|:--- |:--- |
714+
| None | Listed Exceptions: |
715+
| | ``InvalidBucketNameException`` : upon invalid bucket name. |
716+
| | ``NoSuchAlgorithmException`` : upon requested algorithm was not found during signature calculation. |
717+
| | ``InsufficientDataException`` : Thrown to indicate that reading given InputStream gets EOFException before reading given length. |
718+
| | ``IOException`` : upon connection error. |
719+
| | ``InvalidKeyException`` : upon an invalid access key or secret key. |
720+
| | ``NoResponseException`` : upon no response from server. |
721+
| | ``org.xmlpull.v1.XmlPullParserException`` : upon parsing response XML. |
722+
| | ``ErrorResponseException`` : upon unsuccessful execution. |
723+
| | ``InternalException`` : upon internal library error. |
724+
725+
__Example__
726+
727+
728+
```java
729+
730+
try {
731+
/* Amazon S3: */
732+
MinioClient minioClient = new MinioClient("https://s3.amazonaws.com", "YOUR-ACCESSKEYID",
733+
"YOUR-SECRETACCESSKEY");
734+
String lifecycle = minioClient.getBucketLifecycle("my-bucketName" );
735+
System.out.println(" Life Cycle is : " + lifecycle );
736+
} catch (MinioException e) {
737+
System.out.println("Error occurred: " + e);
738+
}
739+
```
740+
741+
<a name="deleteBucketLifeCycle"></a>
742+
### deleteBucketLifeCycle(String bucketName)
743+
`private void deleteBucketLifeCycle(String bucketName)`
744+
745+
Delete the lifecycle of the bucket.
746+
747+
[View Javadoc](http://minio.github.io/minio-java/io/minio/MinioClient.html#getBucketLifeCycle-java.lang.String-)
748+
749+
__Parameters__
750+
751+
|Param | Type | Description |
752+
|:--- |:--- |:--- |
753+
| ``bucketName`` | _String_ | Name of the bucket. |
754+
755+
| Return Type | Exceptions |
756+
|:--- |:--- |
757+
| None | Listed Exceptions: |
758+
| | ``InvalidBucketNameException`` : upon invalid bucket name. |
759+
| | ``NoSuchAlgorithmException`` : upon requested algorithm was not found during signature calculation. |
760+
| | ``InsufficientDataException`` : Thrown to indicate that reading given InputStream gets EOFException before reading given length. |
761+
| | ``IOException`` : upon connection error. |
762+
| | ``InvalidKeyException`` : upon an invalid access key or secret key. |
763+
| | ``NoResponseException`` : upon no response from server. |
764+
| | ``org.xmlpull.v1.XmlPullParserException`` : upon parsing response XML. |
765+
| | ``ErrorResponseException`` : upon unsuccessful execution. |
766+
| | ``InternalException`` : upon internal library error. |
767+
768+
__Example__
769+
770+
771+
```java
772+
773+
try {
774+
/* Amazon S3: */
775+
MinioClient minioClient = new MinioClient("https://s3.amazonaws.com", "YOUR-ACCESSKEYID",
776+
"YOUR-SECRETACCESSKEY");
777+
minioClient.deleteBucketLifeCycle("my-bucketName" );
778+
} catch (MinioException e) {
779+
System.out.println("Error occurred: " + e);
780+
}
781+
```
782+
650783
<a name="listenBucketNotification"></a>
651784
### listenBucketNotification(String bucketName, String prefix, String suffix, String[] events, BucketEventListener listener)
652785
`public void listenBucketNotification(String bucketName, String prefix, String suffix, String[] events, BucketEventListener listener)`
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Minio Java SDK for Amazon S3 Compatible Cloud Storage, (C) 2019 Minio, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import java.io.IOException;
18+
import java.security.NoSuchAlgorithmException;
19+
import java.security.InvalidKeyException;
20+
import org.xmlpull.v1.XmlPullParserException;
21+
import io.minio.MinioClient;
22+
import io.minio.errors.MinioException;
23+
24+
public class DeleteBucketLifeCycle {
25+
/**
26+
* MinioClient.DeleteBucketLifeCycle() example.
27+
*/
28+
public static void main(String[] args)
29+
throws IOException, NoSuchAlgorithmException, InvalidKeyException, XmlPullParserException {
30+
try {
31+
/* Amazon S3: */
32+
MinioClient minioClient = new MinioClient("https://s3.amazonaws.com", "YOUR-ACCESSKEYID",
33+
"YOUR-SECRETACCESSKEY");
34+
// Pass blank as life cycle in setBucketLifeCycle method.
35+
minioClient.deleteBucketLifeCycle("my-bucketName");
36+
} catch (MinioException e) {
37+
System.out.println("Error occurred: " + e);
38+
}
39+
}
40+
}

examples/GetBucketLifeCycle.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Minio Java SDK for Amazon S3 Compatible Cloud Storage, (C) 2019 Minio, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import java.io.IOException;
18+
19+
import java.security.NoSuchAlgorithmException;
20+
import java.security.InvalidKeyException;
21+
import org.xmlpull.v1.XmlPullParserException;
22+
import io.minio.MinioClient;
23+
import io.minio.errors.MinioException;
24+
25+
public class GetBucketLifeCycle {
26+
/**
27+
* MinioClient.getBucketLifecycle() example.
28+
*/
29+
public static void main(String[] args)
30+
throws IOException, NoSuchAlgorithmException, InvalidKeyException, XmlPullParserException {
31+
try {
32+
/* Amazon S3: */
33+
MinioClient minioClient = new MinioClient("https://s3.amazonaws.com", "YOUR-ACCESSKEYID",
34+
"YOUR-SECRETACCESSKEY");
35+
String lifecycle = minioClient.getBucketLifeCycle("my-bucketName" );
36+
System.out.println(" Life Cycle is : " + lifecycle );
37+
} catch (MinioException e) {
38+
System.out.println("Error occurred: " + e);
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)