Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Customize Filenames #25

chadwtaylor opened this Issue Dec 12, 2012 · 10 comments


None yet
4 participants

Need the ability to change the filename before it gets uploaded to S3.

For instance, the original filename is "picture.jpg" and need to rename it to "2012_01_31_picture.jpg".


pomartel commented Dec 12, 2012

This is probably not possible due to most browsers limitations that restrain you from renaming a user uploaded file for security reasons.

Before I used this gem, I was able to rename the file...

And I am using this gem because it resolves the IE bug I was experiencing.

Pierre Olivier Martel wrote:

This is probably not possible due to most browsers limitations that
restrain you from renaming a user uploaded file for security reasons.

Reply to this email directly or view it on GitHub
#25 (comment).


waynehoover commented Dec 12, 2012

I don't think that any current browsers allow you to change the name of the file before its uploaded. You could change the path its stored in S3 though.

Here's the code snippet that I used to change the filename and had success -- that was before I came across this useful gem.

Notice the filename line -- I was able to modify it and confirm that the filename changed when it got published in the bucket.

def index # SignedUrlsController

    dt = DateTime.now.strftime("%Y_%m_%d_%I_%M_%p") 
    filename = "#{params[:case_number]}_#{dt}_#{params[:doc][:title]}"

    data = Hashie::Mash.new
    data.policy = s3_upload_policy_document
    data.signature = s3_upload_signature
    data.key = "uploads/#{SecureRandom.uuid}/#{filename}"
    data.success_action_redirect = "/"
    puts "json: #{data.to_json}"

    render json: data.to_json

pomartel commented Dec 13, 2012

Can you show the Javascript part which does the renaming of the file? Because that's where the limitation is.

Here's my s3_upload.js snippet that shows where I was able to override the filename:

      url: form.attr('action'),
      type: 'POST',
      autoUpload: true,
      dataType: 'xml', // This is really important as s3 gives us back the url of the file in a XML document
      add: function (event, data) {
          url: "/signed_urls",
          type: 'GET',
          dataType: 'json',
          data: {doc: {title: data.files[0].name}, case_number:case_number }, // send the file name to the server so it can generate the key param
          async: false,
          success: function(data) {
            // Now that we have our data, we update the form so it contains all
            // the needed data to sign the request

Here is how it works step-by-step:

  1. Select a file to upload (ie: picture.png)
  2. The add: function above calls the /signed_urls (see my SignedUrlsController#index comment above that changes the filename, ie: 2012_01_31_picture.png)
  3. We update the input[name=key] to reflect the new filename change.
  4. Finally, data.submit() is called which uploads the file to S3 along with the new filename change.

Hope that helps?


Screenshot 2012-12-13 08h 33m 07s

The screenshot above is from my S3 Bucket where you can see that Desert.jpg and aiming_6.png files were renamed to 31808_2012_12_12_08_41_AM_Desert.jpg and 31754_2012_10_25_03_40_PM_aimimg_6.png successfully.


waynehoover commented Dec 13, 2012

@chadwtaylor you can't modify the filename exactly, but you could append to it by modifying this line: https://github.com/waynehoover/s3_direct_upload/blob/master/lib/s3_direct_upload/form_helper.rb#L52

How would you like to modify the filename?

I was thinking of adding some kind of configurable options such as prefix_filename = "2012_02_22_" somewhere?

I came across this gem in hopes of replacing the flash version of my uploader that uploads my files directly to s3. However, it is a requirement that i am able to update the filename so when its saved on the s3 side the filename has been updated with the prefix i am adding to it, like what chadwtaylor needs. It would be amazing if this kind of feature could be added to the gem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment