I'm using the s3_direct_upload gem to store images and videos on Amazon s3. When the image or video is changed or deleted, I want to nuke the old image or video on s3 and save everyone money and space.
This solution uses the V1 Aws SDK and is no longer valid:
http://blog.littleblimp.com/post/53942611764/direct-uploads-to-s3-with-rails-paperclip-and
This solution deletes files that were initially uploaded in a batch, but does nothing for the final files post-processing:
github - waynehoover/s3_direct_upload
Here is the Aws v2 SDK doc, which seems clear enough:
http://docs.aws.amazon.com/sdkforruby/api/Aws/S3/Client.html#delete_object-instance_method
Yet this solution:
class Image < ActiveRecord::Base
validates :name, :s3_image_file_url, :s3_image_file_path, :s3_image_file_key, presence: true
before_destroy :delete_s3_files
private
def delete_s3_files
s3 = Aws::S3::Client.new()
response = s3.delete_object(
bucket: Rails.configuration.aws[:bucket],
key: self.s3_image_file_key
)
end
end
...returns only:
=> #<struct delete_marker=nil, version_id=nil>
(And the file is still available on s3 at the original url.)
Thoughts? Hasn't everyone had to do this?
I've written code using the v2 SDK to delete objects from S3. Here is a sample from my codebase:
def delete_avatar_from_s3
Aws::S3::Client.new.delete_object(
bucket: "user-avatars",
key: "#{@id}"
)
return true
rescue => e
Rails.logger.error "Error deleting avatar of user #{@id}. Failure with S3 call. Details: #{e}; #{e.backtrace}"
return false
end
It looks similar to yours, so I don't think that this code is the issue. Have you confirmed your bucket & key names and ensured that your method is actually being called?
AWS SDK for Ruby V2
delete one or more file, just pass objects
array containing keys.
def delete_from_s3
S3_BUCKET.delete_objects({
delete: {
objects: [
{key: key}
]
}
})
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.