Skip to content

Instantly share code, notes, and snippets.

@dynamicguy
Last active April 6, 2023 18:17
Show Gist options
  • Save dynamicguy/719dd51c5488cc87c9d855fc620ced24 to your computer and use it in GitHub Desktop.
Save dynamicguy/719dd51c5488cc87c9d855fc620ced24 to your computer and use it in GitHub Desktop.

Revisions

  1. dynamicguy revised this gist Apr 6, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion s3batch.rb
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@ def initialize(bucket)
    @bucket = bucket
    end

    # Lists object in a bucket update it's mime type.
    # 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.
  2. dynamicguy renamed this gist Apr 6, 2023. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. dynamicguy revised this gist Apr 5, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion s4batch.rb
    Original file line number Diff line number Diff line change
    @@ -38,7 +38,7 @@ def list_objects(start, max_objects)

    def run_script
    bucket_name = "oscar-dev-document-service"
    puts ARGV[1]

    if ARGV.length != 2
    puts "We need exactly two arguments. a start and limit. Example: ruby s3batch.rb 0 2"
    exit
  4. dynamicguy revised this gist Apr 5, 2023. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions s4batch.rb
    Original file line number Diff line number Diff line change
    @@ -23,7 +23,7 @@ def list_objects(start, max_objects)
    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: \t#{obj_summary.key} to set content type as: \t#{mime}"
    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
    @@ -38,13 +38,15 @@ def list_objects(start, max_objects)

    def run_script
    bucket_name = "oscar-dev-document-service"
    puts ARGV[1]
    if ARGV.length != 2
    puts "We need exactly two arguments. a start and limit. Example: ruby s3batch.rb 0 2"
    exit
    end
    wrapper = BucketListObjectsWrapper.new(Aws::S3::Bucket.new(bucket_name))
    bkt = Aws::S3::Bucket.new(bucket_name)
    wrapper = BucketListObjectsWrapper.new(bkt)
    count = wrapper.list_objects(ARGV[0].to_i, ARGV[1].to_i)
    puts "Listed #{count} objects."
    puts "Updated #{count} objects out of #{bkt.objects.count}"
    end

    run_script if $PROGRAM_NAME == __FILE__
  5. dynamicguy created this gist Apr 5, 2023.
    50 changes: 50 additions & 0 deletions s4batch.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    #!/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 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: \t#{obj_summary.key} to set content type as: \t#{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
    wrapper = BucketListObjectsWrapper.new(Aws::S3::Bucket.new(bucket_name))
    count = wrapper.list_objects(ARGV[0].to_i, ARGV[1].to_i)
    puts "Listed #{count} objects."
    end

    run_script if $PROGRAM_NAME == __FILE__