Unboxing Dropbox and SharpBox

On a train trip during the New Year’s weekend, I spent time testing and collecting notes about how to interact with Dropbox. I will share my findings with you in this blog post.

 Dropbox

Dropbox is a free service that lets you bring your photos, docs, and videos anywhere and share them easily. Never email yourself a file again!  – dropbox.com

Follow these steps to create a Dropbox account and register the app you will use to interact with your Dropbox file area:

    1. Create a new Dropbox account at www.dropbox.com/login#register
    2. Accept the Dropbox App Developer terms at www.dropbox.com/developers/apply
    3. Continue with the next step, where you create an app with a suitable test name and a short nonsense description


The Dropbox admin interface should now show you the App Key and App Secret you need later for creating a security token file.

SharpBox

My train trip was quite long, so I decided to put another card in the deck. SharpBox is a popular framework for accessing storage and one of the storage service providers it supports is Dropbox. (You can, of course, skip SharpBox and use the Dropbox API directly but I decided out of curiosity to test SharpBox.)

“SharpBox is the right free open source solution for your project. Our goal is to disburden software projects from the demand to implement storage access twice to support a wide range of storage clouds.” – harpbox.codeplex.com

Follow these steps to set up a test application in Visual Studio 2010 and include Sharpbox in it:

    1. Create a new C# Console Application in Visual Studio
    2. Open the properties of the application and set Target Framework to .NET Framework 4
    3. Add the SharpBox NuGet package named  AppLimit.CloudComputing.SharpBox
    4. Open the File Explorer and start the SharpBox tool DropBoxTokenIssuer.exe located in the folder approotpackagesAppLimit.CloudComputing.SharpBox.1.2.0.542libnet40-full. This tool is used to create a Security Token file you will use in your application.
    5. Enter the Application Key and Application Secret you previously got in the Dropbox admin interface. Name the file DropBoxToken and finally click the Authorize button.

 

 

This will hopefully lead you to the Dropbox login page, where you will use the login credentials for the account you previously created. (Please make sure you set the Target Framework correctly to .Net Framework 4 if no login page appears.)

If everything worked out correctly, you will now see an error page. Confusing? This is because the Sharpbox tool redirects to a page that does not exist on Codeplex. You will find the Security Token file created in the same folder where the tool application exists.

Finally, move this Security Token file to C:Test together with an empty text file you create and name texttest.txt.

The code

Everything is now set up for some coding. Fill the Program.cs with this:

using System;
using System.IO;
using AppLimit.CloudComputing.SharpBox;

namespace DropBoxTestApp
{
  class Program
  {
    static void Main()
    {
      var dropBoxStorage = new CloudStorage();
      var dropBoxConfig = CloudStorage.GetCloudConfigurationEasy(nSupportedCloudConfigurations.DropBox);

      // declare an access token
      ICloudStorageAccessToken accessToken;
      // load a valid security token from file
      using (var fs = File.Open(@"C:TestDropBoxToken", FileMode.Open, FileAccess.Read, FileShare.None))
      {
        accessToken = dropBoxStorage.DeserializeSecurityToken(fs);
      }

      // open the connection
      var storageToken = dropBoxStorage.Open(dropBoxConfig, accessToken);

      var srcFile = Environment.ExpandEnvironmentVariables(@"C:Testtexttest.txt");
      dropBoxStorage.UploadFile(srcFile, "/");

      // close the connection
      dropBoxStorage.Close();
    }
  }
}

After executing the application, you should be able to see in your Dropbox client of choice or Dropbox admin interface that the file texttest.txt has been uploaded to your Dropbox file area.

Useful Hints and Links

I have now successfully stored files this way for more than a month in a minor hobby application and it works great. After uploading files, you probably also want to download them and SharpBox can help you with this and much more. The only problem I encountered in my own application was when trying to download a file from Dropbox and sending it to the user via a Web browser as a stream. The file had the correct size, but seemed to be empty. The solution was to use Stream.Seek(0,0) before returning the filestream.

You can find more info here:

 

Trackback URL: https://codeblog.silfversparre.com/2012/02/unboxing-dropbox-and-sharpbox/trackback/