首页 > 解决方案 > S3 存储桶对象正在上传重复文件

问题描述

我很难弄清楚为什么要上传重复的文件。这是正在发生的事情:

  1. 我们上传不同尺寸的照片(即 75 宽、300 宽、1920 宽等)。
  2. 上传成功,我可以在日志中看到没有创建两次照片。照片上传是按顺序运行的,但每次 S3 中都有两条 300 宽的记录。它只发生在那种尺寸的照片上,每次。

即使我运行一些代码来检查对象是否已经存在,也会为该特定宽度创建重复项。

这是 S3 类上传代码:

def self.upload(file, bucket, object_path)
    # Build and setup path to object
    unless bucket.object(object_path).exists?
      obj = bucket.object(object_path)
      # Upload file to object path
      obj.upload_file(file.path, acl: 'public-read')
      # # Return S3 public url of cached image
      obj.public_url({virtual_host: true}).gsub("REDACTED.com", "cdn.REDACTED.com").gsub("http://", "https://")
    end
  end

副本实际上是完全相同的。这不是 UI 故障,我可以在S3.bucket.objects.to_a

总结:每次上传重复 300w,即使所有大小都是一起上传的,每次都一致地复制一个特定文件。

有人有什么想法吗?

编辑

这是缓存被迭代和上传的代码:

initial_cache_sizes = [
  {width: 75, height: 50},
  # Width 300
  {width: 300, height: 200},
  # Width 830
  {width: 830, height: 553},
]
caches = initial_cache_sizes.map {|cache| create_cache(cache)}

# create an object to be uploaded
def create_cache(attributes)
  Cache.new(self, attributes).build
end

class Cache
  # Tableless Model | Extending methods
  extend ActiveModel::Naming
  # Tableless Model | Setting attributes
  attr_accessor :cacheable, :size, :width, :height, :path, :disclaimer, :aspect_ratio, :vendor_watermark, :company_watermark, :file
  # Tableless Model | Initialize based on PlanOcore
  def initialize(cacheable, attributes = {})
    # Build model from attributes
    attributes.each do |name, value|
      # Save value to model
      send("#{name}=", value)
    end
    # Save photo or floor_plan to cacheable
    send("cacheable=", cacheable)
    # Save file to cache
    send("file=", MiniMagick::Image.open(cacheable.path))
  end
  # Build cache file
  def build
    begin
      # Resize if width and height are given.
      resize if width && height
      # Resize if a size was given.
      crop(aspect_ratio) if aspect_ratio
      # Add vendor watermark if asked for and available.
      add_watermark(cacheable.vendor_watermark, "south-east") if vendor_watermark
      # Add company watermark if asked for and available.
      add_watermark(cacheable.company_watermark, "south-west") if company_watermark
      # Add disclaimer is asked for.
      add_disclaimer if disclaimer
      # Upload to S3
      upload
    ensure
      unlink # Delete tempfile
    end
    response_format # Return cache.
  end

def upload
  # Build S3 path.
  s3_path = "#{cacheable.s3_path}/#{file_name}"
  # Upload file to S3
  send("path=", S3.upload(file, cacheable.bucket, s3_path))
end

# S3.upload code outlined above

这是记录缓存对象的输出caches = initial_cache_sizes.map {|cache| create_cache(cache)}

{:size=>5041, :width=>75, :height=>50, :path=>"REDACTED", :key=>"75w50h", :disclaimer=>nil, :vendor_watermark=>nil, :company_watermark=>nil}
{:size=>37317, :width=>300, :height=>200, :path=>"REDACTED", :key=>"300w200h", :disclaimer=>nil, :vendor_watermark=>nil, :company_watermark=>nil}
{:size=>186293, :width=>830, :height=>553, :path=>"REDACTED", :key=>"830w553h", :disclaimer=>nil, :vendor_watermark=>nil, :company_watermark=>nil}
{:size=>264617, :width=>1024, :height=>682, :path=>"REDACTED", :key=>"1024w682h", :disclaimer=>nil, :vendor_watermark=>nil, :company_watermark=>nil}
{:size=>787140, :width=>1920, :height=>1280, :path=>"REDACTED", :key=>"1920w1280h", :disclaimer=>nil, :vendor_watermark=>nil, :company_watermark=>nil}

如您所见,只有一个 300 宽度的文件,但 S3 中存在两个。

标签: rubyamazon-s3ruby-on-rails-5aws-sdk-ruby

解决方案


推荐阅读