Active Storage

そういえば確認しているのは 5.1.0.beta2です。

(byebug) @user.documents.first.blob.service_url_for_direct_upload
  CACHE ActiveStorage::Attachment Load (0.0ms)  SELECT  "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = ? AND "active_storage_attachments"."record_type" = ? AND "active_storage_attachments"."name" = ? ORDER BY "active_storage_attachments"."id" ASC LIMIT ?  [["record_id", 1], ["record_type", "User"], ["name", "documents"], ["LIMIT", 1]]
  CACHE ActiveStorage::Blob Load (0.0ms)  SELECT  "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = ? LIMIT ?  [["id", 3], ["LIMIT", 1]]
  Disk Storage (0.5ms) Generated URL for file at key: JmoQ7kirgRS4KEufb3Ka9Dhj (/rails/active_storage/disk/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdDVG9JYTJWNVNTSWRTbTF2VVRkcmFYSm5VbE0wUzBWMVptSXpTMkU1UkdocUJqb0dSVlE2RVdOdmJuUmxiblJmZEhsd1pVa2lEMmx0WVdkbEwycHdaV2NHT3daVU9oTmpiMjUwWlc1MFgyeGxibWQwYUdrRDB1TVJPZzFqYUdWamEzTjFiVWtpSFhwSGQwUTRaeTlhVEU5Nk1UaDVXa3RMS3l0bmFsRTlQUVk3QmxRPSIsImV4cCI6IjIwMTgtMDEtMTRUMDc6NTY6MzcuNzU0WiIsInB1ciI6ImJsb2JfdG9rZW4ifX0=--e56d7720ff34358d472be5638f9d910af9ca165c)
"/rails/active_storage/disk/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdDVG9JYTJWNVNTSWRTbTF2VVRkcmFYSm5VbE0wUzBWMVptSXpTMkU1UkdocUJqb0dSVlE2RVdOdmJuUmxiblJmZEhsd1pVa2lEMmx0WVdkbEwycHdaV2NHT3daVU9oTmpiMjUwWlc1MFgyeGxibWQwYUdrRDB1TVJPZzFqYUdWamEzTjFiVWtpSFhwSGQwUTRaeTlhVEU5Nk1UaDVXa3RMS3l0bmFsRTlQUVk3QmxRPSIsImV4cCI6IjIwMTgtMDEtMTRUMDc6NTY6MzcuNzU0WiIsInB1ciI6ImJsb2JfdG9rZW4ifX0=--e56d7720ff34358d472be5638f9d910af9ca165c"
(byebug) @user.documents.first.blob.service_headers_for_direct_upload
  CACHE ActiveStorage::Attachment Load (0.0ms)  SELECT  "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = ? AND "active_storage_attachments"."record_type" = ? AND "active_storage_attachments"."name" = ? ORDER BY "active_storage_attachments"."id" ASC LIMIT ?  [["record_id", 1], ["record_type", "User"], ["name", "documents"], ["LIMIT", 1]]
  CACHE ActiveStorage::Blob Load (0.0ms)  SELECT  "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = ? LIMIT ?  [["id", 3], ["LIMIT", 1]]
{"Content-Type"=>"image/jpeg"}

  • direct uploadでblobを作成するPOSTでurlやheader, signed_idを返している
    • なぜレコード登録とサーバーへのファイルの配置をリクエスト自体分けたのかな...
  • blobを作った後は実際のfileをサーバに配置する。この時にurlやheaderを利用する
  • signed_idは最後に実際のformがsubmitされるの時のパラメータとして利用される