Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Special characters in filename affecting aws-sdk ruby gem url_for method

I am using the official AWS ruby gem for S3 and I am having trouble using the "url_for" method on files that have special characters in them (ie. commas, apostrophes). I am using the latest aws-sdk gem for ruby and my code looks like this:

s3 = AWS::S3::new
bucket = s3.buckets[bucket]
object = bucket.objects[object_address]
object_url = object.url_for(:read, :expires => 60*60, :secure => true)

The object is getting found correctly, but the URL I get from url_for gives me a HTTPError: 404 Not Found error. It works fine if the filename doesn't have commas or apostrophes in it.

Is there a way to handle this without needing to restrict the filenames in the first place?

like image 605
Joel Friedlaender Avatar asked Oct 10 '22 07:10

Joel Friedlaender


1 Answers

Are you escaping the URL string by default? For example:

object_url =  CGI.escape(object.url_for(:read, :expires => 60*60, :secure => true))

This would properly escape the string into a browser-readable format. I do this for all of my secure S3 URL's since there are sometimes a / or + character in the signature that will cause the link to fail as well if it's not properly escaped. This will also escape the commas and apostrophes properly.

like image 189
iwasrobbed Avatar answered Oct 12 '22 23:10

iwasrobbed