Channels ▼
RSS

Open Source

Working with Dropbox


The advent of high-speed networks and affordable high-capacity storage breathed new life into that once fickle online service — file hosting. Unlike the old BBS and FTP services, today's file hosting employs Web 2.0 standards to display an intuitive interface. It can handle a wide range of file formats, and it has become consistently reliable.

In this article, I examine one of the most popular such services: Dropbox. I'll look at what this service offers and how it interacts with its users. And after going over its SDK, I'll explain how to use it to build a custom service client. In the process, I'll demonstrate how to log into a user's account, how to move files to and from that account, how to do a query, and how to manage the account.

For reasons of length, all sample code is written with Cocoa and Objective-C in mind.

Overview of the Service

Dropbox is the brainchild of MIT graduates Drew Houston and Arash Ferdowsi. Established in 2007, the service gives users a place to store their files online. It lets users access those same files from any locale or platform. Dropbox is a cross-platform service, requiring only an AJAX-enabled Web browser to use.

A Dropbox account comes in two flavors. A free account entitles users up to 2 GB of space. Users can earn more storage space, up to a maximum of 8 GB, by inviting other users to join Dropbox.

A paid account entitles users to more storage space, from 50 GB to 1000 GB. Paid users get preferential treatment in terms of support and features. At the time of writing, both free and paid accounts offer the same feature set.

The typical Dropbox account uses the familiar file/folder metaphor to organize its user data (Figure 1). Above the file/folder list is a button toolbar, showing the possible actions users can make on their accounts. Above that toolbar is a sequence of tabs, each tab linked to a separate panel view.


Figure 1.

By default, the account gets two folders: Photos and Public. A click on the toolbar button New Folder adds an empty folder to the account. Setting a folder's check-box, then clicking the button Share a folder prompts for a user's e-mail address, inviting that user to share the folder. A click on a folder displays that folder's contents.

Accessing a Dropbox account with a Web browser has specific limitations. Some actions, for instance, take more steps on the browser than on the desktop. Consider the act of renaming a file. On Dropbox, a rename action takes about four steps. On Mac OS X, it takes at least two.

Then there is the lack of keyboard control. On the Windows desktop, a Ctrl-C copies a file; a Ctrl-V pastes that copy into the desired folder. This is not possible with the Web browser, which prefers a pointing device.

Finally, drag and drop support is minimal at best. Users can move a file to a folder by dragging. But if the desired folder is inside another folder, the same drag action does not cause a drill-down. Moreover, dragging a file or folder to the desktop does not start a download action.

Clients to the Service

To address the limits of the Web browser, Dropbox provides software clients for most major platforms. These clients enable users log into their accounts and access their files. But they do not provide access to the full range of Dropbox services. To get the right client for a given platform, go to http://www.dropbox.com/install.

Each Dropbox client brings its own user experience. Consider the OS X client (Figure 2). It runs as a background process. It has its own menu on which it lists the commands for controlling the process.


Figure 2.

The OS X client adds its own folder to the user's home directory. In that folder, it recreates the file/folder structure from user's account. The client keeps both folder and account synchronized. So, files added to the account will appear on the folder. Files removed from the same account will be removed from the folder.

Not all Dropbox clients are as transparent or as integrated as the OS X client. The iOS client, for instance, is a separate application. To use it, users may have to end their current app process. The clients are not scriptable. They cannot be controlled using an applet or a shell script. This makes the clients difficult to integrate with databases or SCM.

These Dropbox clients use SSL to connect securely with the user account. But they do not encrypt files on the fly. If a file is unencrypted to start, it stays unencrypted after being stored on the account. The same clients do not perform diffs, and they do not authenticate files copied to or from the account.

In short, these official Dropbox clients provide only a basic backup/restore feature. If users want other features, they will need a custom client solution.

SDK for the Service

To help developers write a custom client, Dropbox provides an SDK. This SDK defines a standard interface for linking with a user's account and for managing its files. It uses HTTPS as its network protocol, and renders each protocol command in REST. The SDK is available for five major languages.

With the SDK, we can add a new folder to the account or rename an existing one. We can upload a file to a specific folder or download it from the folder to the client platform. We can even query for details about a specific file or folder on that account.

At the time of writing, the latest stable version of the SDK is 1.0. Each SDK package has a different minor version depending on the target language. For instance, the ObjC package has the lowest version of 1.0.0. The Android and Java packages have the highest one at 1.2.2. It is not known if the version difference reflects the amount of work spent on the SDK package or the priority of support for that package.

Preparing to Link

Before we start building our custom client, we must register the project with Dropbox. Steps to register the client are available at this URL: http://www.Dropbox.com/developers/apps.

Registration gives us two developer keys with which to enable a Dropbox session. Registration does require a valid Dropbox account. Each developer key should contain only alphanumeric characters. Any other characters will mean an invalid key. To check the keys, we could use the NSCharacterSet class as shown in Listing One. Here, we have the keys rendered as constant NSString objects. We create an instance of NSCharacterSet, one with only alphanumeric characters; then we invert that instance. Next, we use the instance method rangeOfCharacterFromSet: to check each key character against the inverted set. If the method returns a valid NSRange object, the key in question is invalid.

Listing One

NSString* tKey1 = @"thisiskey123456";
NSString* tKey2 = @"thisiskey987654";
NSCharacterSet *tSet;
BOOL tErr;
// define the character set
tSet = [[NSCharacterSet alphanumericCharacterSet] invertedSet];
// validate the keys
tErr = ([[tKey1 rangeOfCharacterFromSet:tSet].location != NSNotFound]);
tErr = tErr and ([[tKey2 rangeOfCharacterFromSet:tSet].location != NSNotFound]);
if (tErr){
    NSLog(@"The keys are invalid.");
}
else {
    NSLog(@"The keys are valid.");
}

Listing Two shows how we prepare a controller object to support Dropbox. This controller class, FooDrop, declares the private property pOut_ in its @interface block. It also declares the accessor dboxClient with which to link to that property. Both property and accessor are instances of DBRestClient.

Listing Two

@interface FooDrop : NSObject <DBLoginControllerDelegate,
    DBRestClientDelegate<
{
@private
	DBRestClient *pOut_;
}

// -- property:accessors
@property (nonatomic, retain) DBRestClient* dboxClient;

// -- see Listing Three

@end

/*
--
-- FooDrop.m
--
*/
@implementation FooDrop

// -- property:accessors
@synthesize dboxClient = pOut_;

- (void)awakeFromNib
{
	// set and validate the developer keys
	// -- see Listing One
	
	// create a session
	DBSession *tDBx = [[DBSession alloc] 
		initWithConsumerKey:tKey1 consumerSecret:tKey2];
	tDBx.delegate = self;
	
	[DBSession setSharedSession:tDBx];
	[tDBx release];
}
     @end

Next, in its @implementation block, FooDrop defines its awakeFromNib method. This method runs after FooDrop is loaded from its xib bundle. The first half of the method sets and validates the developer keys as described in Listing One. The second half creates an instance of DBSession using the factory method initWithConsumerKey:consumerSecret:. It sets FooDrop as the delegate and passes the DBSession instance to the class method setSharedSession:. This makes the DBSession instance available to other objects in the project, provided they import the Dropbox.h header.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video