zoukankan      html  css  js  c++  java
  • ruby on rails 把阿里云上的图片资源转移到七牛云上写的一个task 文件 自动转移

    先写下关于转移时,配置文件的修改

    1 Gemfile中增加

    gem 'carrierwave-qiniu'
    gem 'qiniu-rs'

    2 增加文件 config文件下qiniu-rs.rb

    Qiniu::RS.establish_connection! :access_key => 'key',
    :secret_key => 'key'

    3 avatar_upload.rb中

      storage :file 改为 storage :qiniu

    4 carrierwave.rb文件中增加

    config.storage = :qiniu
    config.qiniu_access_key = "key"
    config.qiniu_secret_key = "key"
    config.qiniu_bucket = "  "
    config.qiniu_bucket_domain = " "
    config.qiniu_block_size = 4*1024*1024
    config.qiniu_protocal = "http"

    这样配置文件就改好了

    参考 https://github.com/huobazi/carrierwave-qiniu

    https://github.com/qiniu/ruby-sdk

    接下来就是task 文件了 

    require 'rake'
    
    namespace :utils do
    
    	desc "Deploy all picture in local to QiuNiuCloud"
    	task :deploy_picture_to_qiniu => :environment do
    		@coy = Company.all
    		@tech = Technician.all
    		@ve = Venue.all
    		@sm = ServiceMenu.all
    		@pic = Pic.all
    		modified_by_shell
    		upload_to_qiniu(@coy,1)
    		upload_to_qiniu(@tech,1)
    		upload_to_qiniu(@ve,1)
    		upload_to_qiniu(@sm,1)
    		upload_to_qiniu(@pic,2)
    	end
    
    	desc "update_gemfile"
    	task :update_gemfile => :environment do
    		url = Rails.root.to_s+"/Gemfile"
    		context = "gem "qiniu-rs" \n gem "carrierwave-qiniu"" #\转义符号
    		`sed -i '4 i #{context}' #{url}`  #ruby中用``执行shell脚本
    	end
    end
    
    def upload_to_qiniu(ava,a)
    	case a
    	when 1
    		if !ava.nil?
    			ava.each do |t|
    				if t.avatar.url.nil? 
    					puts "avatar is nil"
    				else
    					puts t.avatar.current_path
    					if FileTest::exist?(t.avatar.current_path)
    						t.avatar = File.open(t.avatar.current_path)
    						t.save!
    						puts "upload is success to qiniu"
    						puts "qiniu address #{t.avatar.url}"
    					else
    						puts "file is not exist"
    					end
    				end
    			end
    		end
    	when 2
    		if !ava.nil?
    			ava.each do |t|
    				if t.photo.url.nil? 
    					puts "photo is nil"
    				else
    					puts t.photo.current_path
    					if FileTest::exist?(t.photo.current_path)
    						t.photo = File.open(t.photo.current_path)
    						t.save!
    						puts "upload is success to qiniu"
    						puts "qiniu address #{t.photo.url}"
    					else
    						puts "file is exist"
    					end
    				end
    			end
    		end
    	end
    end
    
    def modified_by_shell
    	
    	url = Rails.root.to_s+"/app/uploaders/avatar_uploader.rb"
    	`sed -i 's/storage :file/storage :qiniu/g' #{url}`
    
    	url = Rails.root.to_s+"/app/uploaders/photo_uploader.rb"
    	`sed -i 's/storage :file/storage :qiniu/g' #{url}`
    
    	url = Rails.root.to_s+"/config/initializers/carrierwave.rb"
    	con = IO.read(url)
    	if con.include?("qiniu")
    		puts "carrierwave.rb already update"
    	else
    		context = "config.storage             = :qiniu\nconfig.qiniu_access_key    = "93vlzlK9UlO6UhZaVlrZ4RyVanIv5f1meAX_ofK2"\nconfig.qiniu_secret_key    = "7UGe9arh_jrxTQGa1WLba3D8xDZ-FbXOJSVYAJt7"\nconfig.qiniu_bucket        = "xiadan"\nconfig.qiniu_bucket_domain = "xiaddan.qiniudn.com"\nconfig.qiniu_block_size    = 4*1024*1024\nconfig.qiniu_protocal      = "http""
    		`sed -i '12 i #{context}'  #{url}`
    	end	
    	
    
    	url = Rails.root.to_s+"/config/qiniu-rs.rb"
    	context = "Qiniu::RS.establish_connection! :access_key => '93vlzlK9UlO6UhZaVlrZ4RyVanIv5f1meAX_ofK2', 
     :secret_key => '7UGe9arh_jrxTQGa1WLba3D8xDZ-FbXOJSVYAJt7'"
    	if !FileTest::exist?(url)
    		`touch #{url}`
    		file = File.open("#{url}","w")
    		file.puts context
    		file.close
    	else
    		puts "qiniu-rs.rb file is exist"
    	end
    end
    

      

  • 相关阅读:
    计算机三级数据库-指导
    spring导入约束
    hibernater获取session时org.hibernate.service.spi.ServiceException错误
    springxml配置注入报错
    右键复制类的完整路径
    关于实体里的toString方法
    idea创建web项目环境
    框架快速找类
    如何在scdn博客里搜索自己博客文章
    永久消除自动产生的QQPCMgr
  • 原文地址:https://www.cnblogs.com/duxiaolong/p/3241868.html
Copyright © 2011-2022 走看看