jstamos
jstamos

Reputation: 55

Add cache_control to images with Ruby aws-sdk

I'm trying to add cache_control to images within s3 buckets via a Ruby script, yet I keep running into an Access Denied (Aws::S3::Errors::AccessDenied) error. All my environment variables are correct, and I'm having no issue creating new buckets with the script, it's just every time I try to add cache_control it throws an error.

I've tried digging into the aws-sdk documentation, https://docs.aws.amazon.com/sdk-for-ruby/v2/api/Aws/S3/Object.html, but I haven't been able to wrap my mind around what is wrong with my script.

Here's what the code looks like when it's functional:

  def initialize(region: 'us-west-2', bucket_name: 'project-images')
    @bucket =  Aws::S3::Resource.new(region: region).bucket(bucket_name)
  end

  def copy(to:, from:)
     bucket.objects(prefix: from).each do |object|
      _, filename = object.key.split("/")
      object.copy_to(bucket: bucket.name, key: "#{to}/#{filename}")
    end
  end

Here's what the code looks like when I'm getting the Access Denied errors from trying to add in the cache_control and additional options:

  def initialize(region: 'us-west-2', bucket_name: 'project-images')
    @bucket =  Aws::S3::Resource.new(region: region).bucket(bucket_name)
  end

  def copy(to:, from:)
     bucket.objects(prefix: from).each do |object|
      _, filename = object.key.split("/")
      object.copy_to(bucket: bucket.name, key: "#{to}/#{filename}", acl: "public-read", cache_control: "max-age=154400", metadata_directive: "REPLACE")
    end
  end

Any help would be greatly appreciated!

Upvotes: 1

Views: 52

Answers (0)

Related Questions