1

I'm using Carrierwave Direct with S3 for file uploads.

When a file is uploaded to S3, we take the S3 URL and run it through FFMPEG to measure duration.

duration = `ffmpeg -i "#{post.s3_url}" 2>&1 | grep Duration | awk '{print $2}' | tr -d ,`

The problem is, some of our users are placing special characters in filenames.

I've seen this post showing how to rename files using Fog. However, I'm wondering if there's a simpler way... I'd love to just hardcode the filename in Carrierwave Direct before it's uploaded.

I'm curious what the best approach might be to accomplish this.

Any help is appreciated!

Community
  • 1
  • 1
cmw
  • 946
  • 2
  • 11
  • 26
  • Did you figure out a solution for this? – sfkaos Nov 24 '14 at 06:44
  • I found that you couldn't rename before uploading, but, using Fog, you can rename immediately after uploading. I detailed the solution below. – cmw Nov 24 '14 at 15:56

1 Answers1

1

It appears that you're unable to modify a filename before it's uploaded.

The solution, ultimately, was to allow the file to upload (with its original filename) to S3, and then, use Fog to make a copy, rename it, and then delete the original.

Then, we'd run FFMPEG against the new version of the file, with the updated name.

Code is as follows...

Open a connection to S3, via Fog...

connection = Fog::Storage.new({
  :provider => 'AWS',
  :aws_access_key_id => '[access key]', 
  :aws_secret_access_key => '[secret key]', 
})

Select the bucket in which the file lives...

bucket = connection.directories.get('bucket_name')

Select the original file you'd like to rename/copy...

original_file = bucket.files.get 'path/to/filename.jpg'

Copy the original file, to create a new one – naming it as you wish, and make it public...

new_file = bucket.files.create :key => 'path/to/newfilename.jpg', :body => original_file.body, :public => true

At this point, we've duplicated the original file – and have renamed it to avoid any conflicts with FFMPEG. We can now destroy the original.

original_file.destroy
cmw
  • 946
  • 2
  • 11
  • 26