Skip to content

Instantly share code, notes, and snippets.

@dynamicguy
Last active April 6, 2023 18:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dynamicguy/719dd51c5488cc87c9d855fc620ced24 to your computer and use it in GitHub Desktop.
Save dynamicguy/719dd51c5488cc87c9d855fc620ced24 to your computer and use it in GitHub Desktop.
batch update s3 objects metadata with proper mime-type
#!/usr/bin/env ruby
require 'aws-sdk-s3'
require 'rack'
class BucketListObjectsWrapper
attr_reader :bucket
def initialize(bucket)
@bucket = bucket
end
# Lists object in a bucket and update it's mime type.
#
# @param start [Integer] The maximum number of objects to list.
# @param max_objects [Integer] The maximum number of objects to list.
# @return [Integer] The number of objects listed.
def list_objects(start, max_objects)
count = 0
puts "The objects in #{@bucket.name} are:"
@bucket.objects.each do |obj_summary|
if count >= start
source = Aws::S3::Object.new(obj_summary.bucket_name, obj_summary.key)
target = source
mime = Rack::Mime.mime_type('.' + obj_summary.key.split('.').last)
puts "processing key: #{obj_summary.key} to set content type as: #{mime}"
target.copy_from(source, content_type: mime, metadata_directive: 'REPLACE')
end
count += 1
break if count == max_objects
end
count
rescue Aws::Errors::ServiceError => e
puts "Couldn't list objects in bucket #{bucket.name}. Here's why: #{e.message}"
0
end
end
def run_script
bucket_name = "oscar-dev-document-service"
if ARGV.length != 2
puts "We need exactly two arguments. a start and limit. Example: ruby s3batch.rb 0 2"
exit
end
bkt = Aws::S3::Bucket.new(bucket_name)
wrapper = BucketListObjectsWrapper.new(bkt)
count = wrapper.list_objects(ARGV[0].to_i, ARGV[1].to_i)
puts "Updated #{count} objects out of #{bkt.objects.count}"
end
run_script if $PROGRAM_NAME == __FILE__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment