We are using carrierwave + aws S3 to upload file, and we need provide a download function.
For solution 1, we use:
= link_to "Download", file.doc.url, download: file.original_name
And it does not work under IE8, click the link will open this file(image).
According to This, I should add Content-Disposition header,
Then I check aws S3 document, Found I need add response-content-disposition to file.doc.url,
Is there any way I can do this in carrierwave, or I could use other ways? Thanks for your help.
Got it, for fu = FileUploader.new, we can use:
fu.url(query: {"response-content-disposition" => "attachment;"})
Read tons of documents and source code.
Related
Using url_for() on a file stored in active storage returns a url that leads to the application and then redirects to the actual location. Because of a bug in firefox with CORS, the redirect breaks my application.
Is there any way to get the direct link to the file with ActiveStorage?
You can do this
record.active_storage_object.blob.service_url
Found here https://github.com/rails/rails/blob/master/activestorage/app/controllers/active_storage/blobs_controller.rb
I had to dig through the rails source to create this so I have no idea how recommended it is but this works for disk storage at least.
ActiveStorage::Current.host = "yourhostname"
attachment_blob = ActiveStorage::Attachment.find_by(record_type: "YourModel", record_id: record.id).blob
direct_url = ActiveStorage::Blob.service.url(
attachment_blob.key,
expires_in: 20000,
disposition: "attachment",
filename: attachment_blob.filename,
content_type: attachment_blob.content_type
)
For me, rails_blob_url(#blog.pdf) (if you're trying to get the file stored as #blog.pdf) worked best.
In Rails, what is the best (ie. efficient, elegant) way to download a file from a public HTTP url, upload to Amazon S3 and delete the file in the server.
I am using Heroku so I have the additional restriction of a ephemeral file system.
If reading into memory isn't an option.
You can use the /tmp directory. As long as this is in the same process using that shouldn't be a problem.
The answer is to read your image into memory instead of storing it on the filesystem. Here's an example.
s3 = Aws::S3::Resource.new
obj = s3.bucket(your_bucket_name).object(your_object_key)
s3_put_url = URI.parse(obj.presigned_url(:put))
image_url = 'http://www.google.com/google.jpg'
image_file = open(image_url).read
Net::HTTP.start(s3_put_url.host) { |http| http.send_request('PUT', s3_put_url.request_uri, image_file); }
# Let's get the URL
s3.bucket(your_bucket_name).object(your_object_key).presigned_url(:get)
We can get the share link for a dropbox folder from SDK(https://github.com/dropbox/dropbox-sdk-ruby/blob/master/lib/dropbox_sdk.rb#L1222-L1225), which link is dl=0 by default, I need to set it with dl=1 so that it will download directly, I did some hack:
require 'dropbox_sdk';
client = DropboxClient.new(ENV['dropbox_access_token'])
session = DropboxOAuth2Session.new(Option['dropbox_access_token'], nil)
response = session.do_get "/shares/auto#{client.format_path('/auction_offerings/two')}", {"short_url" => false}
response = Dropbox::parse_response(response)
response["url"][-1]="1"
Then response["url"] will become the download link I want, but it is obvious not the right way to do that. Any better practice?
I followed the following blogpost to figure out how to create Facebook events remotely using my app. I've been having problems loading the images from my app, however, because I do not have images stored locally on my app, they are stored in AWS.
#graph = Koala::Facebook::GraphAPI.new(#token)
picture = Koala::UploadableIO.new(#event.photo.url(:small))
params = {
:picture => picture,
:name => 'Event name',
:description => 'Event descriptio
:start_time => datetime,
}
is the following code I am currently using to send pictures to Facebook when Facebook events are created on my app. The problem is, however, that Rails is throwing the error: No such file or directory - http://s3.amazonaws.com/ColumbiaEventsApp/photos/21/small.jpeg?1312521889.
Does anybody who's more experienced with Rails development know if there is a way for me to treat a URL like a path to a file? The UploadableIO class expects a path to a file, and I'm struggling to figure out if there's a way in Ruby to treat URL's like filepaths. The way that photos stored on the app can be loaded to Facebook is as follows:
picture = Koala::UploadableIO.new(File.open("PATH TO YOUR EVENT IMAGE"))
if that helps.
I appreciate any new insights into this issue.
Ok so I played around and figured out how to post pictures.
Basically what I did was use the 'open-uri' library to convert the image links into file objects, which can then be passed to UploadableIO and sent to Facebook. This is the code that worked:
require 'open-uri'
OpenURI::Buffer.send :remove_const, 'StringMax' if OpenURI::Buffer.const_defined?('StringMax')
OpenURI::Buffer.const_set 'StringMax', 0
picture = Koala::UploadableIO.new(open(#event.photo.url(:small)).path, 'image')
params = {
picture: picture,
name: #event.name,
description: #event.description,
location: #event.location,
start_time: datetime
}
#graph.put_object('me', 'events', params )
The OpenURI constant StringMax needed to be changed because the image files I was using were small enough that the files were being processed as Strings rather than File Objects.
Hope this helps anyone trying to fix this!
With Koala 1.2.1 it's a very elegant solution. Here is sample code for creating an album and uploading to it from a remote, AWS link (btw this took about 30 lines in PHP w/ the PHP SDK!
#foo = Foo.find(params[:foo_id])
albuminfo = #graph.put_object('me','albums', :name=>#foo.title)
album_id = albuminfo["id"]
#graph.put_picture(#foo.remote_image_path,{}, album_id)
Facebook recently released an update that lets you post pictures using publicly accessible URLs (http://developers.facebook.com/blog/post/526/). The Koala library you're using supports that (https://github.com/arsduo/koala/blob/master/lib/koala/graph_api.rb#L102), so you should be able to post the pictures you're hosting on S3 without having to use OpenURI::Buffer.
For Facebook Ad Images, you unfortunately currently cannot do it by URL, thus:
require 'open-uri'
img_data = open(my_post.image.url :medium).read
img = graph.put_connections('act_X', 'adimages', bytes: Base64.encode64(img_data))
I'm uploading the image using aws-s3 gem, it uploads perfectly and give me the url. Image is in PNG format but when I hit the public url in my browser, it starts downloading, but I don't want to download it, I want to see the image in browser.
Please tell me if you know which parameter in the following request should be added for this or what change is required to achieve this?
AWS::S3::S3Object.store(base_name,open(local_file),bucket,:content_type => mime_type,:access => :public_read,:authenticated => false)
s3_url = AWS::S3::S3Object.url_for(params[:name],bucket)[/[^?]+/]
Check :content_type => mime_type if mime_type is correct.