如何在生产环境中提供新的 public 资产?

How to serve new public assets in production?

我的 Play 应用程序在运行时生成了一些文件,我希望能够将这些文件作为 public 资产进行访问。

我试图将它们放在 public 文件夹中,该文件夹由

提供
GET    /assets/*file    controllers.Assets.at(path="/public", file)

但我无法获取这些文件。服务器似乎只能提供构建期间打包的文件。

我还尝试按照另一个 post 中的建议在 build.sbt 中添加以下行:

import com.typesafe.sbt.packager.MappingsHelper._
    mappings in Universal ++= directory(baseDirectory.value / "public")

有了这个我可以看到 public 文件夹中的所有打包资产,但服务器仍然不提供运行时添加的新文件。

有什么想法吗?

Playframework 将 public 文件夹打包到 jar 文件中,并在提供资产时使用它。所以你不能在运行时在这里添加东西。

https://www.playframework.com/documentation/2.6.x/AssetsOverview#How-are-public-assets-packaged?

during the build process, the contents of the public folder are processed and added to the application classpath.

When you package your application, all assets for the application, including all sub projects, are aggregated into a single jar, in target/my-first-app-1.0.0-assets.jar. This jar is included in the distribution so that your Play application can serve them. This jar can also be used to deploy the assets to a CDN or reverse proxy.

您需要创建文件上传器(您已经创建了)和“文件控制器”又名“资产控制器”。像

def file(name: String) = Action {
  Ok.sendFile(new java.io.File("/uploaded/" + name))
}

更多信息:https://www.playframework.com/documentation/2.6.x/ScalaStream#Serving-files