Skip to content

Instantly share code, notes, and snippets.

@dkam
Last active May 15, 2024 23:34
Show Gist options
  • Save dkam/af75f7ecf0d441ec38ccb3f6534debeb to your computer and use it in GitHub Desktop.
Save dkam/af75f7ecf0d441ec38ccb3f6534debeb to your computer and use it in GitHub Desktop.
Ensure all objects in s3 bucket are referenced in ActiveStorage::Blob
## Checking files in your bucket are referenced by ActiveStorage
require 'aws-sdk-s3'
s3_client = Aws::S3::Client.new(
region: "us-east-1",
access_key_id: Rails.application.credentials.dig(Rails.env, :s3, :access_key_id),
secret_access_key: Rails.application.credentials.dig(Rails.env, :s3, :secret_access_key),
endpoint: "https://myendpoint.com",
force_path_style: true
)
bucket_name = "bucket-name-#{Rails.env}"
s3_client.list_objects_v2(bucket: bucket_name).contents.each do |object|
key = object.key
blob = ActiveStorage::Blob.find_by(key: key)
if blob.nil?
# If no corresponding blob, delete the file from S3
s3_client.delete_object(bucket: bucket_name, key: key)
puts "Deleted unreferenced file from S3: #{key}"
else
puts "File is referenced in ActiveStorage::Blob: #{key}"
end
end
## Check ActiveStorage::Blobs are associated with a record
# Find all blobs that are not attached to any records
dangling_blobs = ActiveStorage::Blob.where.missing(:attachments)
dangling_blobs.find_each do |blob|
begin
# Purge the blob (deletes from storage service and database)
blob.purge
puts "Purged blob: #{blob.key}"
rescue => e
puts "Failed to purge blob: #{e.message}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment