我正在尝试使用 S3 文件存储和通过 Heroku 上的 ruby-on-rails 应用程序访问 sdk 来创建地理标记图像的 kmz 格式文件。
当“项目”视图加载时,我正在运行文件进程,但我为访问 S3 和处理文件而编写的实例方法需要大约 40 秒才能完成,从而导致 504 超时错误。
我已经考虑在没有本地下载的情况下压缩 S3 本身,但这似乎不可行。有没有更好的方法来处理这个下载/上传过程来加速它,或者有一个更好的地方来运行它以避免超时?
项目控制器中的方法:
# GET /projects/1
# GET /projects/1.json
def show
@pictures = @project.pictures.all
@project.generate_kml
@project.download_project
@project.generate_kmz
end
完整的细节:
def generate_kml
content = []
content.push('<?xml version="1.0" encoding="UTF-8"?>')
content.push('<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">')
content.push('<Document>')
content.push("<name>#{self.id}.kmz</name>")
#cycles through each picture in db for the current project
self.pictures.each do |pic|
pic_title = pic.image.to_s.split('/').last
content.push('<Placemark>')
content.push("<name>#{pic_title}</name>")
content.push('<description>')
content.push('<![CDATA[')
line = '<img style="max-width:1000px;" src="' + '' + pic_title + '">'
content.push(line)
content.push(']]>')
content.push('</description>')
content.push('<Point>')
content.push("<coordinates>-#{pic.long},#{pic.lat}</coordinates>")
content.push('</Point>')
content.push('</Placemark>')
end
content.push('</Document>')
content.push('</kml>')
#pushes upload to S3 folder
s3 = Aws::S3::Resource.new
obj = s3.bucket(ENV['S3_BUCKET']).object("uploads/" + "#{self.id}" + "/doc.kml")
File.open("kml_temp", "w+") { |f|
f.puts(content)
obj.put(body: f)
}
end
def generate_kmz
#create
directory_to_zip = "/tmp/#{self.id}"
output_file = "/tmp/kmz_directory/#{self.id}.kmz"
zf = ZipFileGenerator.new(directory_to_zip, output_file)
zf.write()
#send to S3
s3 = Aws::S3::Resource.new
obj = s3.bucket(ENV['S3_BUCKET']).object("uploads/kmz_directory/" + "#{self.id}.kmz")
obj.upload_file("/tmp/kmz_directory/#{self.id}.kmz")
end
def download_project
#tmp cleanup
#FileUtils.rm_r '/tmp'
#delete target directory if exists
if Dir.exist?("/tmp/#{self.id}")
FileUtils.remove_dir("/tmp/#{self.id}")
end
#create kmz_dir if needed
if Dir.exist?("/tmp/kmz_directory")
else
FileUtils.mkdir "/tmp/kmz_directory"
end
#create target dir
FileUtils.mkdir "/tmp/#{self.id}"
#download pics
s3 = Aws::S3::Resource.new
s3.bucket(ENV['S3_BUCKET']).object_versions({ prefix:"uploads/#{self.id}" }).each do |object|
#get file name
full_key = object.key
file_name = full_key.to_s.split('/').last
#save to /tmp
object.get(response_target: "/tmp/#{self.id}/#{file_name}")
end
end