从Play目录结构外部直接投放文件 [英] Direct serving files from outside of Play directories structure
问题描述
你好玩家
我需要为小型应用程序创建类似于自定义CDN
的解决方案,并想知道用Play直接提供文件而不放在应用程序的public
目录下的最佳方法是什么?我需要访问FTP上传文件夹.
I need to crate custom CDN
-like solution for small app purposes and wonder what is the best way to serving files directly with Play, but not placed under the public
directory of the application? I need to gain access to FTP-upload folder.
假设我的应用程序是从/home/myaccount/playapps/app201
文件夹运行的,并且可以从http://somedomain.tld
地址获得.我也有一个普通的FTP帐户,其文件夹指向/home/myaccount/ftp_upload
.
Let's say that my app is run from /home/myaccount/playapps/app201
folder and is available at http://somedomain.tld
address. I have also a common FTP account with folder pointing to /home/myaccount/ftp_upload
.
将文件/home/myaccount/ftp_upload/folder_1/sub_2/file.txt
作为
http://somedomain.tld/ftp_upload/folder_1/sub_2/file.txt
(没有任何检查和限制)?
What is the best way to serve file /home/myaccount/ftp_upload/folder_1/sub_2/file.txt
as
http://somedomain.tld/ftp_upload/folder_1/sub_2/file.txt
(without any checks and restrictions)?
- 一种选择是使用HTTP服务器并为
ftp folder
设置单独的host
或alias
,但是我想避免在某些节点上使用其他服务器( ref: 如果您正在寻找服务器解决方案,请检查示例配置 ). - 第二个正在编写
Application.serve(String filepath)
操作+路由,但是在提供文件之前我不需要任何其他操作.使用这种方法有意义吗?
- One option is to use HTTP server and set separate
host
oralias
forftp folder
, but I would like to avoid using additional servers on some nodes (ref: if you're looking for server solution check the sample config). - Second is writing
Application.serve(String filepath)
action + route, but I don't need any additional actions before serving files. Is that make sense to use this approach?
还有其他选择吗?
(此问题也可通过 Google网上论坛)
推荐答案
如果您的Play文件夹和FTP文件夹位于同一主机上,则可以使用公共资产.从文档:
If your Play folder and FTP folder are on the same host, you can use the public assets. From the documentation:
请注意,如果您在公共"之外定义资产映射,则需要告知 关于它的sbt如果您愿意:
Note, if you define asset mappings outside "public," you’ll need to tell sbt about it, e.g. if you want:
GET /assets/*file controllers.Assets.at("/public", file)
GET /liabilities/*file controllers.Assets.at("/foo", file)
您应该将此添加到project/Build.scala
// Add your own project settings here
playAssetsDirectories <+= baseDirectory / "foo"
我没有测试它,但是值得一试:-)
I did not test it, but it's worth a try :-)
这篇关于从Play目录结构外部直接投放文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!