Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 5.2 Active Storage purging/deleting attachements won't delete Blob or Attachent records

I am using rails 5.2 and I am trying to set up Active Storage with Amazon S3. My application has full access to S3 and I am able to attach an avatar image to a user. But when I try to delete the avatar, I run into the following issues:

> user.avatar.attached? #true`
> user.avatar.purge
   S3 Storage (697.9ms) Deleted file from key: Ns1KBRzdgxLNnY31sH72vT5t
   S3 Storage (227.0ms) Deleted files by key prefix: variants/Ns1KBRzdgxLNnY31sH72vT5t/
Aws::S3::Errors::AccessDenied: Access Denied

Then when I inspect the bucket, the file was actually deleted, but looking in the database, both Blob, and the Attachment records are still present.

Any ideas why this is happening?

EDIT I made some updates in my IAM permissions following the advice from the accepted answer. These are the items that I updated:

  • Attached a policy to my IAM user, instead of my s3 bucket.
  • Listed all the required actions.
  • Add the whole bucket in the resource field, not just all objects.

In the end my policy json looked like this:

    {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Sid": "VisualEditor0",
                "Effect": "Allow",
                "Action": [
                    "s3:PutObject",
                    "s3:GetObject",
                    "s3:ListBucket",
                    "s3:DeleteObject"
                ],
                "Resource": [
                    "arn:aws:s3:::my-bucket/*",
                    "arn:aws:s3:::my-bucket"
                ]
            }
        ]
    }
like image 504
felix Avatar asked May 14 '18 22:05

felix


People also ask

How do I delete active storage attachments?

To remove an attachment from a model, call purge on the attachment. If your application is set up to use Active Job, removal can be done in the background instead by calling purge_later . Purging deletes the blob and the file from the storage service.

How does active storage work in Rails?

Active Storage uses two tables in your application's database named active_storage_blobs and active_storage_attachments . After creating a new application (or upgrading your application to Rails 5.2), run rails active_storage:install to generate a migration that creates these tables.


1 Answers

The S3 account you use for Active Storage must have the s3:DeleteObject permission for the entire bucket. (As specified in the Active Storage guide, it must also have the s3:ListBucket, s3:PutObject, and s3:GetObject permissions.)

like image 81
George Claghorn Avatar answered Oct 27 '22 01:10

George Claghorn