Skip to content
This repository was archived by the owner on Dec 6, 2024. It is now read-only.

Commit 6e7e455

Browse files
author
synapticloop
committed
fixed documentation, start of LifecycleRules (when available on B2)
1 parent 65d8c72 commit 6e7e455

File tree

7 files changed

+219
-22
lines changed

7 files changed

+219
-22
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ see [B2ApiClient.java](https://github.com/synapticloop/backblaze-b2-java-api/blo
103103
B2ApiClient()
104104
105105
// authenticate the client
106-
authorize(String, String)
106+
authenticate(String, String)
107107
108108
// create a bucket
109109
createBucket(String, BucketType)
@@ -480,9 +480,9 @@ repositories {
480480

481481
```
482482
dependencies {
483-
runtime(group: 'synapticloop', name: 'backblaze-b2-java-api', version: '2.1.0', ext: 'jar')
483+
runtime(group: 'synapticloop', name: 'backblaze-b2-java-api', version: '2.1.1', ext: 'jar')
484484
485-
compile(group: 'synapticloop', name: 'backblaze-b2-java-api', version: '2.1.0', ext: 'jar')
485+
compile(group: 'synapticloop', name: 'backblaze-b2-java-api', version: '2.1.1', ext: 'jar')
486486
}
487487
```
488488

@@ -494,9 +494,9 @@ or, more simply for versions of gradle greater than 2.1
494494

495495
```
496496
dependencies {
497-
runtime 'synapticloop:backblaze-b2-java-api:2.1.0'
497+
runtime 'synapticloop:backblaze-b2-java-api:2.1.1'
498498
499-
compile 'synapticloop:backblaze-b2-java-api:2.1.0'
499+
compile 'synapticloop:backblaze-b2-java-api:2.1.1'
500500
}
501501
```
502502

@@ -514,7 +514,7 @@ dependencies {
514514
<dependency>
515515
<groupId>synapticloop</groupId>
516516
<artifactId>backblaze-b2-java-api</artifactId>
517-
<version>2.1.0</version>
517+
<version>2.1.1</version>
518518
<type>jar</type>
519519
</dependency>
520520
```

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ group = 'synapticloop'
1414
archivesBaseName = 'backblaze-b2-java-api'
1515
description = """A java api for the truly excellent backblaze b2 storage service"""
1616

17-
version = '2.1.0'
17+
version = '2.1.1'
1818

1919
sourceCompatibility = 1.7
2020
targetCompatibility = 1.7
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
package synapticloop.b2;
2+
3+
import org.json.JSONObject;
4+
5+
/*
6+
* Copyright (c) 2016 Synapticloop.
7+
*
8+
* All rights reserved.
9+
*
10+
* This code may contain contributions from other parties which, where
11+
* applicable, will be listed in the default build file for the project
12+
* ~and/or~ in a file named CONTRIBUTORS.txt in the root of the project.
13+
*
14+
* This source code and any derived binaries are covered by the terms and
15+
* conditions of the Licence agreement ("the Licence"). You may not use this
16+
* source code or any derived binaries except in compliance with the Licence.
17+
* A copy of the Licence is available in the file named LICENSE.txt shipped with
18+
* this source code or binaries.
19+
*/
20+
21+
/**
22+
* <p>Lifecycle rules instruct the B2 service to automatically hide and/or delete
23+
* old files. You can set up rules to do things like delete old versions of
24+
* files 30 days after a newer version was uploaded.</p>
25+
*
26+
* <p>A bucket can have up to 100 lifecycle rules. Each rule has a fileNamePrefix
27+
* that specifies which files in the bucket it applies to. Any file whose name
28+
* starts with the prefix is subject to the rule. A prefix of the empty string,
29+
* "", means that the rule applies to all files in the bucket.</p>
30+
*
31+
* <p>You're not allowed to create two rules that both apply to the same files.
32+
* For example, a rule with a file name prefix of photos/ and a rule with a file
33+
* name prefix of photos/kittens/ would both apply to a file named
34+
* photos/kittens/fluffy.jpg, so you're not allowed to have both rules at the
35+
* same time.</p>
36+
*
37+
* <p>Each lifecycle rule specifies two things:</p>
38+
*
39+
* <ul>
40+
* <li>daysFromUploadingToHiding and</li>
41+
* <li>daysFromHidingToDeleting.</li>
42+
* </ul>
43+
*
44+
* <p>Either can be null, which means that part of the rule doesn't apply. Setting
45+
* both to null is not allowed, because the rule would do nothing.</p>
46+
*
47+
* <p>Setting daysFromUploadingToHiding to 0 is not allowed. When set, it must be a
48+
* positive number.</p>
49+
*
50+
* <p>The most commonly used setting is daysFromHidingToDeleting, which says how long
51+
* to keep file versions that are not the current version. A file version counts
52+
* as hidden when explicitly hidden with b2_hide_file, or when a newer file with
53+
* the same name is uploaded. When a rule with this setting applies, the file will
54+
* be deleted the given number of days after it is hidden.</p>
55+
*
56+
* <p>For example, if you are backing up your files to B2 using the B2 command-line
57+
* tool, or another software package that uploads files when they change, B2 will
58+
* keep old versions of the file. This is very helpful, because it means the old
59+
* versions are there if the original file is accidentally deleted. On the other
60+
* hand, keeping them forever can clutter things up. For an application like this,
61+
* you might want a lifecycle rule that keeps old versions in the backup/ folder
62+
* for 30 days, and then deletes them:</p>
63+
*
64+
* <pre>
65+
{
66+
"daysFromHidingToDeleting": 30,
67+
"daysFromUploadingToHiding": null,
68+
"fileNamePrefix": "backup/"
69+
}
70+
* </pre>
71+
*
72+
* <p>The daysFromUploadingToHiding setting is less frequently used. It causes files to
73+
* be hidden automatically after the given number of days. This can be useful for things
74+
* like server log files that can be deleted after a while. This rule will keep files
75+
* in the logs/ folder for 7 days, and then hide and immediately delete them:</p>
76+
*
77+
* <pre>
78+
{
79+
"daysFromHidingToDeleting": 0,
80+
"daysFromUploadingToHiding": 7,
81+
"fileNamePrefix": "logs/"
82+
}
83+
* </pre>
84+
*
85+
* <p>The API calls related to lifecycle rules are:</p>
86+
*
87+
* <ul>
88+
* <li>b2_create_bucket - creates a new bucket</li>
89+
* <li>b2_update_bucket - changes the settings on a bucket</li>
90+
* </ul>
91+
*
92+
* <p>When you create a new bucket, you can specify the lifecycle rules. Later,
93+
* you can change the rules on a bucket by updating it.</p>
94+
*
95+
* @author synapticloop
96+
*
97+
*/
98+
public class LifecycleRule {
99+
private final Long daysFromHidingToDeleting;
100+
private final Long daysFromUploadingToHiding;
101+
private final String fileNamePrefix;
102+
103+
public LifecycleRule(Long daysFromHidingToDeleting, Long daysFromUploadingToHiding, String fileNamePrefix) {
104+
this.daysFromHidingToDeleting = daysFromHidingToDeleting;
105+
this.daysFromUploadingToHiding = daysFromUploadingToHiding;
106+
this.fileNamePrefix = fileNamePrefix;
107+
}
108+
109+
public LifecycleRule(JSONObject jsonObject) {
110+
long hide = jsonObject.optLong("daysFromHidingToDeleting", Long.MIN_VALUE);
111+
if(hide == Long.MIN_VALUE) {
112+
daysFromHidingToDeleting = null;
113+
} else {
114+
daysFromHidingToDeleting = hide;
115+
}
116+
117+
long upload = jsonObject.optLong("daysFromUploadingToHiding", Long.MIN_VALUE);
118+
if(upload == Long.MIN_VALUE) {
119+
daysFromUploadingToHiding = null;
120+
} else {
121+
daysFromUploadingToHiding = upload;
122+
}
123+
124+
fileNamePrefix = jsonObject.optString("fileNamePrefix", null);
125+
}
126+
127+
/**
128+
* The number of days from when the file was hidden to when it will be deleted
129+
*
130+
* @return the number of days from hiding to deleting
131+
*/
132+
public Long getDaysFromHidingToDeleting() { return this.daysFromHidingToDeleting; }
133+
134+
/**
135+
* The number of days from when the file was uploaded to when it will be deleted
136+
*
137+
* @return the number of days from uploading to deleting
138+
*/
139+
public Long getDaysFromUploadingToHiding() { return this.daysFromUploadingToHiding; }
140+
141+
/**
142+
* The file name prefix to which this rule applies
143+
*
144+
* @return the file name prefix to which this rule applies
145+
*/
146+
public String getFileNamePrefix() { return this.fileNamePrefix; }
147+
148+
}

src/main/java/synapticloop/b2/request/B2ListFileNamesRequest.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package synapticloop.b2.request;
22

3+
import java.io.IOException;
4+
35
/*
46
* Copyright (c) 2016 Synapticloop.
57
*
@@ -19,22 +21,19 @@
1921
import org.apache.http.impl.client.CloseableHttpClient;
2022
import org.apache.http.util.EntityUtils;
2123

22-
import java.io.IOException;
23-
2424
import synapticloop.b2.exception.B2ApiException;
2525
import synapticloop.b2.response.B2AuthorizeAccountResponse;
2626
import synapticloop.b2.response.B2ListFilesResponse;
2727

2828
/**
2929
* <p>Lists the names of all files in a bucket, starting at a given name.</p>
30-
* <p>
30+
*
3131
* <p>This call returns at most 1000 file names, but it can be called repeatedly to scan through all of the file names in a bucket. Each time you call, it returns an "endFileName" that can be used as the starting point for the next call.</p>
3232
* <p>There may be many file versions for the same name, but this call will return each name only once. If you want all of the versions, use b2_list_file_versions instead.</p>
33-
* <p>
34-
* <p>
35-
* This is the interaction class for the <strong>b2_list_file_names</strong> api calls, this was
36-
* generated from the backblaze api documentation - which can be found here:
37-
* <p>
33+
*
34+
* <p>This is the interaction class for the <strong>b2_list_file_names</strong> api calls, this was
35+
* generated from the backblaze api documentation - which can be found here:</p>
36+
*
3837
* <a href="http://www.backblaze.com/b2/docs/b2_list_file_names.html">http://www.backblaze.com/b2/docs/b2_list_file_names.html</a>
3938
*
4039
* @author synapticloop

src/main/java/synapticloop/b2/response/B2BucketResponse.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package synapticloop.b2.response;
22

3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Map;
6+
7+
import org.json.JSONArray;
8+
39
/*
410
* Copyright (c) 2016 Synapticloop.
511
*
@@ -21,6 +27,7 @@
2127
import org.slf4j.LoggerFactory;
2228

2329
import synapticloop.b2.BucketType;
30+
import synapticloop.b2.LifecycleRule;
2431
import synapticloop.b2.exception.B2ApiException;
2532

2633
public class B2BucketResponse extends BaseB2Response {
@@ -30,6 +37,9 @@ public class B2BucketResponse extends BaseB2Response {
3037
private final String accountId;
3138
private final String bucketName;
3239
private final String bucketType;
40+
private final Long revision;
41+
private final Map<String, String> bucketInfo;
42+
private final List<LifecycleRule> lifecycleRules = new ArrayList<LifecycleRule>();
3343

3444
/**
3545
* Instantiate a bucket response with the JSON response as a string from
@@ -46,6 +56,13 @@ public B2BucketResponse(String json) throws B2ApiException {
4656
this.accountId = this.readString(B2ResponseProperties.KEY_ACCOUNT_ID);
4757
this.bucketName = this.readString(B2ResponseProperties.KEY_BUCKET_NAME);
4858
this.bucketType = this.readString(B2ResponseProperties.KEY_BUCKET_TYPE);
59+
this.revision = this.readLong(B2ResponseProperties.KEY_REVISION);
60+
this.bucketInfo = this.readMap(B2ResponseProperties.KEY_BUCKET_INFO);
61+
62+
JSONArray lifecycleObjects = this.readObjects(B2ResponseProperties.KEY_LIFECYCLE_RULES);
63+
for (Object object : lifecycleObjects) {
64+
lifecycleRules.add(new LifecycleRule((JSONObject)object));
65+
}
4966

5067
this.warnOnMissedKeys();
5168
}
@@ -65,6 +82,13 @@ public B2BucketResponse(final JSONObject response) throws B2ApiException {
6582
this.accountId = this.readString(B2ResponseProperties.KEY_ACCOUNT_ID);
6683
this.bucketName = this.readString(B2ResponseProperties.KEY_BUCKET_NAME);
6784
this.bucketType = this.readString(B2ResponseProperties.KEY_BUCKET_TYPE);
85+
this.revision = this.readLong(B2ResponseProperties.KEY_REVISION);
86+
this.bucketInfo = this.readMap(B2ResponseProperties.KEY_BUCKET_INFO);
87+
88+
JSONArray lifecycleObjects = this.readObjects(B2ResponseProperties.KEY_LIFECYCLE_RULES);
89+
for (Object object : lifecycleObjects) {
90+
lifecycleRules.add(new LifecycleRule((JSONObject)object));
91+
}
6892

6993
this.warnOnMissedKeys();
7094
}
@@ -104,6 +128,28 @@ public BucketType getBucketType() {
104128
}
105129
}
106130

131+
/**
132+
* Get the map of the bucket info for the bucket that was operated on, or an
133+
* empty map if not set.
134+
*
135+
* @return the map of the file info for the file that was operated on
136+
*/
137+
public Map<String, String> getBucketInfo() { return this.bucketInfo; }
138+
139+
/**
140+
* Get the revision number for the bucket
141+
*
142+
* @return the revision number for the bucket
143+
*/
144+
public long getRevision() { return this.revision; }
145+
146+
/**
147+
* Return the list of all of the lifecycle rules that apply to this bucket
148+
*
149+
* @return the list of all lifecycle rules
150+
*/
151+
public List<LifecycleRule> getLifecycleRules() { return lifecycleRules; }
152+
107153
@Override
108154
protected Logger getLogger() { return LOGGER; }
109155

@@ -117,4 +163,5 @@ public String toString() {
117163
sb.append('}');
118164
return sb.toString();
119165
}
166+
120167
}

src/main/java/synapticloop/b2/response/B2ResponseProperties.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ public final class B2ResponseProperties {
2222
public static final String KEY_API_URL = "apiUrl";
2323
public static final String KEY_AUTHORIZATION_TOKEN = "authorizationToken";
2424
public static final String KEY_BUCKET_ID = "bucketId";
25+
public static final String KEY_BUCKET_INFO = "bucketInfo";
2526
public static final String KEY_BUCKET_NAME = "bucketName";
2627
public static final String KEY_BUCKET_TYPE = "bucketType";
28+
public static final String KEY_BUCKETS = "buckets";
2729
public static final String KEY_CONTENT_LENGTH = "contentLength";
2830
public static final String KEY_CONTENT_SHA1 = "contentSha1";
2931
public static final String KEY_CONTENT_TYPE = "contentType";
@@ -32,14 +34,15 @@ public final class B2ResponseProperties {
3234
public static final String KEY_FILE_INFO = "fileInfo";
3335
public static final String KEY_FILE_NAME = "fileName";
3436
public static final String KEY_FILES = "files";
37+
public static final String KEY_LIFECYCLE_RULES = "lifecycleRules";
38+
public static final String KEY_MINIMUM_PART_SIZE = "minimumPartSize";
3539
public static final String KEY_NEXT_FILE_ID = "nextFileId";
3640
public static final String KEY_NEXT_FILE_NAME = "nextFileName";
41+
public static final String KEY_NEXT_PART_NUMBER = "nextPartNumber";
42+
public static final String KEY_PART_NUMBER = "partNumber";
43+
public static final String KEY_PARTS = "parts";
44+
public static final String KEY_REVISION = "revision";
3745
public static final String KEY_SIZE = "size";
3846
public static final String KEY_UPLOAD_TIMESTAMP = "uploadTimestamp";
3947
public static final String KEY_UPLOAD_URL = "uploadUrl";
40-
public static final String KEY_BUCKETS = "buckets";
41-
public static final String KEY_PARTS = "parts";
42-
public static final String KEY_PART_NUMBER = "partNumber";
43-
public static final String KEY_NEXT_PART_NUMBER = "nextPartNumber";
44-
public static final String KEY_MINIMUM_PART_SIZE = "minimumPartSize";
4548
}

src/test/java/synapticloop/b2/request/B2DownloadFileRequestTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,14 @@ public void testDownloadFileByRange() throws Exception {
8585
0,
8686
5).getResponse();
8787

88-
assertEquals(B2TestHelper.DUMMY_FILE_CONTENT.substring(0, 6), IOUtils.toString(b2DownloadFileResponse.getContent()));
88+
assertEquals(B2TestHelper.DUMMY_FILE_CONTENT.substring(0, 6), IOUtils.toString(b2DownloadFileResponse.getContent(), Charset.defaultCharset()));
8989

9090
b2DownloadFileResponse = new B2DownloadFileByIdRequest(HttpClients.createDefault(), B2TestHelper.getB2AuthorizeAccountResponse(),
9191
b2FileResponse.getFileId(),
9292
0,
9393
5).getResponse();
9494

95-
assertEquals(B2TestHelper.DUMMY_FILE_CONTENT.substring(0, 6), IOUtils.toString(b2DownloadFileResponse.getContent()));
95+
assertEquals(B2TestHelper.DUMMY_FILE_CONTENT.substring(0, 6), IOUtils.toString(b2DownloadFileResponse.getContent(), Charset.defaultCharset()));
9696

9797
B2TestHelper.deleteFile(b2FileResponse.getFileName(), b2FileResponse.getFileId());
9898
B2TestHelper.deleteBucket(randomPrivateBucket.getBucketId());

0 commit comments

Comments
 (0)