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

Commit d41b20b

Browse files
SynapticloopSynapticloop
authored andcommitted
updated documentation and calls
1 parent 06c73a5 commit d41b20b

19 files changed

Lines changed: 456 additions & 52 deletions

src/main/java/synapticloop/b2/ActionType.java renamed to src/main/java/synapticloop/b2/Action.java

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

3-
public enum ActionType {
3+
/**
4+
* The action for an associated file.
5+
*
6+
* @author synapticloop
7+
*/
8+
public enum Action {
49
HIDE("hide"),
510
UPLOAD("upload");
611

712
private final String actionType;
813

9-
ActionType(String actionType) {
14+
Action(String actionType) {
1015
this.actionType = actionType;
1116
}
1217

src/main/java/synapticloop/b2/B2ApiClient.java

Lines changed: 218 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.io.IOException;
55
import java.io.InputStream;
66
import java.util.List;
7+
import java.util.Map;
78

89
import org.apache.commons.io.FileUtils;
910
import org.apache.commons.io.IOUtils;
@@ -26,10 +27,17 @@
2627
import synapticloop.b2.response.B2BucketResponse;
2728
import synapticloop.b2.response.B2DeleteFileVersionResponse;
2829
import synapticloop.b2.response.B2DownloadFileResponse;
30+
import synapticloop.b2.response.B2FileInfoResponse;
2931
import synapticloop.b2.response.B2FileResponse;
3032
import synapticloop.b2.response.B2GetUploadUrlResponse;
3133
import synapticloop.b2.response.B2ListFilesResponse;
3234

35+
/**
36+
* This is a wrapper class for the underlying calls to the request/response
37+
* classes.
38+
*
39+
* @author synapticloop
40+
*/
3341
public class B2ApiClient {
3442
private String accountId = null;
3543
private String applicationKey = null;
@@ -55,7 +63,8 @@ public B2ApiClient(String accountId, String applicationKey) {
5563
*
5664
* @return the newly created bucket
5765
*
58-
* @throws B2ApiException if something went wrong
66+
* @throws B2ApiException if the bucket could not be created, of there was an
67+
* error with the authentication
5968
*/
6069
public B2BucketResponse createBucket(String bucketName, BucketType bucketType) throws B2ApiException {
6170
return(new B2CreateBucketRequest(getB2AuthorizeAccountResponse(), bucketName, bucketType).getResponse());
@@ -74,6 +83,39 @@ public B2BucketResponse deleteBucket(String bucketId) throws B2ApiException {
7483
return(new B2DeleteBucketRequest(getB2AuthorizeAccountResponse(), bucketId).getResponse());
7584
}
7685

86+
/**
87+
* Delete a bucket including all of the files that reside within the bucket
88+
*
89+
* @param bucketId the id of the bucker to delete
90+
*
91+
* @return the deleted bucket response
92+
*
93+
* @throws B2ApiException if there was an error deleting the bucket, or any
94+
* of the enclosed files
95+
*/
96+
public B2BucketResponse deleteBucketFully(String bucketId) throws B2ApiException {
97+
B2ListFilesResponse b2ListFilesResponse = new B2ListFileVersionsRequest(getB2AuthorizeAccountResponse(), bucketId, 1000).getResponse();
98+
String nextFileName = b2ListFilesResponse.getNextFileName();
99+
String nextFileId = b2ListFilesResponse.getNextFileId();
100+
while(true) {
101+
List<B2FileInfoResponse> files = b2ListFilesResponse.getFiles();
102+
for (B2FileInfoResponse b2FileInfoResponse : files) {
103+
new B2DeleteFileVersionRequest(getB2AuthorizeAccountResponse(), b2FileInfoResponse.getFileName(), b2FileInfoResponse.getFileId()).getResponse();
104+
}
105+
106+
if(null == nextFileName) {
107+
break;
108+
} else {
109+
b2ListFilesResponse = new B2ListFileVersionsRequest(getB2AuthorizeAccountResponse(), bucketId, nextFileName, nextFileId, 1000).getResponse();
110+
nextFileName = b2ListFilesResponse.getNextFileName();
111+
nextFileId = b2ListFilesResponse.getNextFileId();
112+
}
113+
}
114+
115+
// now delete the bucket
116+
return(new B2DeleteBucketRequest(getB2AuthorizeAccountResponse(), bucketId).getResponse());
117+
}
118+
77119
/**
78120
* List all of the buckets in the account
79121
*
@@ -85,32 +127,147 @@ public List<B2BucketResponse> listBuckets() throws B2ApiException {
85127
return(new B2ListBucketsRequest(getB2AuthorizeAccountResponse()).getResponse());
86128
}
87129

130+
/**
131+
* Retrieve the file information for a particular fileId, this includes all
132+
* of the 'X-Bz-Info-*' headers that were passed in with the file creation
133+
* in a map (without the 'X-Bz-Info-' prefix). For example, if you uploaded
134+
* a file with a header X-Bz-Info-Tag, the returned file info map would
135+
* contain a key of 'Tag'
136+
*
137+
* @param fileId the file ID to retrieve the information on
138+
*
139+
* @return the File Response
140+
* @throws B2ApiException
141+
*/
88142
public B2FileResponse getFileInfo(String fileId) throws B2ApiException {
89143
return(new B2GetFileInfoRequest(getB2AuthorizeAccountResponse(), fileId).getResponse());
90144
}
91145

92-
public B2UploadFileRequest uploadFile(String bucketId, String fileName, File file) throws B2ApiException {
146+
/**
147+
* Upload a file to a bucket
148+
*
149+
* @param bucketId the id of the bucket
150+
* @param fileName the name of the file that will be placed in the bucket
151+
* (including any path separators '/')
152+
* @param file the file to upload
153+
* @param mimeType the mime type of the file, if null, then the mime type
154+
* will be attempted to be automatically mapped by the backblaze B2 API
155+
* see <a href="https://www.backblaze.com/b2/docs/content-types.html">https://www.backblaze.com/b2/docs/content-types.html</a>
156+
* for a list of content type mappings.
157+
* @param fileInfo the file info map which will be set as 'X-Bz-Info-' headers
158+
*
159+
* @return the uploaded file response
160+
*
161+
* @throws B2ApiException if there was an error uploading the file
162+
*/
163+
public B2UploadFileRequest uploadFile(String bucketId, String fileName, File file, String mimeType, Map<String, String> fileInfo) throws B2ApiException {
93164
B2GetUploadUrlResponse b2GetUploadUrlResponse = new B2GetUploadUrlRequest(getB2AuthorizeAccountResponse(), bucketId).getResponse();
165+
return(new B2UploadFileRequest(getB2AuthorizeAccountResponse(), b2GetUploadUrlResponse, fileName, file, mimeType, fileInfo));
166+
}
167+
168+
/**
169+
* Upload a file to a bucket
170+
*
171+
* @param bucketId the id of the bucket
172+
* @param fileName the name of the file that will be placed in the bucket
173+
* (including any path separators '/')
174+
* @param file the file to upload
175+
* @param fileInfo the file info map which will be set as 'X-Bz-Info-' headers
176+
*
177+
* @return the uploaded file response
178+
*
179+
* @throws B2ApiException if there was an error uploading the file
180+
*/
181+
182+
public B2UploadFileRequest uploadFile(String bucketId, String fileName, File file, Map<String, String> fileInfo) throws B2ApiException {
183+
B2GetUploadUrlResponse b2GetUploadUrlResponse = new B2GetUploadUrlRequest(getB2AuthorizeAccountResponse(), bucketId, fileInfo).getResponse();
94184
return(new B2UploadFileRequest(getB2AuthorizeAccountResponse(), b2GetUploadUrlResponse, fileName, file));
95185
}
96186

187+
/**
188+
* Upload a file to a bucket
189+
*
190+
* @param bucketId the id of the bucket
191+
* @param fileName the name of the file that will be placed in the bucket
192+
* (including any path separators '/')
193+
* @param file the file to upload
194+
* @param mimeType the mime type of the file, if null, then the mime type
195+
* will be attempted to be automatically mapped by the backblaze B2 API
196+
* see <a href="https://www.backblaze.com/b2/docs/content-types.html">https://www.backblaze.com/b2/docs/content-types.html</a>
197+
* for a list of content type mappings.
198+
*
199+
* @return the uploaded file response
200+
*
201+
* @throws B2ApiException if there was an error uploading the file
202+
*/
97203
public B2UploadFileRequest uploadFile(String bucketId, String fileName, File file, String mimeType) throws B2ApiException {
98204
B2GetUploadUrlResponse b2GetUploadUrlResponse = new B2GetUploadUrlRequest(getB2AuthorizeAccountResponse(), bucketId).getResponse();
99205
return(new B2UploadFileRequest(getB2AuthorizeAccountResponse(), b2GetUploadUrlResponse, fileName, file, mimeType));
100206
}
101207

208+
/**
209+
* Upload a file to a bucket, the mimetype will be automatically set by the
210+
* back-end B2 API system
211+
*
212+
* @param bucketId the id of the bucket
213+
* @param fileName the name of the file that will be placed in the bucket
214+
* (including any path separators '/')
215+
* @param file the file to upload
216+
*
217+
* @return the uploaded file response
218+
*
219+
* @throws B2ApiException if there was an error uploading the file
220+
*/
221+
public B2UploadFileRequest uploadFile(String bucketId, String fileName, File file) throws B2ApiException {
222+
B2GetUploadUrlResponse b2GetUploadUrlResponse = new B2GetUploadUrlRequest(getB2AuthorizeAccountResponse(), bucketId).getResponse();
223+
return(new B2UploadFileRequest(getB2AuthorizeAccountResponse(), b2GetUploadUrlResponse, fileName, file));
224+
}
225+
102226
public B2DeleteFileVersionResponse deleteFileVersion(String fileName, String fileId) throws B2ApiException {
103227
return(new B2DeleteFileVersionRequest(getB2AuthorizeAccountResponse(), fileName, fileId).getResponse());
104228
}
105229

230+
/**
231+
* Update a busket to be a specified type
232+
*
233+
* @param bucketId the id of the bucket to set
234+
* @param bucketType the type of the bucket
235+
*
236+
* @return the bucket response
237+
*
238+
* @throws B2ApiException if there was an error updating the bucket
239+
*/
106240
public B2BucketResponse updateBucket(String bucketId, BucketType bucketType) throws B2ApiException {
107241
return(new B2UpdateBucketRequest(getB2AuthorizeAccountResponse(), bucketId, bucketType).getResponse());
108242
}
109243

244+
/**
245+
* Return a list of all of the files within a bucket with the specified ID,
246+
* by default a maximum of 100 files are returned with this request.
247+
*
248+
* @param bucketId the id of the bucket to list files
249+
*
250+
* @return the list files response
251+
*
252+
* @throws B2ApiException if there was an error with the call
253+
*/
110254
public B2ListFilesResponse listFileNames(String bucketId) throws B2ApiException {
111255
return(new B2ListFileNamesRequest(getB2AuthorizeAccountResponse(), bucketId).getResponse());
112256
}
113257

258+
/**
259+
* Return a list of all of the files within a bucket with the specified ID,
260+
* by default a maximum of 100 files are returned with this request.
261+
*
262+
* @param bucketId the id of the bucket to list
263+
* @param startFileName the start file name, or if null, this will be the first file
264+
* @param maxFileCount (optional) if null, the default is 100, the maximum number
265+
* to be returned is 1000
266+
*
267+
* @return the list of files response
268+
*
269+
* @throws B2ApiException if there was an error with the call,
270+
*/
114271
public B2ListFilesResponse listFileNames(String bucketId, String startFileName, Integer maxFileCount) throws B2ApiException {
115272
return(new B2ListFileNamesRequest(getB2AuthorizeAccountResponse(), bucketId, startFileName, maxFileCount).getResponse());
116273
}
@@ -127,6 +284,20 @@ public B2ListFilesResponse listFileVersions(String bucketId, String startFileNam
127284
return(new B2ListFileVersionsRequest(getB2AuthorizeAccountResponse(), bucketId, startFileName, startFileId, maxFileCount).getResponse());
128285
}
129286

287+
/**
288+
* Download a named file from a named bucket to an output file. This is a
289+
* utility method which will automatically write the content to the file.
290+
*
291+
* Note: This will not return any of the headers that accompanied the download.
292+
* See downloadFileByName to retrieve the complete response including sha1,
293+
* content length, content type and all headers.
294+
*
295+
* @param bucketName The name of the bucket to download the file from
296+
* @param fileName the name of the file to download
297+
* @param file the file to write out the data to
298+
*
299+
* @throws B2ApiException if there was an error with the call
300+
*/
130301
public void downloadFileByNameToFile(String bucketName, String fileName, File file) throws B2ApiException {
131302
try {
132303
FileUtils.copyInputStreamToFile(new B2DownloadFileByNameRequest(getB2AuthorizeAccountResponse(), bucketName, fileName).getResponse().getContent(), file);
@@ -135,6 +306,22 @@ public void downloadFileByNameToFile(String bucketName, String fileName, File fi
135306
}
136307
}
137308

309+
/**
310+
* Download a named file from a named bucket to an byte[]. This is a
311+
* utility method which will automatically convert the response stream to a
312+
* byte[].
313+
*
314+
* Note: This will not return any of the headers that accompanied the download.
315+
* See downloadFileByName to retrieve the complete response including sha1,
316+
* content length, content type and all headers.
317+
*
318+
* @param bucketName The name of the bucket to download the file from
319+
* @param fileName the name of the file to download
320+
*
321+
* @return the array of bytes from the download
322+
*
323+
* @throws B2ApiException if there was an error with the call
324+
*/
138325
public byte[] downloadFileByNameToBytes(String bucketName, String fileName) throws B2ApiException {
139326
try {
140327
return(IOUtils.toByteArray(new B2DownloadFileByNameRequest(getB2AuthorizeAccountResponse(), bucketName, fileName).getResponse().getContent()));
@@ -143,10 +330,39 @@ public byte[] downloadFileByNameToBytes(String bucketName, String fileName) thro
143330
}
144331
}
145332

333+
/**
334+
* Download a named file from a named bucket and return the input stream from
335+
* the HTTP response. This is a utility method which will automatically return
336+
* the response stream.
337+
*
338+
* Note: This will not return any of the headers that accompanied the download.
339+
* See downloadFileByName to retrieve the complete response including sha1,
340+
* content length, content type and all headers.
341+
*
342+
* @param bucketName The name of the bucket to download the file from
343+
* @param fileName the name of the file to download
344+
*
345+
* @return the input stream
346+
*
347+
* @throws B2ApiException if there was an error with the call
348+
*/
146349
public InputStream downloadFileByNameToStream(String bucketName, String fileName) throws B2ApiException {
147350
return(new B2DownloadFileByNameRequest(getB2AuthorizeAccountResponse(), bucketName, fileName).getResponse().getContent());
148351
}
149352

353+
/**
354+
* Download a named file from a named bucket and return the download file
355+
* response, which includes the headers, the file info and the response
356+
* stream.
357+
*
358+
*
359+
* @param bucketName The name of the bucket to download the file from
360+
* @param fileName the name of the file to download
361+
*
362+
* @return the download file response
363+
*
364+
* @throws B2ApiException if there was an error with the call
365+
*/
150366
public B2DownloadFileResponse downloadFileByName(String bucketName, String fileName) throws B2ApiException {
151367
return(new B2DownloadFileByNameRequest(getB2AuthorizeAccountResponse(), bucketName, fileName).getResponse());
152368
}

src/main/java/synapticloop/b2/BucketType.java

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

3+
/**
4+
* The type of bucket, either allPrivate, or allPublic
5+
*
6+
* @author synapticloop
7+
*/
38
public enum BucketType {
49
ALL_PUBLIC("allPublic"),
510
ALL_PRIVATE("allPrivate");

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
import synapticloop.b2.response.B2AuthorizeAccountResponse;
77

88
/**
9-
* <p>Used to log in to the B2 API. Returns an authorization token that can be used for account-level operations, and a URL that should be used as the base URL for subsequent API calls.</p>
9+
* <p>Used to log in to the B2 API. Returns an authorization token that can be
10+
* used for account-level operations, and a URL that should be used as the base
11+
* URL for subsequent API calls.</p>
1012
*
1113
*
1214
* This is the interaction class for the <strong>b2_authorize_account</strong> api calls, this was
@@ -19,12 +21,25 @@
1921
public class B2AuthorizeAccountRequest extends BaseB2Request {
2022
private static final String B2_AUTHORIZE_ACCOUNT = BASE_API + "b2_authorize_account";
2123

24+
/**
25+
* Instantiate a new authorize account request
26+
*
27+
* @param accountId the account id
28+
* @param applicationKey the application key
29+
*/
2230
public B2AuthorizeAccountRequest(String accountId, String applicationKey) {
2331
super(null);
2432
url = B2_AUTHORIZE_ACCOUNT;
2533
headers.put(REQUEST_PROPERTY_AUTHORIZATION, "Basic " + Base64.getEncoder().encodeToString((accountId + ":" + applicationKey).getBytes()));
2634
}
2735

36+
/**
37+
* Execute the call and return the authorize response
38+
*
39+
* @return the authorize response
40+
*
41+
* @throws B2ApiException if there was an error with the call
42+
*/
2843
public B2AuthorizeAccountResponse getResponse() throws B2ApiException {
2944
return(new B2AuthorizeAccountResponse(executeGet()));
3045
}

0 commit comments

Comments
 (0)