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 Node.js to download a file as stream

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 Node.js 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 the Quick Start Guide for gRPC, Quick start guide for gRPC.

Create a connection to the Transferd daemon


// create a connection to the transfer manager daemon
let client = new transferdApi.TransferService('localhost:55002',
    grpc.credentials.createInsecure());

Create the transfer specification

The transfer specification (transferSpec) defines the parameters for the transfer. In this type of transfer, you must specify the size of the source file. The parameter direction: 'receive' indicates that the transfer is an upload.

Note: In the file_system object, you must declare the value of the overwrite option as always or nevers.

For more information, see transferSpec Definition.


// create transfer spec string
const transferSpec = {
    session_initiation: {
        ssh: {
            ssh_port: 33001,
            remote_user: 'aspera',
            remote_password: 'demoaspera'
        }
    },
    direction: 'recv',
    file_system: {
        // file to stream downloads require an overwrite option
        overwrite: 'always'
    },
    remote_host: 'demo.asperasoft.com',
    assets: {
        paths: [
            {
                source: 'aspera-test-dir-small/10MB.1'
            },
            {
                source: 'aspera-test-dir-tiny/200KB.2'
            },
            {
                source: 'aspera-test-dir-tiny/200KB.3'
            }
        ]
    }
}
        

Create a transfer request

This example shows a transfer definition with these elements:

  • transferType - Identifies the transfer type, which in this case is FILE-TO-STREAM-DOWNLOAD.
  • config - Optional configuration values.
  • transferSpec - The transfer specification created in the previous step.

See the API documentation for full details.


let startTransferRequest = {
    transferType: 'FILE_TO_STREAM_DOWNLOAD',
    config: {},
    transferSpec: JSON.stringify(transferSpec)
}

Send a start transfer request to the Transferd daemon and read data from the stream


    let transferId;
    let startTransferResponse = client.startTransfer(
        startTransferRequest,
        function (err, result) {
        if (err) {
            console.log("error starting transfer " + err)
        } else {
            transferId = result.transferId
            console.log("transfer started " + transferId)
        // read data stream from transfer
        let readStream = client.readStream({transferId: transferId})
        readStream.on('data', function (data) {
            console.log(data)
        })
    }
})

Legend
Technologies
Products & Services
Company information
Company logoIBM Aspera