shrine.cr icon indicating copy to clipboard operation
shrine.cr copied to clipboard

Error on Heroku uploading to an s3

Open usern3 opened this issue 5 years ago • 0 comments

I'm having rouble uploading on Heroku. heres part of the log, the config, and part of the form.

2021-03-21T02:32:10.850984+00:00 app[web.1]: {"severity":"Error","source":"lucky","timestamp":"2021-03-21T02:32:10+00:00","error":{"class":"Shrine::Error","message":"storage "store" isn't registered on Shrine","backtrace":["/tmp/build_3ef14957/lib/shrine/src/shrine.cr:31:3 in 'find_storage'","/tmp/build_3ef14957/lib/shrine/src/shrine.cr:241:7 in 'upload:metadata'","/tmp/build_3ef14957/lib/shrine/src/shrine.cr:173:7 in 'call'","/tmp/build_3ef14957/lib/lucky/src/lucky/renderable.cr:111:16 in 'perform_action'","/tmp/build_3ef14957/lib/lucky/src/lucky/route_handler.cr:10:7 in 'call'","/tmp/build_3ef14957/.heroku/crystal/share/crystal/src/http/server/handler.cr:28:7 in 'call'","/tmp/build_3ef14957/.heroku/crystal/share/crystal/src/http/server/handler.cr:28:7 in 'call'","/tmp/build_3ef14957/.heroku/crystal/share/crystal/src/http/server/handler.cr:28:7 in 'call'","/tmp/build_3ef14957/.heroku/crystal/share/crystal/src/http/server/handler.cr:28:7 in 'call'","/tmp/build_3ef14957/.heroku/crystal/share/crystal/src/http/server/handler.cr:28:7 in 'call_next'","/tmp/build_3ef14957/lib/lucky/src/lucky/force_ssl_handler.cr:36:8 in 'call'","/tmp/build_3ef14957/.heroku/crystal/share/crystal/src/http/server/request_processor.cr:50:11 in 'process'","/tmp/build_3ef14957/.heroku/crystal/share/crystal/src/http/server.cr:498:5 in '->'","/tmp/build_3ef14957/.heroku/crystal/share/crystal/src/primitives.cr:255:3 in 'run'","???"]}} 2021-03-21T02:32:10.850987+00:00 app[web.1]: {"severity":"Info","source":"lucky","timestamp":"2021-03-21T02:32:10+00:00","local":{"status":500,"duration":"195.49ms"}} 2021-03-21T02:32:10.848941+00:00 heroku[router]: at=info method=POST path="/users/1/edit" host=askcr.io request_id=b971bb4c-32dc-4cc3-8dd5-f6a7ae8da0db fwd="185.99.3.119" dyno=web.1 connect=0ms service=1228ms status=500 bytes=1817 protocol=http

config/shrine.cr

Shrine.configure do |config|
  if Lucky::Env.test?
    config.storages[ShrineStorage::UPLOADS] = Shrine::Storage::Memory.new
  elsif Lucky::Env.development?
    config.storages[ShrineStorage::UPLOADS] = Shrine::Storage::FileSystem.new("tmp", prefix: "assets/uploads")
  else
    client = Awscr::S3::Client.new(ENV["BUCKETEER_AWS_REGION"], ENV["BUCKETEER_AWS_ACCESS_KEY_ID"], ENV["BUCKETEER_AWS_SECRET_ACCESS_KEY"])
    config.storages[ShrineStorage::UPLOADS] = Shrine::Storage::S3.new(bucket: ENV["BUCKETEER_BUCKET_NAME"] , client: client, prefix: "public/upload", public: false)
  end
end

# Defines the `Shrine` storage locations for different data types.
module ShrineStorage
  UPLOADS = "uploads"
end

# TRICKY: we patch this because it's not providing the correct download url
class Shrine
  module Storage
    class S3 < Storage::Base
      def url(id : String, **options) : String
        endpoint : String?
        if ep = csrhine]Zent.@endpoint
          endpoint = ep.gsub("https://", "")
        end
        presigned_options = Awscr::S3::Presigned::Url::Options.new(
          aws_access_key: client.@aws_access_key,
          aws_secret_key: client.@aws_secret_key,
          region: client.@region,
          object: UUID.new.variant.to_s,
          bucket: bucket,
          host_name: endpoint
        )

        url = Awscr::S3::Presigned::Url.new(presigned_options)
        url.for(:get)
      end
    end
  end
end

form code:

  #Shortened for brevity.
  mount ::Shared::Field, op.profile_picture, label_text: "Avatar", &.file_input

usern3 avatar Mar 21 '21 02:03 usern3