Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unable to delete file from amazon s3 using ruby script

I am using aws-sdk-ruby for deleting a file saved in a bucket in my amazon s3 account, but i can't figure out why i am able to delete the desired file from S3 bucket using the following code.

this is my code

require 'aws-sdk-v1'
require 'aws-sdk'

ENV['AWS_ACCESS_KEY_ID'] = "XXXXXXX"
ENV["AWS_SECRET_ACCESS_KEY"] = '/ZZZZZZZZ'
ENV['AWS_REGION'] = 'us-east-1'


s3 = Aws::S3::Resource.new
bucket = s3.bucket('some-bucket')
obj = bucket.object('https://s3.amazonaws.com/some-bucket/38ac8226-fa72-4aee-8c3d-a34a1db77b91/some_image.jpg')
obj.delete
like image 864
Sachin Singh Avatar asked Oct 31 '25 14:10

Sachin Singh


1 Answers

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 of AWS::S3
like image 187
spickermann Avatar answered Nov 03 '25 05:11

spickermann



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!