Is there a simple way to set AWS tags when uploading to S3 with ActiveStorage?

40 Views Asked by At

I want to set AWS tags when uploading to S3 with ActiveStorage. However, S3Service does not support that, even though the underlying aws-sdk-s3 library does.

I've actually managed to implement this, creating my own TaggingS3Service, but it ain't pretty, copying-and-pasting from S3Service, and it is also fragile, using reflection to access a private field.

So I wonder if there is any simpler way to do that that I'm missing.

Fwiw, here's my working solution.

app/services/active_storage/service/tagging_s3_service.rb

require 'active_storage/service/s3_service'
require 'uri'

class ActiveStorage::Service::TaggingS3Service < ActiveStorage::Service::S3Service
  def upload(key, io, checksum: nil, filename: nil, content_type: nil, disposition: nil, custom_metadata: {}, tagging: {}, **)
    instrument :upload, key: key, checksum: checksum do
      content_disposition = content_disposition_with(filename: filename, type: disposition) if disposition && filename

      tagging_str = URI.encode_www_form(tagging) if tagging
      if io.size < multipart_upload_threshold
        upload_with_single_part key, io, checksum: checksum, content_type: content_type, content_disposition: content_disposition, custom_metadata: custom_metadata, tagging_str: tagging_str
      else
        upload_with_multipart key, io, content_type: content_type, content_disposition: content_disposition, custom_metadata: custom_metadata, tagging_str: tagging_str
      end
    end
  end

  def upload_with_single_part(key, io, checksum: nil, content_type: nil, content_disposition: nil, custom_metadata: {}, tagging_str: nil)
    object_for(key).put(
      body: io,
      content_md5: checksum,
      content_type: content_type,
      content_disposition: content_disposition,
      metadata: custom_metadata,
      tagging: tagging_str,
      **upload_options)
    # commenting out the following lines, as they result in a warning: Cannot find 'BadDigest' for type 'Module<Aws::S3::Errors>'
  # rescue Aws::S3::Errors::BadDigest
  #   raise ActiveStorage::IntegrityError
  end

  def upload_with_multipart(key, io, content_type: nil, content_disposition: nil, custom_metadata: {}, tagging_str: nil)
    part_size = [ io.size.fdiv(MAXIMUM_UPLOAD_PARTS_COUNT).ceil, MINIMUM_UPLOAD_PART_SIZE ].max

    object_for(key).upload_stream(
      content_type: content_type,
      content_disposition: content_disposition,
      part_size: part_size,
      metadata: custom_metadata,
      tagging: tagging_str,
      **upload_options) do |out|
      IO.copy_stream(io, out)
    end
  end

end

It can be configured like so:

amazon:
  service: TaggingS3
...

And used like so:

blob.service.upload(blob.key, file, checksum: blob.checksum, **blob.send(:service_metadata), tagging: {key: value})
0

There are 0 best solutions below