The most efficient way to do this is to simply move the existing S3 object (assuming your storage level is S3):
def rename(new_name) bucket_name = "yourapp-#{Rails.env}" resource = Aws::S3::Resource.new bucket = resource.bucket(bucket_name) object = bucket.object(path) new_filename = "#{new_name}#{File.extname(path)}" new_path = File.join(File.dirname(path), new_filename) object.move_to(bucket: bucket_name, key: new_path) model.update_column(mounted_as, new_filename) model.reload
It uses the aws-sdk-s3 gem.
source share