How to Rename a File with Fog

How do I rename a file with Fog?

The bad news is you need to do a get/create/destroy

foo = bucket.files.get 'foo'
bar = bucket.files.create :key => 'bar', :body => foo.body
foo.destroy

The good news is if you're doing it from ec2 in the same region it will probably happen as fast as renaming a file on your local computer

Modifying Carrierwave Direct filenames before upload

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

Batch rename log files from one time format to another

find . -type f -name '*.log.*[^0-9-]*' -print0 | while read -d '' -r logfile; do
mv "${logfile}" "${logfile/.log.*/.log.`date -d ${logfile#*.log.} +%Y-%m-%d`}"
done

Assuming it's in a locale date knows how to handle.

where to define name of s3 bucket for different env while using fog gem

You can use Rails.env in order to customize your bucket name, like:

has_attached_file :news_logo,
:storage => :fog,
:fog_credentials => "#{Rails.root}/config/s3.yml",
:fog_directory => "s3-bucket-name-#{Rails.env}"

you can also do something like:

has_attached_file :news_logo,
:storage => :fog,
:fog_credentials => "#{Rails.root}/config/s3.yml",
:fog_directory => (case Rails.env
when 'production' then 'my-production-bucket'
when 'testing' then 'testing-bucket'
else 'this-is-development-bucket';
end)

Rails]Initializing for Fog in development environment

I'd avoid putting any credentials in code. It's such a terrible idea and Heroku has the right idea. So what I do is use RVM and put a file .rvmrc in my project folder. I put .rvmrc in .gitignore as well.

Then you edit .rvmrc to have

export AWS_ACCESS_KEY_ID="BLAH"

so on and so forth. Anytime I "cd" into this directory my env is setup for me for that project by RVM. If you're not using RVM there is other alternatives out there.

rails s

and it will have all the environment variables setup that you put in your .rvmrc script. No need for initializer or development only yaml config files that you keep out of source control. From my experience this is the simplest solution.

Carrierwave, Fog, Amazon S3, Rails 4

It appears as if Fog is using 'excon' in order to connect with the S3 bucket. I resolved my issue by removing fog and excon 0.27.2 . I switched over to the carrierwave-aws gem (using the same credentials, and everything is working as expected).



Related Topics



Leave a reply



Submit