IBM API Hub
IBM Aspera Transfer SDK

By IBM Aspera

The IBM Aspera Transfer SDK allows you to initiate, monitor and control file transfers and streams.


Use Java to upload a stream as a file

This page breaks down the sample code into individual steps so you can evaluate how to customize the code for your application.

To run the full Java® sample code for uploading a file, go to the /api folder in the installation directory.

For more information on using gRPC to help develop your language-specific client, see Quick start guide for Java.

Create a connection to the TransferD daemon.


// create a connection to transferd
final TransferServiceGrpc.TransferServiceStub client =
        TransferServiceGrpc.newStub(
                ManagedChannelBuilder.forAddress(
                        "localhost", 55002)
                        .usePlaintext().build());

Create the transfer specification

The transfer specification (transferSpec) defines the parameters for the transfer.

In this type of transfer, you must specify the total number of bytes to upload. The parameter "direction":"send" indicates that the transfer is an upload.

For more information, see transferSpec Definition.


    // create transfer spec string
    String transferSpec = "{" +
            "  \"session_initiation\": {" +
            "    \"ssh\": {" +
            "      \"ssh_port\": 33001," +
            "      \"remote_user\": \"aspera\"," +
            "      \"remote_password\": \"demoaspera\"" +
            "    }" +
            "  }," +
            "  \"direction\": \"send\"," +
            "  \"remote_host\": \"demo.asperasoft.com\"," +
            "  \"assets\": {" +
            "    \"destination_root\": \"/Upload\"," +
            "    \"paths\": [" +
            "       {" +
            "         \"destination\": \"file\"" +
            "       }" +
            "     ]" +
            "  }" +
            "}";

Create a transfer request

This example shows a transfer definition with these elements:

Transfer definition elements
transferType Identifies the transfer type, which in this example is STREAM_TO_FILE_UPLOAD
config Optional values for configuring the transfer. In this example, the configuration is setting the log level.
transferSpec The transfer specification created in the previous step

NOTE: For a complete description of the parameters that are in the transferSpec, see: transferd.html, transderd.md, or transfer.proto that are in the /api directory.


Send a startTransfer request to the TransferD daemon


    // send asynchronous start transfer request
    final CountDownLatch transferLatch = new CountDownLatch(1);
    client.startTransfer(Transferd.TransferRequest.newBuilder()
            .setTransferType(Transferd.TransferType.STREAM_TO_FILE_UPLOAD)
            .setConfig(Transferd.TransferConfig.newBuilder().build())
            .setTransferSpec(transferSpec)
            .build(), new StreamObserver<>() {
        @Override
        public void onNext(Transferd.StartTransferResponse response) {
            System.out.println(String.format("transfer started with id %s",
                    response.getTransferId()));
            // once the transfer starts, write data
            try {
                writeData(client, response.getTransferId());
            } catch (InterruptedException e) {
                System.out.println("failed to write data");
            }
            transferLatch.countDown();
        }
        @Override
        public void onError(Throwable t) {
            System.out.println("failed " + t.getMessage());
            transferLatch.countDown();
        }
        @Override
        public void onCompleted() {
            System.out.println("start transfer finished");
        }
    });
    // wait for our async transfer operation to finish
    transferLatch.await(10, TimeUnit.SECONDS);
    System.out.println("finished");
}

Get the write stream


    // get write stream observer
    CountDownLatch writeStreamLatch = new CountDownLatch(1);
    StreamObserver writeStreamObserver =
            pClient.writeStream(new StreamObserver<>() {
                @Override
                public void onNext(Transferd.WriteStreamResponse value) {
                    System.out.println(value);
                }
                @Override
                public void onError(Throwable t) {
                    System.out.println("error while writing stream " +
                            t.getMessage());
                }
                @Override
                public void onCompleted() {
                    System.out.println("write stream completed");
                    writeStreamLatch.countDown();
                }
            });

Write to the data stream, create a write request and then send the request with the writeStreamObserver

// java example
    // write to the stream
    int bytesWritten = 0;
    while (bytesWritten < size) {
        int writeSize = Math.min(size - bytesWritten, content.length());
        ByteString chunk = ByteString.copyFrom(
                content.substring(0, writeSize),
                StandardCharsets.UTF_8);
        // create write request
        Transferd.WriteStreamRequest writeStreamRequest =
                Transferd.WriteStreamRequest.newBuilder()
                        .setTransferId(pTransferId)
                        .setPath("file")
                        .setSize(size)
                        .setChunk(Transferd.Chunk.newBuilder()
                                .setContents(chunk).build())
                        .build();
        // send request via the observer
        writeStreamObserver.onNext(writeStreamRequest);
        bytesWritten += writeSize;
        System.out.println("bytes written " + bytesWritten);
    }
    // wait for the write stream to actually write
    writeStreamLatch.await(5, TimeUnit.SECONDS);
}
}
Legend
Technologies
Products & Services
Company information
Company logoIBM Aspera