Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to write to tmp file or stream an image object up to s3 in ruby on rails
    primarykey
    data
    text
    <p>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. </p> <pre><code> origImage = MiniMagick::Image.open(myPhoto.tempfile.path) origImage.resize "200x200" thumbKey = "tiny-#{key}" obj = bucket.objects[thumbKey].write(:file =&gt; origImage.write("tiny.jpg")) </code></pre> <p>I can upload the original file just fine to s3 with the below command:</p> <pre><code>obj = bucket.objects[key].write('data') obj.write(:file =&gt; myPhoto.tempfile) </code></pre> <p>I think I want to create a temp file, read the image file into it and upload that:</p> <pre><code> thumbFile = Tempfile.new('temp') thumbFile.write(origImage.read) obj = bucket.objects[thumbKey].write(:file =&gt; thumbFile) </code></pre> <p>but the origImage class doesn't have a read command.</p> <p><strong>UPDATE:</strong> I was reading the source code and found this out about the write command</p> <pre><code># 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 </code></pre> <p>And the s3 api docs say you can stream the content using a code block like:</p> <pre><code>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 </code></pre> <p>How do I change my code so </p> <p>origImage.write(s3stream here)</p> <p><a href="http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/S3/S3Object.html" rel="nofollow">http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/S3/S3Object.html</a></p> <p><strong>UPDATE 2</strong></p> <p>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.</p> <pre><code> #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 =&gt; newFile) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload