Sungpah Lee Sungpah Lee - 7 months ago 31
Ruby Question

How to make users to download files in Dropbox with Ruby?

I'm now trying to make a function that allows users to access to the link of the file to download.

The native link is https://www.dropbox.com/home/Apps/ringle_records?preview=lesson_id_11%3Auser_id_5.mp3 but, it is only accessible by admin ( for sure ). If I have a user who is eligible to know this link, then I would love to allow this user to click some button to download the file.

I am kind of bogged down when it comes to implementation. Because if I use the below code, it seems it stores data into the server.

def self.dropbox_download
contents, metadata = Drop_client.get_file_and_metadata('/lesson_id_12:user_id_7.mp3.mp3')
begin
File.open('filename.mp3', 'wb') do |file|
file.write(contents)
end
rescue
end
end


I want to make user to click something and the "mp3" file will be downloaded, but I don't think the above code works in this way.

Please shoot any opinion! I'm looking forward to seeing!

Best

Answer

If you only need to allow other users to download this specific file (or some specific set of files), the easiest way is to just get a shared link to the relevant file(s):

https://www.dropbox.com/help/167

You can modify these shared links for different behaviors, e.g., for file downloads, as shown here:

https://www.dropbox.com/help/201

If you do need to get these links programmatically, you can use the shares method:

https://dropbox.github.io/dropbox-sdk-ruby/api-docs/v1.6.4/DropboxClient.html#method-i-shares

You can then give the shared link to the user, e.g., as a link on a web page, or in an email, etc.