Attaching an already uploaded file to shire

I am writing large CSV files directly to S3 in a background job (sidekiq). I am wondering how I can attach that file to the :file attribute after the file has been successfully written?

Method 1: Attach directly to Shrine in your background job

Something like this would be the simplest way:

class UploadCSVFileWorker
  include Sidekiq::Worker
  sidekiq_options retry: 5, queue: 'default'

  def perform(conversation_id)
    largeCSVFile = createCSVFile()
    document = largeCSVFile)

Shrine will take care of extracting the metadata associated.

Method 2: Update the URL directly

But I suspect you might be wanting to update the URL directly. I have some vague ideas, but I could be completely wrong on this because i’m not a shrine expert. You might want to manually create the associated meta data associated with your csv file and manually insert it all in your file_data column.

You can use Shrine.uploaded_file to create a Shrine::UploadedFile from the S3 object key, and assign it via Attacher#change, e.g:

# `object_key` is where you've uploaded the CSV
uploaded_file = ImageUploader.uploaded_file(id: object_key, storage: :store) 


That way you’re assigning an already uploaded file, so Shrine doesn’t do any additional work other than populating the <attachment>_data column.

1 Like