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

Commit f788bfe

Browse files
committed
Expose upload URI methods in client interface. The URL and authorization token that you get from b2_get_upload_url can be used by only one thread at a time. If you want multiple threads running, each one needs to get its own URL and auth token. It can keep using that URL and auth token for multiple uploads, until it gets a returned status indicating that it should get a new upload URL.
1 parent 3067fd5 commit f788bfe

1 file changed

Lines changed: 167 additions & 45 deletions

File tree

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

Lines changed: 167 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -231,27 +231,60 @@ public B2DownloadFileResponse headFileById(String fileId) throws B2ApiException,
231231
*
232232
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
233233

234+
/**
235+
* An uploadUrl and upload authorizationToken are valid for 24 hours or until the endpoint rejects an upload, see b2_upload_file.
236+
* You can upload as many files to this URL as you need. To achieve faster upload speeds, request multiple uploadUrls and
237+
* upload your files to these different endpoints in parallel.
238+
*
239+
* @param bucketId the id of the bucket to upload to
240+
* @return Upload URL. Not be shared between threads but can be used for multiple parts
241+
* @throws B2ApiException if there was an error with the request
242+
* @throws IOException if there was an error communicating with the API service
243+
*/
244+
public B2GetUploadUrlResponse getUploadUrl(String bucketId) throws B2ApiException, IOException {
245+
return new B2GetUploadUrlRequest(client, b2AuthorizeAccountResponse, bucketId).getResponse();
246+
}
247+
234248
/**
235249
* Upload a file to a bucket
236250
*
237-
* @param bucketId the id of the bucket
238-
* @param fileName the name of the file that will be placed in the bucket
239-
* (including any path separators '/')
240-
* @param entity the file content to upload
251+
* @param bucketId the id of the bucket
252+
* @param fileName the name of the file that will be placed in the bucket
253+
* (including any path separators '/')
254+
* @param entity the file content to upload
241255
* @param sha1Checksum the checksum for the file
242-
* @param mimeType the mime type of the file, if null, then the mime type
243-
* will be attempted to be automatically mapped by the backblaze B2 API
244-
* see <a href="https://www.backblaze.com/b2/docs/content-types.html">https://www.backblaze.com/b2/docs/content-types.html</a>
245-
* for a list of content type mappings.
246-
* @param fileInfo the file info map which will be set as 'X-Bz-Info-' headers
247-
*
256+
* @param mimeType the mime type of the file, if null, then the mime type
257+
* will be attempted to be automatically mapped by the backblaze B2 API
258+
* see <a href="https://www.backblaze.com/b2/docs/content-types.html">https://www.backblaze.com/b2/docs/content-types.html</a>
259+
* for a list of content type mappings.
260+
* @param fileInfo the file info map which will be set as 'X-Bz-Info-' headers
248261
* @return the uploaded file response
249-
*
250262
* @throws B2ApiException if there was an error uploading the file
251-
* @throws IOException if there was an error communicating with the API service
263+
* @throws IOException if there was an error communicating with the API service
252264
*/
253265
public B2FileResponse uploadFile(String bucketId, String fileName, HttpEntity entity, String sha1Checksum, String mimeType, Map<String, String> fileInfo) throws B2ApiException, IOException {
254-
B2GetUploadUrlResponse b2GetUploadUrlResponse = new B2GetUploadUrlRequest(client, b2AuthorizeAccountResponse, bucketId).getResponse();
266+
B2GetUploadUrlResponse b2GetUploadUrlResponse = this.getUploadUrl(bucketId);
267+
return this.uploadFile(b2GetUploadUrlResponse, fileName, entity, sha1Checksum, mimeType, fileInfo);
268+
}
269+
270+
/**
271+
* Upload a file to a bucket
272+
*
273+
* @param b2GetUploadUrlResponse Upload URL.
274+
* @param fileName the name of the file that will be placed in the bucket
275+
* (including any path separators '/')
276+
* @param entity the file content to upload
277+
* @param sha1Checksum the checksum for the file
278+
* @param mimeType the mime type of the file, if null, then the mime type
279+
* will be attempted to be automatically mapped by the backblaze B2 API
280+
* see <a href="https://www.backblaze.com/b2/docs/content-types.html">https://www.backblaze.com/b2/docs/content-types.html</a>
281+
* for a list of content type mappings.
282+
* @param fileInfo the file info map which will be set as 'X-Bz-Info-' headers
283+
* @return the uploaded file response
284+
* @throws B2ApiException if there was an error uploading the file
285+
* @throws IOException if there was an error communicating with the API service
286+
*/
287+
public B2FileResponse uploadFile(B2GetUploadUrlResponse b2GetUploadUrlResponse, String fileName, HttpEntity entity, String sha1Checksum, String mimeType, Map<String, String> fileInfo) throws B2ApiException, IOException {
255288
return new B2UploadFileRequest(client, b2AuthorizeAccountResponse, b2GetUploadUrlResponse, fileName, entity, sha1Checksum, mimeType, fileInfo).getResponse();
256289
}
257290

@@ -260,21 +293,39 @@ public B2FileResponse uploadFile(String bucketId, String fileName, HttpEntity en
260293
*
261294
* @param bucketId the id of the bucket
262295
* @param fileName the name of the file that will be placed in the bucket
263-
* (including any path separators '/')
264-
* @param file the file to upload
296+
* (including any path separators '/')
297+
* @param file the file to upload
265298
* @param mimeType the mime type of the file, if null, then the mime type
266-
* will be attempted to be automatically mapped by the backblaze B2 API
267-
* see <a href="https://www.backblaze.com/b2/docs/content-types.html">https://www.backblaze.com/b2/docs/content-types.html</a>
268-
* for a list of content type mappings.
299+
* will be attempted to be automatically mapped by the backblaze B2 API
300+
* see <a href="https://www.backblaze.com/b2/docs/content-types.html">https://www.backblaze.com/b2/docs/content-types.html</a>
301+
* for a list of content type mappings.
269302
* @param fileInfo the file info map which will be set as 'X-Bz-Info-' headers
270-
*
271303
* @return the uploaded file response
272-
*
273304
* @throws B2ApiException if there was an error uploading the file
274-
* @throws IOException if there was an error communicating with the API service
305+
* @throws IOException if there was an error communicating with the API service
275306
*/
276307
public B2FileResponse uploadFile(String bucketId, String fileName, File file, String mimeType, Map<String, String> fileInfo) throws B2ApiException, IOException {
277-
B2GetUploadUrlResponse b2GetUploadUrlResponse = new B2GetUploadUrlRequest(client, b2AuthorizeAccountResponse, bucketId).getResponse();
308+
B2GetUploadUrlResponse b2GetUploadUrlResponse = this.getUploadUrl(bucketId);
309+
return this.uploadFile(b2GetUploadUrlResponse, fileName, file, mimeType, fileInfo);
310+
}
311+
312+
/**
313+
* Upload a file to a bucket
314+
*
315+
* @param b2GetUploadUrlResponse Upload URL.
316+
* @param fileName the name of the file that will be placed in the bucket
317+
* (including any path separators '/')
318+
* @param file the file to upload
319+
* @param mimeType the mime type of the file, if null, then the mime type
320+
* will be attempted to be automatically mapped by the backblaze B2 API
321+
* see <a href="https://www.backblaze.com/b2/docs/content-types.html">https://www.backblaze.com/b2/docs/content-types.html</a>
322+
* for a list of content type mappings.
323+
* @param fileInfo the file info map which will be set as 'X-Bz-Info-' headers
324+
* @return the uploaded file response
325+
* @throws B2ApiException if there was an error uploading the file
326+
* @throws IOException if there was an error communicating with the API service
327+
*/
328+
public B2FileResponse uploadFile(B2GetUploadUrlResponse b2GetUploadUrlResponse, String fileName, File file, String mimeType, Map<String, String> fileInfo) throws B2ApiException, IOException {
278329
return new B2UploadFileRequest(client, b2AuthorizeAccountResponse, b2GetUploadUrlResponse, fileName, file,
279330
ChecksumHelper.calculateSha1(file), mimeType, fileInfo).getResponse();
280331
}
@@ -284,18 +335,31 @@ public B2FileResponse uploadFile(String bucketId, String fileName, File file, St
284335
*
285336
* @param bucketId the id of the bucket
286337
* @param fileName the name of the file that will be placed in the bucket
287-
* (including any path separators '/')
288-
* @param file the file to upload
338+
* (including any path separators '/')
339+
* @param file the file to upload
289340
* @param fileInfo the file info map which will be set as 'X-Bz-Info-' headers
290-
*
291341
* @return the uploaded file response
292-
*
293342
* @throws B2ApiException if there was an error uploading the file
294-
* @throws IOException if there was an error communicating with the API service
343+
* @throws IOException if there was an error communicating with the API service
295344
*/
296-
297345
public B2FileResponse uploadFile(String bucketId, String fileName, File file, Map<String, String> fileInfo) throws B2ApiException, IOException {
298-
B2GetUploadUrlResponse b2GetUploadUrlResponse = new B2GetUploadUrlRequest(client, b2AuthorizeAccountResponse, bucketId).getResponse();
346+
B2GetUploadUrlResponse b2GetUploadUrlResponse = this.getUploadUrl(bucketId);
347+
return this.uploadFile(b2GetUploadUrlResponse, fileName, file, fileInfo);
348+
}
349+
350+
/**
351+
* Upload a file to a bucket
352+
*
353+
* @param b2GetUploadUrlResponse Upload URL.
354+
* @param fileName the name of the file that will be placed in the bucket
355+
* (including any path separators '/')
356+
* @param file the file to upload
357+
* @param fileInfo the file info map which will be set as 'X-Bz-Info-' headers
358+
* @return the uploaded file response
359+
* @throws B2ApiException if there was an error uploading the file
360+
* @throws IOException if there was an error communicating with the API service
361+
*/
362+
public B2FileResponse uploadFile(B2GetUploadUrlResponse b2GetUploadUrlResponse, String fileName, File file, Map<String, String> fileInfo) throws B2ApiException, IOException {
299363
return new B2UploadFileRequest(client, b2AuthorizeAccountResponse, b2GetUploadUrlResponse, fileName, file,
300364
ChecksumHelper.calculateSha1(file), fileInfo).getResponse();
301365
}
@@ -305,20 +369,37 @@ public B2FileResponse uploadFile(String bucketId, String fileName, File file, Ma
305369
*
306370
* @param bucketId the id of the bucket
307371
* @param fileName the name of the file that will be placed in the bucket
308-
* (including any path separators '/')
309-
* @param file the file to upload
372+
* (including any path separators '/')
373+
* @param file the file to upload
310374
* @param mimeType the mime type of the file, if null, then the mime type
311-
* will be attempted to be automatically mapped by the backblaze B2 API
312-
* see <a href="https://www.backblaze.com/b2/docs/content-types.html">https://www.backblaze.com/b2/docs/content-types.html</a>
313-
* for a list of content type mappings.
314-
*
375+
* will be attempted to be automatically mapped by the backblaze B2 API
376+
* see <a href="https://www.backblaze.com/b2/docs/content-types.html">https://www.backblaze.com/b2/docs/content-types.html</a>
377+
* for a list of content type mappings.
315378
* @return the uploaded file response
316-
*
317379
* @throws B2ApiException if there was an error uploading the file
318-
* @throws IOException if there was an error communicating with the API service
380+
* @throws IOException if there was an error communicating with the API service
319381
*/
320382
public B2FileResponse uploadFile(String bucketId, String fileName, File file, String mimeType) throws B2ApiException, IOException {
321-
B2GetUploadUrlResponse b2GetUploadUrlResponse = new B2GetUploadUrlRequest(client, b2AuthorizeAccountResponse, bucketId).getResponse();
383+
B2GetUploadUrlResponse b2GetUploadUrlResponse = this.getUploadUrl(bucketId);
384+
return this.uploadFile(b2GetUploadUrlResponse, fileName, file, mimeType);
385+
}
386+
387+
/**
388+
* Upload a file to a bucket
389+
*
390+
* @param b2GetUploadUrlResponse Upload URL.
391+
* @param fileName the name of the file that will be placed in the bucket
392+
* (including any path separators '/')
393+
* @param file the file to upload
394+
* @param mimeType the mime type of the file, if null, then the mime type
395+
* will be attempted to be automatically mapped by the backblaze B2 API
396+
* see <a href="https://www.backblaze.com/b2/docs/content-types.html">https://www.backblaze.com/b2/docs/content-types.html</a>
397+
* for a list of content type mappings.
398+
* @return the uploaded file response
399+
* @throws B2ApiException if there was an error uploading the file
400+
* @throws IOException if there was an error communicating with the API service
401+
*/
402+
public B2FileResponse uploadFile(B2GetUploadUrlResponse b2GetUploadUrlResponse, String fileName, File file, String mimeType) throws B2ApiException, IOException {
322403
return new B2UploadFileRequest(client, b2AuthorizeAccountResponse, b2GetUploadUrlResponse, fileName, file,
323404
ChecksumHelper.calculateSha1(file), mimeType).getResponse();
324405
}
@@ -329,16 +410,30 @@ public B2FileResponse uploadFile(String bucketId, String fileName, File file, St
329410
*
330411
* @param bucketId the id of the bucket
331412
* @param fileName the name of the file that will be placed in the bucket
332-
* (including any path separators '/')
333-
* @param file the file to upload
334-
*
413+
* (including any path separators '/')
414+
* @param file the file to upload
335415
* @return the uploaded file response
336-
*
337416
* @throws B2ApiException if there was an error uploading the file
338-
* @throws IOException if there was an error communicating with the API service
417+
* @throws IOException if there was an error communicating with the API service
339418
*/
340419
public B2FileResponse uploadFile(String bucketId, String fileName, File file) throws B2ApiException, IOException {
341-
B2GetUploadUrlResponse b2GetUploadUrlResponse = new B2GetUploadUrlRequest(client, b2AuthorizeAccountResponse, bucketId).getResponse();
420+
B2GetUploadUrlResponse b2GetUploadUrlResponse = this.getUploadUrl(bucketId);
421+
return this.uploadFile(b2GetUploadUrlResponse, fileName, file);
422+
}
423+
424+
/**
425+
* Upload a file to a bucket, the mimetype will be automatically set by the
426+
* back-end B2 API system
427+
*
428+
* @param b2GetUploadUrlResponse Upload URL.
429+
* @param fileName the name of the file that will be placed in the bucket
430+
* (including any path separators '/')
431+
* @param file the file to upload
432+
* @return the uploaded file response
433+
* @throws B2ApiException if there was an error uploading the file
434+
* @throws IOException if there was an error communicating with the API service
435+
*/
436+
public B2FileResponse uploadFile(B2GetUploadUrlResponse b2GetUploadUrlResponse, String fileName, File file) throws B2ApiException, IOException {
342437
return new B2UploadFileRequest(client, b2AuthorizeAccountResponse, b2GetUploadUrlResponse, fileName,
343438
file, ChecksumHelper.calculateSha1(file)).getResponse();
344439
}
@@ -349,6 +444,19 @@ public B2FileResponse uploadFile(String bucketId, String fileName, File file) th
349444
*
350445
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
351446

447+
/**
448+
* When you upload part of a large file to B2, you must call b2_get_upload_part_url first to get the URL for uploading.
449+
* Then, you use b2_upload_part on this URL to upload your data.
450+
*
451+
* @param fileId the id of the file to upload
452+
* @return Upload URL. Not be shared between threads but can be used for multiple parts
453+
* @throws B2ApiException if there was an error with the request
454+
* @throws IOException if there was an error communicating with the API service
455+
*/
456+
public B2GetUploadPartUrlResponse getUploadPartUrl(String fileId) throws B2ApiException, IOException {
457+
return new B2GetUploadPartUrlRequest(client, b2AuthorizeAccountResponse, fileId).getResponse();
458+
}
459+
352460
/**
353461
* Start large file upload
354462
*
@@ -399,7 +507,21 @@ public B2FinishLargeFileResponse finishLargeFileUpload(String fileId, String[] p
399507
* @throws B2ApiException if there was an error uploading the file
400508
*/
401509
public B2UploadPartResponse uploadLargeFilePart(String fileId, int partNumber, HttpEntity entity, String sha1Checksum) throws B2ApiException, IOException {
402-
final B2GetUploadPartUrlResponse b2GetUploadUrlResponse = new B2GetUploadPartUrlRequest(client, b2AuthorizeAccountResponse, fileId).getResponse();
510+
final B2GetUploadPartUrlResponse b2GetUploadUrlResponse = this.getUploadPartUrl(fileId);
511+
return this.uploadLargeFilePart(b2GetUploadUrlResponse, partNumber, entity, sha1Checksum);
512+
}
513+
514+
/**
515+
* Upload large file upload part
516+
*
517+
* @param b2GetUploadUrlResponse Upload URL.
518+
* @param partNumber A number from 1 to 10000. The parts uploaded for one file must have contiguous numbers, starting with 1.
519+
* @param entity Part content body
520+
* @param sha1Checksum the checksum for the part
521+
* @return Upload response
522+
* @throws B2ApiException if there was an error uploading the file
523+
*/
524+
public B2UploadPartResponse uploadLargeFilePart(B2GetUploadPartUrlResponse b2GetUploadUrlResponse, int partNumber, HttpEntity entity, String sha1Checksum) throws B2ApiException, IOException {
403525
return new B2UploadPartRequest(client, b2AuthorizeAccountResponse, b2GetUploadUrlResponse, partNumber, entity, sha1Checksum).getResponse();
404526
}
405527

0 commit comments

Comments
 (0)