how to get direct link of folder with Dropbox Ruby SDK? - ruby

I'm using Dropbox Ruby SDK, now I want get a full length share url of folder instead of the short url method shares(path) returns. But I couldn't find a way to pass query parameter 'short_url=false'( found on core API) with the method, since it only accepts one argument which is the path to share.
The reason I want the full length url is that I need to provide user a direct link to download the files( by adding 'dl=1'to the full url).
Any suggestion is appreciated.

It doesn't look like the Ruby library currently exposes this. But the source code is here: https://github.com/dropbox/dropbox-sdk-ruby/blob/master/lib/dropbox_sdk.rb#L1222-L1225. If you're up for it, please submit a pull request to add this to the SDK.
In the meantime, here's standalone code to do this:
def long_share_url(access_token, path)
client = DropboxClient.new(access_token)
session = DropboxOAuth2Session.new(access_token, nil)
response = session.do_get "/shares/auto/#{client.format_path(path)}", {"short_url"=>false}
Dropbox::parse_response(response)
end

Related

Google Drive API v3 : there isn't any way to get a download url for a google document?

The Google Drive API v2 to v3 migration guide says:
The exportLinks field has been removed from files. To export Google Documents, use the files.export method instead.
I don't want to export (download) the file right away. "files.export" will actually download the file. I want a link to download the file, later. This was possible in v2 by means of the exportLinks.
How can I in v3 accomplish the same? If it is not possible, why was this useful feature removed?
Besides, (similar problem to above) downloadUrl was also removed, and the suggested alternative ("files.get with ?alt=media") downloads the file instead of providing a download link. This means there is no way in v3 to get a public short lived URL for a file?
EDIT:
there is no way in v3 to get a public short lived URL for a file?
For regular files, apparently yes.
This seems to work fine (a public short lived link to the file with its right name and contents):
https://www.googleapis.com/drive/v3/files/ID?alt=media&access_token=TOKEN
For google apps files, no (not even private, as v2 exportLinks used to be).
https://www.googleapis.com/drive/v3/files/ID/exportmimeType=TYPEv&access_token=TOKEN
Similar to regular files, this URL is a short lived link to the file contents, but lacking of its right name.
BTW, I see the API is not behaving consistently: /drive/v3/files/FILEID delivers the right file name, but /drive/v3/files/FILEID/export does not.
I think the API itself should be setting the right Content-Disposition, as it is apparently doing when issuing a /drive/v3/files/FILEID call.
This file naming problem invalidates the workaround to the lack of ExportLinks in v3.
The v2 ExportLinks allowed me to link a file (which is not the same as getting its content right away). Anyone logged in and with the proper permissions was able to access it, and the link didn't needed any access_token, and it wasn't short lived. It was good and useful.
Building a link with a raw API call like /drive/v3/files/FILEID/export (with mandatory access_token) would be an close enough workaround (it is temporary and public, not the same as it was, anyway). However, the naming problem invalidates it.
In v2, regular files have a WebContentLink and google apps files have exportLinks. In v3 exportLinks are gone, and I don't see any suitable alternative to them.
Once you query for your file by id you can use the function getWebContentLink() to get the download link of the file (eg. $file->getWebContentLink() ).
I think you're placing too much emphasis on the word "method".
There is still a link to export a file, it's https://www.googleapis.com/drive/v3/files/fileIdxxxxx/export&mimeType=xxxxx/xxxxx. Make sure you URL encode the mime type.
Eg
https://www.googleapis.com/drive/v3/files/1fGBQ81haNU_nEiC5GITZD3bxT0ppL2LHg-C0ubD4Q_s/export?mimeType=text/csv&access_token=ya29.Gmo0BMvO-pVEPKsiD9j4D-NZVGE91MChRvwOcBSg3cTHt5uAClf-jFxcovQScbO2QQhwHS95eSGW1eQQcK5G1UQ6oI4BFEJJkntEBkgriZ14GbHuvpDL7LT2pKA--WiPuNoDDIuZMm5lWtlr
These links form part of the API, so the expectation is that you've written a client that sends authenticated requests, and deals with the response data. This explains why, if you simply paste the link into a browser without an access_token, it will fail. It also explains why the filename is export, ie. it isn't intended that your client would ever use a filename, but rather it should receive the data as a stream. This SO answer discusses the situation in more detail How to set name of file downloaded from browser?

REST API URL with Directory and File name as Path Variables

I have a requirement to send the folder details in the URL for POST method, some thing like this
http:///{directory}/{filename}
I am using Spring API to create the service. Using #PathVariables to two variables in the URI.
Problem : Directory can have "/" slashes in it.
Now how can I create my API, please help me.
POST http://example.com/api/files/path/to/my/file/filename
Stick a controller on /api/files. Scrape the URL starting after /files. Use that to locate the file.
P.S. This has the potential to be a Very Bad Idea. Make sure you secure the controller to only expose those parts of your filesystem you don't mind random internet strangers to be able to operate on.

Image hosting with Dropbox in Ruby

Hi I'm trying to use Dropbox as an imaging hosting server. The framework is currently Ruby.
I tried to use /media or /shares to get the data's url. When I use "/media" command, it seems the image can be hosted for a couple of hours but at the end it expires(4? hours roughly). If possible, I want to know how to set the expiration dates for this ( at something like 1 year? ), not manually, but in a programmable manner.
[How I extract "url" in Ruby]
image_1_link = Drop_client.media('path_name')["url"]
pdf_link = Drop_client.shares(''path_name'["url"]
The below is a sample link after using "/media".
"https://dl.dropboxusercontent.com/1/view/e9lr642qerdmgm9/Apps/ringle_records/images/Test_1image_1?dl=1"
When I use "/share", the generated link looks like "https://db.tt/Zs0v4yaffal?dl=1". But it can't host the image if I use it as . If I put this type of address, it leads to dropbox page and ask manual downloading.
I want to know how to generate link for imaging hosting so that can work for 1 year or at least for a couple of months!
Thanks for reading my question!
/media links expire after 4 hours, as documented.
/shares takes a parameter, short_url, that can be set to false to return an unshortened URL. Unfortunately, I believe the v1 Ruby SDK doesn't support this parameter. You can either modify the Ruby SDK yourself or get the shortened URL and unshorten it yourself. (Make an HTTP GET request to the short URL and grab the Location header of the redirect response.)
Once you have an unshortened URL, you can modify it per www.dropbox.com/help/201 to get a link directly to the content. Specifically, you should use the raw=1 query parameter.

(Multipart) zip upload in ruby webmachine handled by rack

I'm making an upload form for zips in a ruby webmachine app. My idea is to have an upload through my backend where I can add some extra params and then upload it to amazons s3 service with RestClient.
I did successfully create a direct upload (web based form post) to a s3bucket, but in that way I'm unable to handle the variables which are needed in the request, the way I want.
I've tried several things but I can't figure out, how to handle the request, as soon as it gets in my backend. I've created a resource and I'm debugging directly in the process_post method.
My #request variable represents a Webmachine::Request, with a Webmachine::Adapters::Rack::RequestBody and a Rack::Request, but I can't get the file out of it to use it as input for my RestClient request.
I think; #request.body.to_s and #request.body.to_io, represent the uploaded file in some way, and I tried to use them as input for Rack::Multipart methods, but that doesn't give me the file.
I also tried to work with the rack-raw-upload gem, but I can't get the mime-type something else than "application/x-www-form-urlencoded" or multipart. I do explicitly set it to; application/octet-stream
Things like File.new(filename, 'rb') gave me `rrno::ENOENT: No such file or directory # rb_sysopen'. For filename I just used 'example.zip'.
I guess I'm missing something which has to do with the Rack::Request call(env) method.
Does somebody have an idea, on how to handle the Rack uploads? Or give me any hints for a new direction? Thanks.
I've created a gist which shows how to retrieve the multipart stream. You'll need further parsing in order to get the uploaded file.
https://gist.github.com/jewilmeer/eb40abd665b70f53e6eb60801de24342

Reading a Google Spreadsheet into Ruby Objects (or settle for a file download)

I think it's probably simplest to start with my use case:
I'm trying to read the contents of a Google Spreadsheet into ruby, and then use that data for other purposes. This needs to happen server-to-server.
Here's what I've tried:
First I tried to use this google drive gem. Since my interaction needs to be server to server (i.e. a service account), I couldn't get this to work (someone please let me know if they've managed this!)
Next, I tried using the latest google-api-ruby gem, and, following its documentation, was successfully able to authenticate my service account, and have been able to get lists of files, etc. So basically the API is authenticated and working.
The latest issue is that Google Spreadsheets can't be downloaded using the normal download_dest parameter on the get_file method (sorry - would post links to relevant documentation here, but don't have enough rep to do so). So I'm not really sure how to proceed with downloading the relevant file (I understand how to get an export_link), but have no clue how to then prepare the next request).
Any help would be greatly appreciated :)
Here's some basic code for reference -
require 'googleauth'
require 'google/apis/drive_v2'
scopes = ['https://www.googleapis.com/auth/drive']
ENV["GOOGLE_APPLICATION_CREDENTIALS"] = 'path/to/my/creds.json'
auth = Google::Auth.get_application_default(scopes)
drive = Google::Apis::DriveV2::DriveService.new
drive.authorization = auth
ss = drive.get_file('my_spreadsheet_id')
export_url = ss.export_links
# what now?

Resources