unable to delete file from amazon s3 using ruby script
The documentation tells that it should look like this:
s3 = Aws::S3.new
bucket = s3.buckets['some-bucket']
object = bucket.objects['38ac8226-fa72-4aee-8c3d-a34a1db77b91/some_image.jpg']
object.delete
Please note:
- the square brackets,
- that the object's key doesn't include the domain and
- instead of creating an instance of
Aws::S3::Resource
create an instance ofAWS::S3
Rails aws-sdk cannot delete file on Amazon S3
Got it working. I was submitting the url rather than the key. The key is obtained from the url by slicing off the host bit:
Picturethings controller
def delete_picture_from_s3
key = params[:picture_url].split('amazonaws.com/')[1]
S3_BUCKET.object(key).delete
return true
rescue => e
# Do nothing. Leave the now defunct file sitting in the bucket.
return true
end
rails AWS S3 delete file
You must first configure the AWS
gem. Add this code to the config/initializers/aws.rb
file.
Aws.config.update({
region: '<default-region>',
credentials: Aws::Credentials.new('<access-key-id>', '<secret-access-key')
})
You can also set the environment variables AWS_ACCESS_KEY_ID
, AWS_SECRET_ACCESS_KEY
, and AWS_REGION
on your server and the SDK
will pick them up automatically.
Then, anywhere in your app or a controller action, you can call the S3 API
like this:
def some_action
# You can simply call Aws::S3::Client.new
# if you are already configuring using the
# above methods or configure by passing
# parameters explicitly
s3_client = Aws::S3::Client.new(
credentials: Aws::Credentials.new('<aws_access_key_id>', '<aws_secret_key>'),
region: '<aws_region>'
)
# delete object by passing bucket and object key
s3_response = s3_client.delete_object({
bucket: '<bucket-name>', # required
key: '<object-key>', # required
})
end
Delete object from s3 bucket Rails 5 aws-sdk
More than likely the issue that you are running into is with the permissions that are set on the s3
bucket and not your code. You either need to change the permissions on that bucket it self or you have to set up a policy that aws user that is trying trying to delete the file.
Rails Active Storage, delete file from S3 bucket
I don't know what was the problem but restarting my rails console fix the problem.
Fog Gem - Access Denied on deleting S3 file
From what I see, you are setting the bucket_name
variable on fog gem to be 'bucket-name'
, either that or you have edited it to post here.
Your config/initializer/carrierwave.rb
should look something like this
CarrierWave.configure do |config|
config.fog_credentials = {
# Configuration for Amazon S3 should be made available through an Environment variable.
# For local installations, export the env variable through the shell OR
# if using Passenger, set an Apache environment variable.
#
# In Heroku, follow http://devcenter.heroku.com/articles/config-vars
#
# $ heroku config:add S3_KEY=your_s3_access_key S3_SECRET=your_s3_secret S3_REGION=eu-west-1 S3_ASSET_URL=http://assets.example.com/ S3_BUCKET_NAME=s3_bucket/folder
# Configuration for Amazon S3
:provider => 'AWS',
:aws_access_key_id => ENV['S3_KEY'],
:aws_secret_access_key => ENV['S3_SECRET'],
:region => ENV['S3_REGION']
}
# For testing, upload files to local `tmp` folder.
if Rails.env.test? || Rails.env.cucumber?
config.storage = :file
config.enable_processing = false
config.root = "#{Rails.root}/tmp"
else
config.storage = :fog
end
config.cache_dir = "#{Rails.root}/tmp/uploads" # To let CarrierWave work on heroku
config.fog_directory = ENV['S3_BUCKET_NAME']
config.s3_access_policy = :public_read # Generate http:// urls. Defaults to :authenticated_read (https://)
config.fog_host = "#{ENV['S3_ASSET_URL']}/#{ENV['S3_BUCKET_NAME']}"
end
You may be setting ENV['S3_BUCKET_NAME']
or ENV['S3_ASSET_URL']
variables wrong, or even setting it mannually, check those in your .env
file
Related Topics
What's the Difference Between the Ruby Irb Prompt Modes
Cron Is Running in Home Directory Instead of File Directory
How to Refactor Openssl Pkcs5_Keyivgen in Ruby
Ruby on Rails: Converting "Somewordhere" to "Some Word Here"
Convert Ip Address to 32 Bit Integer in Ruby
How to Install Rmagick Gem on Windows
Ruby Installation (2.2.2) Fails in MACos Big Sur
Floating Point Precision in Ruby on Rails Model Validations
How to Set Correct Ruby Version in Gem Environment
Ruby: How Does Constant-Lookup Work in Instance_Eval/Class_Eval
Detect Similar Sounding Words in Ruby
How to Call a Method That Is a Hash Value
Does Anyone Have Parsing Rules for the Notepad++ Function List Plugin for Ruby and Rake
Can't Find Gem Railties (>= 0.A) with Executable Rails (Gem::Gemnotfoundexception)
Unexpected Keyword_End, Expecting $End (Syntaxerror)
What Could Be Causing This Rails Ioerror Closed Stream
Ruby's Argv Can Be Empty on Windows Depending on a Way to Run Script