Progress Indication for Dropbox, Google Drive, OneDrive for Up- and Download
It’s very easy to integrate cloud storage providers like Dropbox, Google Drive or OneDrive into your application with CLoudRail’s Unified Cloud Storage API. In many cases it makes sense to show a progress indication, for example in form of a progress bar, during a file upload or download, especially for bigger files or on a poor connection. In this tutorial we will show you how to implement this feature with our SDKs for Node.js, Android, iOS (Swift & Objective-C) and Java.
How to Realize Progress Indication for all Cloud Storage Providers with CloudRail
In this blog post we want to outline a way you can get such progress information from CloudRail SDKs. Even though there is no “built in” functionality for achieving this, there is a way of utilizing the Streams provided to and returned by our SDKs. We will have a sample for each platform in a second but first lets discuss the generic idea.
When you perform a download on a file, the SDK will return a Stream to the content of the file. But it is important to understand that at the time the Stream is returned the file is not yet downloaded. A Stream can be understood as a pointer to the data being requested. In addition it also keeps track of how much data was consumed and if there is more available. In order to get a progress indication there are two options. If you are manually reading the content of the Stream and keep it in memory, you could just sum up the amount of bytes you read from the Stream. In most cases though you want to pass the Stream to another method which then, for instance, writes the content to the disk. In this case you generally want to wrap the Stream into a custom implementation which extends the basic Stream. What this class does is just to proxy the methods to the original Stream and emit a certain event when data was consumed.
The upload follows a similar logic. You have to pass a Stream to the SDK. This Stream is passed to the HTTP client which takes care of sending the data or in other words reading from the Stream. Just like for the download, the content is not read at once. So in order to get the progress information we need to capture these read events. These are usually the same events no matter if you perform an upload or a download so in most of the following samples it is enough to create/use one custom class for both use cases.
Sample Code for Android, Java, Node.js and iOS
On the other hand, for upload you need to use a class that we also use internally. Here an example on how to use it:
This should give you a good understanding of how you can implement the progress indication into your application. If you have trouble getting it to work just send me an email.