Here we'll show you how to use our Ruby SDK to upload a file to Dropbox.
3You'll also need a valid Dropbox account and a Dropbox app, which you can create in the Dropbox App Console. The app name and domain can be whatever you like. Make sure to use the settings shown in the screenshot below when creating your new Dropbox app:
The Dropbox app console with the app settings you need for this example
4With your Dropbox application in place, visit the Dropbox OAuth bundle. Starting with the InitializeOAuth Choreo, use these Choreos to grant your Dropbox application access to interact with Dropbox on your behalf. This short video demonstrates how to use our OAuth Choreos, and it's a good idea to take a break and watch it now before you dive into this step. You'll need the access tokens returned by this process to complete the rest of this tutorial.
5 Go to the Dropbox > FilesAndMetadata > UploadFile Choreo in our Library. Enter your Dropbox access tokens and click on the Save Profile button to save your Dropbox app details. Make sure to fill in "sandbox" for Root input. Now you can reuse these Dropbox authentication details on any Dropbox Choreo in our Library. Using a Profile will also make your code simpler and more secure.
6 Let's test the Dropbox > FilesAndMetadata > UploadFile Choreo directly from our website. First, select Ruby from the drop down menu at the top of the page. Then insert your Dropbox account info that you saved earlier. Next, enter a FileName (like "test.txt") and finally, put something into the FileContents input (like "Hello World!").
You could also use the FileContentsFromURL input to pass a hosted file or image to Dropbox.
7Click Generate Code to test the Choreo from our website, and you'll see some details about your uploaded file in the Output section. If you go to your app folder in Dropbox, you'll see your file appear!
8 If you open your uploaded file, you'll see some garbled text. That's because the Dropbox UploadFile Choreo needs a Base64 encoded string for its FileContents input. Luckily enough we have one of these Choreos in our Temboo Library. Go to the Utilities > Encoding > Base64Encode Choreo to get started.
9 Type "Hello world" into the Text input and click Generate Code to test the Choreo. You should see a Base64 translation appear in the output. If you pass this text to the Dropbox Choreo it will create the file in Dropbox with human-readable text as expected.
10 To finish this example up, let's combine the two Choreos in one Ruby class. In the code block below, we've called both the Dropbox Choreo and the Base64 Encode Choreo. We've added some constants for the file name and contents, and a time stamp on the file name to allow multiple uploads (otherwise the upload will overwrite an earlier file of the same name).
To use the code below, simply substitute in your Dropbox Profile name and run the code. Your new upload should appear in your Dropbox folder with the value of the
file_contents variable as its text.
require "temboo" require "Library/Dropbox" require "Library/Utilities" class UploadToDropbox def initialize() # Set up your connection to Temboo. @session = TembooSession.new("ACCOUNT_NAME", "APP_NAME", "APP_KEY") # Set the name and content of the file you will upload. @file_name = "temboo" @file_contents = "You just made this file with Ruby and Temboo" end def perform_upload() # Set up choreo for encoding your file contents. encode_choreo = Utilities::Encoding::Base64Encode.new(@session) encode_inputs = encode_choreo.new_input_set() encode_inputs.set_Text(@file_contents) encode_results = encode_choreo.execute(encode_inputs) # Create a time string to identify the file from THIS test. time_string = Time.now.strftime("%Y-%m-%dT%H:%M:%S") # Set up choreo for uploading file. upload_choreo = Dropbox::FilesAndMetadata::UploadFile.new(@session) upload_inputs = upload_choreo.new_input_set() # Use the Profile you set up earlier. upload_inputs.set_credential("DROPBOX_PROFILE_NAME") upload_inputs.set_FileName(@file_name << "_" << time_string << ".txt") upload_inputs.set_Root("sandbox") # Use the encoded text to set the contents of your file. upload_inputs.set_FileContents(encode_results.get_Base64EncodedText()) upload_results = upload_choreo.execute(upload_inputs) # Check to see if there was a problem puts upload_results.get_Response() end end instance = UploadToDropbox.new() instance.perform_upload()
We're all finished! This Ruby application can create a text file out of any string you might produce while coding. Try using it to save all sorts of different content, like the results returned from the 2000+ Choreos in our Library.
Once you've got your code up and running, you're ready to move on and do more. From monitoring your running applications, to moving your generated Temboo code to your preferred development environment and sharing it with colleagues, collaborators and friends - we've got you covered.
We're always happy to help. Just email us at email@example.com, and we'll answer your questions.