@@ -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
0 commit comments