sharepoint-ruby
sharepoint-ruby copied to clipboard
Download large files via stream
I can see that there is a way you can upload files via a stream but is there a way to download a file via stream to try and speed up the process.
Combining the information from https://learn.microsoft.com/en-us/previous-versions/office/developer/sharepoint-rest-reference/dn450841(v=office.15)#file-resource and https://github.com/taf2/curb/blob/13144ec5d50ffea0460298cc5de8a0b33db78d22/lib/curl/easy.rb#L446
I would say the solution is something like
File.open('/tmp/some-file', 'wb') do |file|
body = nil
skip_json = true
site.query :get, 'https://my.site.com/some/site/some/path/to/file.pdf', body, skip_json do |curl|
curl.headers["Accept"] = 'application/octet-stream'
curl.headers["Content-Type"] = 'application/json;odata=verbose'
curl.on_body do |data|
file.write data # or whatever else you want to do with the data.
data.length # curl.on_body block must return the data length
end
end
end
Arguably
folder = site.folder '/SiteAssets/documents' # Get a folder by server relative path
file = folder.file_from_name('some-file-name.pdf')
file.download
# or
file.download_to_file(some_file)
is actually doing the same thing.
See
- https://github.com/Plaristote/sharepoint-ruby/blob/dc7cb5a30f3d5fd9f28044de1c5b0054accd560d/lib/sharepoint-files.rb#L50
- https://github.com/Plaristote/sharepoint-ruby/blob/dc7cb5a30f3d5fd9f28044de1c5b0054accd560d/lib/sharepoint-files.rb#L65