lundi 1 juin 2015

AWS::S3::Errors::NoSuchKey: No Such Key error

I'm trying to create a method that deletes files on an S3 instance, but I am getting a AWS::S3::Errors::NoSuchKey: No Such Key error when I try to call .head or .read on an object.

app/models/file_item.rb

def thumbnail
    {
      exists: thumbnailable?,
      small: "http://#{bucket}.s3.amazonaws.com/images/#{id}/small_thumb.png",
      large: "http://#{bucket}.s3.amazonaws.com/images/#{id}/large_thumb.png"
    }
end 

lib/adapters/amazons3/accessor.rb

module Adapters
  module AmazonS3
    class Accessor
      S3_BUCKET =  AWS::S3.new.buckets[ENV['AMAZON_BUCKET']]

      ...

      def self.delete_file(thumbnail)
        prefix_pattern = %r{http://[MY-S3-HOST]-[a-z]+.s3.amazonaws.com/}
        small_path = thumbnail[:small].sub(prefix_pattern, '')
        large_path = thumbnail[:large].sub(prefix_pattern, '')
        small = S3_BUCKET.objects[small_path]
        large = S3_BUCKET.objects[large_path]
        binding.pry
        S3_BUCKET.objects.delete([small, large])
      end
    end
  end
end

example url1

"http://ift.tt/1Jkj5Up"

example url2

"http://ift.tt/1cu070G"




Aucun commentaire:

Enregistrer un commentaire