如何写入tmp文件或流的图像对象在Ruby on Rails的到S3图像、对象、文件、tmp

2023-09-11 09:47:00 作者:明明动了情、

在code以下重新调整我的形象。但我不知道怎么写出来到一个临时文件或斑点,所以我可以把它上传到S3。

  origImage = MiniMagick :: Image.open(myPhoto.tempfile.path)
  origImage.resize200×200
  thumbKey =微小 - #{}键

  OBJ = bucket.objects [thumbKey] .WRITE(:文件=> origImage.write(tiny.jpg))
 

我可以上传原始​​文件蛮好S3使用以下命令:

  OBJ = bucket.objects [关键] .WRITE(数据)
obj.write(:文件=> myPhoto.tempfile)
 

我觉得我要创建一个临时文件,读取图像文件到它,并上传:

  thumbFile = Tempfile.new('临时')
  thumbFile.write(origImage.read)
  OBJ = bucket.objects [thumbKey] .WRITE(:文件=> thumbFile)
 
使用Ruby on Rails快速开发web应用的教程实例

但origImage类没有读命令。

更新:我在读源$ C ​​$ C,发现了这一点,对写入命令

 #(通过传递一个字符串)或写入临时文件出到一个文件位置
#传递一个流,你可以#write(块)反复
#
#@参数output_to [iostream的,字符串]某种流对象的需要被读取或文件路径作为字符串
#@返回[iostream的,布尔如果你传递一个文件位置[字符串]那么你得到一个成功的布尔值。如果其流,你把它找回来。
#写,我们使用的是处理到输出路径的临时图像
 

和S3的API文档说,你可以用code座喜欢流内容:

  obj.write办|缓冲,字节|
 #写入比请求的字节数到缓冲较少
 #将导致写入停止屈服于块
结束
 

如何更改我的code所以

origImage.write(s3stream这里)

http://docs.aws.amazon.com/ AWSRubySDK /最新/ AWS / S3 / S3Object.html

更新2

这code成功上传的缩略图文件到S3。但我仍然爱知道如何流不起来。这将是更有效的,我认为。

  #resize图像,并上传缩略图
  smallImage = MiniMagick :: Image.open(myPhoto.tempfile.path)
  smallImage.resize200×200
  thumbKey =微小 - #{}键
  NEWFILE = Tempfile.new(tempimage)
  smallImage.write(newFile.path)
  OBJ = bucket.objects [thumbKey] .WRITE(数据)
  obj.write(:文件=> NEWFILE)
 

解决方案

smallImage.to_blob

下面从 https://github.com code复制/probablycorey/mini_magick/blob/master/lib/mini_magick.rb

 #为您提供了原始图像数据传回
    #@返回[字符串]二进制字符串
    高清to_blob
      F = File.new @path
      f.binmode
      f.read
    确保
      f.close若f
    结束
 

The code below resizes my image. But I am not sure how to write it out to a temp file or blob so I can upload it to s3.

  origImage = MiniMagick::Image.open(myPhoto.tempfile.path)
  origImage.resize "200x200"
  thumbKey = "tiny-#{key}"

  obj = bucket.objects[thumbKey].write(:file => origImage.write("tiny.jpg"))

I can upload the original file just fine to s3 with the below command:

obj = bucket.objects[key].write('data')
obj.write(:file => myPhoto.tempfile)

I think I want to create a temp file, read the image file into it and upload that:

  thumbFile = Tempfile.new('temp')
  thumbFile.write(origImage.read)
  obj = bucket.objects[thumbKey].write(:file => thumbFile)

but the origImage class doesn't have a read command.

UPDATE: I was reading the source code and found this out about the write command

# Writes the temporary file out to either a file location (by passing in a String) or by
# passing in a Stream that you can #write(chunk) to repeatedly
#
# @param output_to [IOStream, String] Some kind of stream object that needs to be read or a file path as a String
# @return [IOStream, Boolean] If you pass in a file location [String] then you get a success boolean. If its a stream, you get it back.
# Writes the temporary image that we are using for processing to the output path

And the s3 api docs say you can stream the content using a code block like:

obj.write do |buffer, bytes|
 # writing fewer than the requested number of bytes to the buffer
 # will cause write to stop yielding to the block
end

How do I change my code so

origImage.write(s3stream here)

http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/S3/S3Object.html

UPDATE 2

This code successfully uploads the thumbnail file to s3. But I would still love to know how to stream it up. It would be much more efficient I think.

  #resize image and upload a thumbnail
  smallImage = MiniMagick::Image.open(myPhoto.tempfile.path)
  smallImage.resize "200x200"
  thumbKey = "tiny-#{key}"
  newFile = Tempfile.new("tempimage")
  smallImage.write(newFile.path)
  obj = bucket.objects[thumbKey].write('data')
  obj.write(:file => newFile)

解决方案

smallImage.to_blob ?

below code copy from https://github.com/probablycorey/mini_magick/blob/master/lib/mini_magick.rb

    # Gives you raw image data back
    # @return [String] binary string
    def to_blob
      f = File.new @path
      f.binmode
      f.read
    ensure
      f.close if f
    end