Method: Paperclip::Storage::S3#flush_writes

Defined in:
lib/paperclip/storage/s3.rb

#flush_writesObject

:nodoc:



351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'lib/paperclip/storage/s3.rb', line 351

def flush_writes #:nodoc:
  @queued_for_write.each do |style, file|
  retries = 0
    begin
      log("saving #{path(style)}")
      write_options = {
        :content_type => file.content_type,
        :acl => s3_permissions(style)
      }

      # add storage class for this style if defined
      storage_class = s3_storage_class(style)
      write_options.merge!(:storage_class => storage_class) if storage_class

      if @s3_server_side_encryption
        write_options[:server_side_encryption] = @s3_server_side_encryption
      end

      style_specific_options = styles[style]

      if style_specific_options
        merge_s3_headers( style_specific_options[:s3_headers], @s3_headers, ) if style_specific_options[:s3_headers]
        .merge!(style_specific_options[:s3_metadata]) if style_specific_options[:s3_metadata]
      end

      write_options[:metadata] =  unless .empty?
      write_options.merge!(@s3_headers)

      s3_object(style).upload_file(file.path, write_options)
    rescue ::Aws::S3::Errors::NoSuchBucket
      create_bucket
      retry
    rescue ::Aws::S3::Errors::SlowDown
      retries += 1
      if retries <= 5
        sleep((2 ** retries) * 0.5)
        retry
      else
        raise
      end
    ensure
      file.rewind
    end
  end

  after_flush_writes # allows attachment to clean up temp files

  @queued_for_write = {}
end