ruby CarrierWave带有来自雾的备用URL,快速而肮脏。对于将生产数据提取到开发中非常有用,以防万一你必须全力以赴

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby CarrierWave带有来自雾的备用URL,快速而肮脏。对于将生产数据提取到开发中非常有用,以防万一你必须全力以赴相关的知识,希望对你有一定的参考价值。

  # Copy the file from S3 to local public folder
  def copy_to_local!(recursive=true)
    public_folder = Rails.root.join("public")

    path = self.path
    if path.start_with?(public_folder.to_s)
      local_path = path
      path = path[(public_folder.to_s.length+1)..path.length]
    else
      local_path = public_folder.join(path)
    end

    remote_url = "s3://#{S3_CONFIG['S3_BUCKET_NAME']}/#{path}"

    if File.exists?(local_path)
      puts " * Skipped existing file: #{local_path}"
    else
      cmd = "aws s3 cp #{remote_url} #{local_path}"
      puts " * #{cmd}"
      system cmd
    end

    versions.values.each { |v| v.copy_to_local! } if recursive
  end

  def url(*args)
    # For development data pulled from production/staging database, don't bother
    # to download all the assets (which may be 10GB+) to local folder. With this
    # it check if the local file exists, otherwise it returns a S3 URL.
    if Rails.env.development?

      if self.path.nil? or File.exists?(self.path)
        super
      else
        public_folder = Rails.root.join("public")

        path = self.path
        if path.start_with?(public_folder.to_s)
          path = path[(public_folder.to_s.length+1)..path.length]
        end

        "http://s3.amazonaws.com/#{S3_CONFIG['S3_BUCKET_NAME']}/#{path}"
      end
    else
      super
    end
  end

以上是关于ruby CarrierWave带有来自雾的备用URL,快速而肮脏。对于将生产数据提取到开发中非常有用,以防万一你必须全力以赴的主要内容,如果未能解决你的问题,请参考以下文章

ruby 使用CarrierWave延迟图像处理

ruby 更新s3上的carrierwave图像

ruby carrierwave.rb

ruby Carrierwave

ruby on rails nginx 如何上传大文件?

ruby Carrierwave图像模型验证图像尺寸/高度/宽度。