使用 IHP 发布 uploading/accessing S3 上传

Issue uploading/accessing S3 uploads with IHP

我在 IHP 或 IHP 插件中遇到 S3 问题。

我正在将文件保存到 AWS 存储,我得到一个 url 返回以保存在数据库中,但 S3 中没有保存任何内容。并且没有来自 IHP 的错误消息。

我仔细检查了存储桶名称和区域,并且访问密钥已连接到具有 AmazonS3FullAccess 权限的 IAM。

我尝试访问已签名的 urls 时收到 403 禁止。

有什么地方可能出错的想法吗?

目前我的控制器看起来像这样,因为我根据文档指示保存整个 link,使用 createTemporaryDownloadUrlFromPath 将不起作用,因此文档中目前存在一些漏洞。

    action CreateDriverAction = do
        let driver = newRecord @Driver
        driver
            |> buildDriver
            |> validateNewDriver
            >>= uploadToStorage #licenceBackUrl
            >>= pure . validateField #licenceFrontUrl (nonEmpty |> withCustomErrorMessage "Bilde av forsiden på førerkortet er obligatorisk")
            >>= pure . validateField #licenceBackUrl (nonEmpty |> withCustomErrorMessage "Bilde av baksiden på førerkortet er obligatorisk")
            >>= ifValid \case
                Left driver -> do
                    render NewView{..}
                Right driver -> do
                    hashed <- hashPassword (get #passwordHash driver)
                    driver <- driver |> set #passwordHash hashed |> createRecord
                    login driver
                    redirectToPath "/driver/"

访问不允许公开的文件时,需要执行一些额外的步骤,并且

您必须使用 fileOrNothing 访问文件并执行更多手动步骤。

我的操作结束时看起来像这样:

    action CreateDriverAction = do
        let driver = newRecord @Driver
        let licenceFrontFile = fileOrNothing "licenceFrontUrl"
        let licenceBackFile = fileOrNothing "licenceBackUrl"

        driver
            |> buildDriver
            |> set #licenceBackPath (maybe "" (cs . get #fileContent) licenceBackFile)
            |> set #licenceFrontPath (maybe "" (cs . get #fileContent) licenceBackFile)
            |> validateField #licenceFrontPath (nonEmpty |> withCustomErrorMessage "Bilde av forsiden på førerkortet er obligatorisk")
            |> validateField #licenceBackPath (nonEmpty |> withCustomErrorMessage "Bilde av baksiden på førerkortet er obligatorisk")
            |> validateNewDriver
            >>= ifValid \case
                Left driver -> do
                    setErrorMessage "Noe gikk galt med fører-registrering. Sjekk at skjemafeltene er korrekte og send inn på nytt"
                    render NewView{..}
                Right driver ->
                    do
                        storedLicenceFront <-
                            storeFileWithOptions
                                (licenceFrontFile |> fromMaybe (error "No file"))
                                ( def
                                    { directory = "drivers/licenceFront"
                                    , contentDisposition = contentDispositionAttachmentAndFileName
                                    }
                                )

                        storedLicenceBack <-
                            storeFileWithOptions
                                (licenceBackFile |> fromMaybe (error "No file"))
                                ( def
                                    { directory = "drivers/licenceBack"
                                    , contentDisposition = contentDispositionAttachmentAndFileName
                                    }
                                )

                        licenceBackSigned <- createTemporaryDownloadUrlFromPath (get #path storedLicenceBack)
                        licenceFrontSigned <- createTemporaryDownloadUrlFromPath (get #path storedLicenceFront)
                        hashed <- hashPassword (get #passwordHash driver)
                        driver <-
                            driver |> set #passwordHash hashed
                                |> set #licenceFrontPath (get #path storedLicenceFront)
                                |> set #licenceBackPath (get #path storedLicenceBack)
                                |> set #licenceBackUrl (get #url licenceBackSigned)
                                |> set #licenceFrontUrl (get #url licenceFrontSigned)
                                |> set #licenceFrontUrlExpiresAt (get #expiredAt licenceFrontSigned)
                                |> set #licenceBackUrlExpiresAt (get #expiredAt licenceBackSigned)
                                |> createRecord
                        setSuccessMessage "Din førerprofil er opprettet! Vi behandler søknaden din snarest."
                        login driver
                        redirectToPath "/driver/"