简体   繁体   中英

Rails paperclip S3 attachment not deleted (bad request)

I'm developing a rails API. I use the paperclip gem to store images in Amazon S3. I'm just using my own access key for the bucket, without any added policies. The attachments are correctly uploaded and stored in S3, but when I destroy a record, the attachments are not deleted. I also tried deleting the attachment alone, and that gave the following error:

[AWS S3 400 0.382023 0 retries] head_object(:bucket_name=>"my-bucket-name",:key=>"the/url/to/the/image.jpg") AWS::S3::Errors::BadRequest AWS::S3::Errors::BadRequest

In my model:

has_attached_file :main_image
validates_attachment :main_image, presence: true,
                     content_type: { content_type: %w(image/jpeg image/png)},
                     size: { in: 0 .. 1.megabytes }

In my configuration:

# Paperclip config
config.paperclip_defaults = {
    storage: :s3,
    s3_credentials: {
        bucket: ENV.fetch('AWS_S3_BUCKET'),
        access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'),
        secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'),
        s3_region: ENV.fetch('AWS_S3_REGION'),
    }
}

The app is running on Heroku. Is this a permissions issue? Note that I'm using the aws-sdk gem version 1.66.

This is a permissions issue with AWS S3 since you are able to upload but not delete. Did you create an AWS IAM user to generate an Access Key and Secret Key? If so can you paste your policy?

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM