Skip to content

Instantly share code, notes, and snippets.

@samsonjs
Created May 30, 2025 17:08
Show Gist options
  • Save samsonjs/8c5be06a583b0f534f9ba1366f02d5de to your computer and use it in GitHub Desktop.
Save samsonjs/8c5be06a583b0f534f9ba1366f02d5de to your computer and use it in GitHub Desktop.
Hack to make the Ruby gem aws-sdk-s3 work with Backblaze B2
module Vidjo
# Factory for Aws::S3::Client so we only have to configure it in one place
module StorageClient
def self.new_s3_client
# The B2 client doesn't work with stubbing for some reason, whatever
stub_responses = ENV.fetch('S3_CLIENT_STUB_RESPONSES', nil) == 'yes'
if stub_responses
Aws::S3::Client.new(
access_key_id: ENV.fetch('B2_KEY_ID'),
secret_access_key: ENV.fetch('B2_APP_KEY'),
endpoint: ENV.fetch('B2_ENDPOINT'),
region: ENV.fetch('B2_REGION'),
force_path_style: true,
stub_responses: true
)
else
B2S3Client.new(
access_key_id: ENV.fetch('B2_KEY_ID'),
secret_access_key: ENV.fetch('B2_APP_KEY'),
endpoint: ENV.fetch('B2_ENDPOINT'),
region: ENV.fetch('B2_REGION'),
force_path_style: true
)
end
end
end
# Subclass that strips checksums because B2 isn't compatible with them
class B2S3Client < Aws::S3::Client
# Plugin to strip checksum headers from requests
class StripChecksumHeaders < Seahorse::Client::Plugin
def add_handlers(handlers, _config)
handlers.add(Handler, step: :build, priority: 0)
end
# Deletes all checksum headers from requests
class Handler < Seahorse::Client::Handler
def call(context)
headers = context.http_request.headers
headers.keys.grep(/^x-amz(-sdk)?-checksum-/i).each do |key|
headers.delete(key)
end
@handler.call(context)
end
end
end
add_plugin(StripChecksumHeaders)
define_singleton_method(:api) { Aws::S3::Client.api }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment