Skip to content

Commit 4b9abae

Browse files
zhuxiaolong37huiguangjun
authored andcommitted
Adding the object size than parameter to the bucket lifecycle
1 parent 3292b5b commit 4b9abae

File tree

6 files changed

+194
-1
lines changed

6 files changed

+194
-1
lines changed

src/main/java/com/aliyun/oss/common/parser/RequestMarshallers.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,12 @@ public FixedLengthInputStream marshall(SetBucketLifecycleRequest request) {
555555

556556
if(rule.getFilter() != null){
557557
xmlBody.append("<Filter>");
558-
558+
if (rule.getFilter().getObjectSizeGreaterThan() != null) {
559+
xmlBody.append("<ObjectSizeGreaterThan>").append(rule.getFilter().getObjectSizeGreaterThan()).append("</ObjectSizeGreaterThan>");
560+
}
561+
if (rule.getFilter().getObjectSizeLessThan() != null) {
562+
xmlBody.append("<ObjectSizeLessThan>").append(rule.getFilter().getObjectSizeLessThan()).append("</ObjectSizeLessThan>");
563+
}
559564
for(LifecycleNot not : rule.getFilter().getNotList()){
560565
xmlBody.append("<Not>");
561566
xmlBody.append("<Prefix>" + not.getPrefix() + "</Prefix>");

src/main/java/com/aliyun/oss/internal/ResponseParsers.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3458,6 +3458,12 @@ public static List<LifecycleRule> parseGetBucketLifecycle(InputStream responseBo
34583458
Element filterElems = ruleElem.getChild("Filter");
34593459
if(filterElems != null){
34603460
LifecycleFilter lifecycleFilter = new LifecycleFilter();
3461+
if (filterElems.getChild("ObjectSizeGreaterThan") != null) {
3462+
lifecycleFilter.setObjectSizeGreaterThan(Long.valueOf(filterElems.getChildText("ObjectSizeGreaterThan")));
3463+
}
3464+
if (filterElems.getChild("ObjectSizeLessThan") != null) {
3465+
lifecycleFilter.setObjectSizeLessThan(Long.valueOf(filterElems.getChildText("ObjectSizeLessThan")));
3466+
}
34613467

34623468
List<LifecycleNot> notList = new ArrayList<LifecycleNot>();
34633469
for(Element notEle : filterElems.getChildren("Not")){

src/main/java/com/aliyun/oss/model/LifecycleFilter.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,31 @@ public class LifecycleFilter {
77

88
private List<LifecycleNot> notList = new ArrayList<LifecycleNot>();
99

10+
private Long objectSizeGreaterThan;
11+
12+
private Long objectSizeLessThan;
13+
1014
public List<LifecycleNot> getNotList() {
1115
return notList;
1216
}
1317

1418
public void setNotList(List<LifecycleNot> notList) {
1519
this.notList = notList;
1620
}
21+
22+
public Long getObjectSizeGreaterThan() {
23+
return objectSizeGreaterThan;
24+
}
25+
26+
public void setObjectSizeGreaterThan(Long objectSizeGreaterThan) {
27+
this.objectSizeGreaterThan = objectSizeGreaterThan;
28+
}
29+
30+
public Long getObjectSizeLessThan() {
31+
return objectSizeLessThan;
32+
}
33+
34+
public void setObjectSizeLessThan(Long objectSizeLessThan) {
35+
this.objectSizeLessThan = objectSizeLessThan;
36+
}
1737
}

src/test/java/com/aliyun/oss/common/parser/RequestMarshallersTest.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,4 +1553,47 @@ public void testAsyncProcessObjectRequestMarshaller() {
15531553
Assert.assertTrue(returnData.equals("x-oss-async-process="+style.replaceAll("=","")));
15541554
}
15551555

1556+
1557+
@Test
1558+
public void testPutLifeCycleFilterObjectSizeThanRequestMarshaller() {
1559+
SetBucketLifecycleRequest request = new SetBucketLifecycleRequest("bucket");
1560+
String ruleId0 = "rule-object-size";
1561+
String matchPrefix0 = "object-size/";
1562+
Map<String, String> matchTags0 = new HashMap<String, String>();
1563+
matchTags0.put("object-size-key", "object-size-value");
1564+
LifecycleRule rule = new LifecycleRule(ruleId0, matchPrefix0, LifecycleRule.RuleStatus.Enabled, 3);
1565+
rule.setTags(matchTags0);
1566+
LifecycleFilter filter = new LifecycleFilter();
1567+
LifecycleNot not = new LifecycleNot();
1568+
List<LifecycleNot> notList = new ArrayList<LifecycleNot>();
1569+
Tag tag = new Tag("key","value");
1570+
not.setPrefix("not-prefix");
1571+
not.setTag(tag);
1572+
notList.add(not);
1573+
filter.setObjectSizeGreaterThan(100L);
1574+
filter.setObjectSizeLessThan(1000L);
1575+
filter.setNotList(notList);
1576+
rule.setFilter(filter);
1577+
request.AddLifecycleRule(rule);
1578+
1579+
FixedLengthInputStream is = setBucketLifecycleRequestMarshaller.marshall(request);
1580+
1581+
SAXBuilder builder = new SAXBuilder();
1582+
Document doc = null;
1583+
try {
1584+
doc = builder.build(is);
1585+
} catch (JDOMException e) {
1586+
e.printStackTrace();
1587+
} catch (IOException e) {
1588+
e.printStackTrace();
1589+
}
1590+
1591+
Element root = doc.getRootElement();
1592+
Assert.assertEquals(matchPrefix0, root.getChild("Rule").getChildText("Prefix"));
1593+
Assert.assertEquals(Long.valueOf(100), Long.valueOf(root.getChild("Rule").getChild("Filter").getChildText("ObjectSizeGreaterThan")));
1594+
Assert.assertEquals(Long.valueOf(1000), Long.valueOf(root.getChild("Rule").getChild("Filter").getChildText("ObjectSizeLessThan")));
1595+
Assert.assertEquals("object-size/not-prefix", root.getChild("Rule").getChild("Filter").getChildren("Not").get(0).getChildText("Prefix"));
1596+
Assert.assertEquals("key", root.getChild("Rule").getChild("Filter").getChildren("Not").get(0).getChild("Tag").getChildText("Key"));
1597+
Assert.assertEquals("value", root.getChild("Rule").getChild("Filter").getChildren("Not").get(0).getChild("Tag").getChildText("Value"));
1598+
}
15561599
}

src/test/java/com/aliyun/oss/common/parser/ResponseParsersTest.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5617,4 +5617,47 @@ public void testParseAsyncProcessObject() {
56175617
Assert.assertEquals("A JSONObject text must begin with '{' at character 0 of ", e.getMessage());
56185618
}
56195619
}
5620+
5621+
@Test
5622+
public void testParseGetBucketLifecycleFilterObjectSizeThan() {
5623+
InputStream inputStream = null;
5624+
String respBody;
5625+
5626+
respBody = "" +
5627+
"<LifecycleConfiguration>\n" +
5628+
" <Rule>\n" +
5629+
" <ID>RuleID</ID>\n" +
5630+
" <Prefix>Prefix</Prefix>\n" +
5631+
" <Status>Enabled</Status>\n" +
5632+
" <Filter>\n" +
5633+
" <ObjectSizeGreaterThan>500</ObjectSizeGreaterThan>\n" +
5634+
" <ObjectSizeLessThan>64000</ObjectSizeLessThan>\n" +
5635+
" <Not>\n" +
5636+
" <Prefix>Prefix/not</Prefix>\n" +
5637+
" <Tag><Key>notkey</Key><Value>notvalue</Value></Tag>\n" +
5638+
" </Not>\n" +
5639+
" </Filter>\n" +
5640+
" </Rule>\n" +
5641+
"</LifecycleConfiguration>";
5642+
5643+
try {
5644+
inputStream = new ByteArrayInputStream(respBody.getBytes("utf-8"));
5645+
} catch (UnsupportedEncodingException e) {
5646+
Assert.fail("UnsupportedEncodingException");
5647+
}
5648+
5649+
List<LifecycleRule> rules = null;
5650+
try {
5651+
rules = ResponseParsers.parseGetBucketLifecycle(inputStream);
5652+
} catch (ResponseParseException e) {
5653+
Assert.fail("parse response body fail!");
5654+
}
5655+
5656+
Assert.assertEquals("Prefix/not", rules.get(0).getFilter().getNotList().get(0).getPrefix());
5657+
Assert.assertEquals("notkey", rules.get(0).getFilter().getNotList().get(0).getTag().getKey());
5658+
Assert.assertEquals("notvalue", rules.get(0).getFilter().getNotList().get(0).getTag().getValue());
5659+
Assert.assertEquals(rules.get(0).getFilter().getObjectSizeGreaterThan(), Long.valueOf(String.valueOf(500)));
5660+
Assert.assertEquals(rules.get(0).getFilter().getObjectSizeLessThan(), Long.valueOf(String.valueOf(64000)));
5661+
}
5662+
56205663
}

src/test/java/com/aliyun/oss/integrationtests/BucketLifecycleTest.java

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,4 +917,80 @@ public void testVerificationLifecycleRulesLimit() throws ParseException {
917917
Assert.fail(e.getMessage());
918918
}
919919
}
920+
921+
@Test
922+
public void testBucketLifecycleWithFilterObjectSizeThan() throws ParseException {
923+
final String bucketName = super.bucketName + "-bucket-lifecycle-object-size-than";
924+
final String ruleId0 = "rule-object-size-than";
925+
final String matchPrefix0 = "rule-object/";
926+
final String ruleId1 = "rule2-object-size-than";
927+
final String matchPrefix1 = "rule2-object/";
928+
Long objectSizeGreaterThan = 120L;
929+
Long objectSizeLessThan = 1240L;
930+
Long objectSizeGreaterThan2 = 1024L;
931+
Long objectSizeLessThan2 = 4096L;
932+
933+
try {
934+
ossClient.createBucket(bucketName);
935+
936+
SetBucketLifecycleRequest request = new SetBucketLifecycleRequest(bucketName);
937+
938+
Map<String, String> tags = new HashMap<String, String>(1);
939+
tags.put("tag1", "abc");
940+
tags.put("tag2", "def");
941+
LifecycleFilter filter = new LifecycleFilter();
942+
LifecycleNot not = new LifecycleNot();
943+
Tag tag = new Tag("tag-key","tag-value");
944+
not.setPrefix("not-prefix");
945+
not.setTag(tag);
946+
List<LifecycleNot> notList = new ArrayList<LifecycleNot>();
947+
notList.add(not);
948+
filter.setNotList(notList);
949+
filter.setObjectSizeGreaterThan(objectSizeGreaterThan);
950+
filter.setObjectSizeLessThan(objectSizeLessThan);
951+
952+
// ruleId0
953+
LifecycleRule rule = new LifecycleRule(ruleId0, matchPrefix0, RuleStatus.Enabled, 3);
954+
rule.setTags(tags);
955+
rule.setFilter(filter);
956+
request.AddLifecycleRule(rule);
957+
958+
// ruleId1
959+
LifecycleNot not1 = new LifecycleNot();
960+
LifecycleFilter filter1 = new LifecycleFilter();
961+
rule = new LifecycleRule(ruleId1, matchPrefix1, RuleStatus.Enabled,
962+
DateUtil.parseIso8601Date("2022-10-12T00:00:00.000Z"));
963+
not1.setPrefix(matchPrefix1+"not-prefix1");
964+
965+
filter1.setObjectSizeGreaterThan(objectSizeGreaterThan2);
966+
filter1.setObjectSizeLessThan(objectSizeLessThan2);
967+
rule.setFilter(filter1);
968+
request.AddLifecycleRule(rule);
969+
970+
ossClient.setBucketLifecycle(request);
971+
972+
List<LifecycleRule> rules = ossClient.getBucketLifecycle(bucketName);
973+
974+
LifecycleRule r0 = rules.get(0);
975+
Assert.assertEquals(ruleId0, r0.getId());
976+
Assert.assertEquals(matchPrefix0, r0.getPrefix());
977+
Assert.assertEquals(RuleStatus.Enabled, r0.getStatus());
978+
Assert.assertEquals(3, r0.getExpirationDays());
979+
Assert.assertTrue(r0.getAbortMultipartUpload() == null);
980+
Assert.assertEquals(2, r0.getTags().size());
981+
Assert.assertEquals(objectSizeGreaterThan, r0.getFilter().getObjectSizeGreaterThan());
982+
Assert.assertEquals(objectSizeLessThan, r0.getFilter().getObjectSizeLessThan());
983+
Assert.assertEquals(matchPrefix0+"not-prefix", r0.getFilter().getNotList().get(0).getPrefix());
984+
Assert.assertEquals("tag-key", r0.getFilter().getNotList().get(0).getTag().getKey());
985+
Assert.assertEquals("tag-value", r0.getFilter().getNotList().get(0).getTag().getValue());
986+
LifecycleRule r1 = rules.get(1);
987+
Assert.assertEquals(ruleId1, r1.getId());
988+
Assert.assertEquals(objectSizeGreaterThan2, r1.getFilter().getObjectSizeGreaterThan());
989+
Assert.assertEquals(objectSizeLessThan2, r1.getFilter().getObjectSizeLessThan());
990+
} catch (OSSException e) {
991+
Assert.fail(e.getMessage());
992+
} finally {
993+
ossClient.deleteBucket(bucketName);
994+
}
995+
}
920996
}

0 commit comments

Comments
 (0)