@Override
  public boolean archive(String gcsUrl, byte[] data) {
    BlobInfo blobInfo = parseBlobInfo(gcsUrl);

    if (data.length <= options.chunkUploadThresholdInBytes) {
      // Create the blob in one request.
      logger.atInfo().log("Archiving data to GCS at '%s' in one request.", gcsUrl);
      storage.create(blobInfo, data);
      return true;
    }

    // When content is large (1MB or more) it is recommended to write it in chunks via the blob's
    // channel writer.
    logger.atInfo().log(
        "Content is larger than threshold, archiving data to GCS at '%s' in chunks.", gcsUrl);
    try (WriteChannel writer = storage.writer(blobInfo)) {
      for (int chunkOffset = 0;
          chunkOffset < data.length;
          chunkOffset += options.chunkSizeInBytes) {
        int chunkSize = Math.min(data.length - chunkOffset, options.chunkSizeInBytes);
        writer.write(ByteBuffer.wrap(data, chunkOffset, chunkSize));
      }
      return true;
    } catch (IOException e) {
      logger.atSevere().withCause(e).log("Unable to archving data to GCS at '%s'.", gcsUrl);
      return false;
    }
  }
@Test
  public void archive_withLargeSizeBlob_createsBlobWithWriter() throws IOException {
    options.chunkSizeInBytes = 8;
    options.chunkUploadThresholdInBytes = 16;
    doReturn(mockWriter)
        .when(mockStorage)
        .writer(eq(BlobInfo.newBuilder(BUCKET_ID, OBJECT_ID).build()));
    GoogleCloudStorageArchiver archiver = archiverFactory.create(mockStorage);
    byte[] dataToArchive = newPreFilledByteArray(20);
    int numOfChunks = (int) Math.ceil((double) dataToArchive.length / options.chunkSizeInBytes);

    boolean succeeded = archiver.archive(buildGcsUrl(BUCKET_ID, OBJECT_ID), dataToArchive);

    assertThat(succeeded).isTrue();
    verify(mockWriter, times(numOfChunks)).write(byteBufferCaptor.capture());
    assertThat(byteBufferCaptor.getAllValues())
        .containsExactly(
            ByteBuffer.wrap(dataToArchive, 0, 8),
            ByteBuffer.wrap(dataToArchive, 8, 8),
            ByteBuffer.wrap(dataToArchive, 16, 4));
  }