コード例 #1
0
  @Test
  public void testDownloadFileBy() throws Exception {
    randomPrivateBucket = B2TestHelper.createRandomPrivateBucket();
    b2FileResponse = B2TestHelper.uploadTemporaryFileToBucket(randomPrivateBucket.getBucketId());

    B2DownloadFileResponse b2DownloadFileResponse =
        new B2DownloadFileByNameRequest(
                HttpClients.createDefault(),
                B2TestHelper.getB2AuthorizeAccountResponse(),
                randomPrivateBucket.getBucketName(),
                b2FileResponse.getFileName())
            .getResponse();
    assertEquals(
        B2TestHelper.DUMMY_FILE_CONTENT,
        IOUtils.toString(b2DownloadFileResponse.getContent(), Charset.defaultCharset()));

    b2DownloadFileResponse =
        new B2DownloadFileByIdRequest(
                HttpClients.createDefault(),
                B2TestHelper.getB2AuthorizeAccountResponse(),
                b2FileResponse.getFileId())
            .getResponse();
    assertEquals(
        B2TestHelper.DUMMY_FILE_CONTENT,
        IOUtils.toString(b2DownloadFileResponse.getContent(), Charset.defaultCharset()));

    B2TestHelper.deleteFile(b2FileResponse.getFileName(), b2FileResponse.getFileId());
    B2TestHelper.deleteBucket(randomPrivateBucket.getBucketId());
  }
コード例 #2
0
  @Test
  public void testDownloadByFilePath() throws Exception {
    randomPrivateBucket = B2TestHelper.createRandomPrivateBucket();
    B2GetUploadUrlResponse b2GetUploadUrlResponse =
        B2TestHelper.getUploadUrl(randomPrivateBucket.getBucketId());
    File file = File.createTempFile("backblaze-api-test", ".txt");
    FileWriter fileWriter = new FileWriter(file);
    fileWriter.write("hello world!");
    fileWriter.flush();
    fileWriter.close();

    String testFileName = "some/file/path/" + file.getName();
    B2FileResponse b2UploadFileResponse =
        new B2UploadFileRequest(
                HttpClients.createDefault(),
                B2TestHelper.getB2AuthorizeAccountResponse(),
                b2GetUploadUrlResponse,
                testFileName,
                file,
                ChecksumHelper.calculateSha1(file))
            .getResponse();

    String fileName = b2UploadFileResponse.getFileName();
    String fileId = b2UploadFileResponse.getFileId();

    B2DownloadFileResponse b2DownloadFileResponse =
        new B2DownloadFileByNameRequest(
                HttpClients.createDefault(),
                B2TestHelper.getB2AuthorizeAccountResponse(),
                randomPrivateBucket.getBucketName(),
                testFileName)
            .getResponse();
    assertEquals(fileName, b2DownloadFileResponse.getFileName());
    // now we need to delete the file as well to clean up after ourselves

    B2DeleteFileVersionResponse b2DeleteFileVersionResponse =
        new B2DeleteFileVersionRequest(
                HttpClients.createDefault(),
                B2TestHelper.getB2AuthorizeAccountResponse(),
                testFileName,
                fileId)
            .getResponse();

    assertEquals(fileName, b2DeleteFileVersionResponse.getFileName());
    assertEquals(fileId, b2DeleteFileVersionResponse.getFileId());

    B2TestHelper.deleteBucket(randomPrivateBucket.getBucketId());
  }