从 Sinatra 应用程序访问存储在服务器上的文件

发布于 2024-12-21 00:07:41 字数 385 浏览 2 评论 0原文

我有一个机架空间云服务器,我在其上托管 Sinatra 应用程序。我的应用程序中有一个公共目录,我在其中存储当前的样式、脚本等。我有其他文件,但我不想在每次部署时更新和传输。我正在使用 git 和 Vlad 进行部署,为了避免这种情况,我当前的流程是

  • 进行本地更改和更新,
  • 更新我的本地存储库
  • 推送到 git
  • run vlad:update
  • ,然后手动 ssh 到我的服务器并 mv mp3 和我想要在项目中的其他内容到当前版本的公共文件夹

简而言之,我希望能够访问我的用户文件夹中提供的文件,或者理想情况下还可以选择访问具有权限的服务器文件上的其他用户,这样我就不需要总是将它们移动到当前用手释放。

谢谢

I have a rackspace cloud server that I am hosting a Sinatra app on. I have a public directory in my app where I store my current styles, scripts, etc. I have additional files though that i dont want to be updating and transferring in with every deployment. I am using git and Vlad to deploy so to avoid this my current process is

  • make local changes and updates
  • update my local repo
  • push to git
  • run vlad:update
  • then manually ssh into my server and mv the mp3s and other stuff I want in the project to the current releases' public folder

In short, I would like to be able to access files served in my user folder, or ideally also the option to access another user on the servers files with permissions, so that I don't need to always move them to the current release by hand.

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡看悲欢离合 2024-12-28 00:07:41

假设您想直接从 Sinatra 提供静态文件(即您没有设置 Apache 或 Nginx 来提供它们),您可能可以使用 Rack::Static。仅当静态文件全部由其他位置的子目录提供时,这才有效。例如,如果您在 /home/other_user/files/mp3s 中存储了一堆 mp3,并且您像 http://example.com/mp3s/foo.mp3 一样访问它们 code> 那么你可以像这样使用中间件

use Rack::Static, :urls => ['/mp3s'], :root => '/home/other_user/files/mp3s'

如果你还有其他子目录如果您想从“/images”等处提供文件,只需将它们添加到 :urls 数组中即可: :urls => ['/mp3s', '/images']

如果您希望能够访问 URL 'base' 处的文件,例如 http://example.com/foo.mp3,则使用 Rack::Static 不起作用。在这种情况下,我们需要针对目录检查每个请求,如果存在匹配的文件,则提供服务,否则继续处理请求。 (Sinatra 本身在检查其 public 目录时必须执行类似的操作)。

实现此目的的最简单方法可能是使用 来自rack-contrib的Rack::TryStatic

require 'rack/contrib' #install rack-contrib first
use Rack::TryStatic, :urls => ['/'], :root => '/home/other_user/files/mp3s'

如果您使用其中任何一个,您可能需要确保只在正确的环境中配置中间件,否则您将出现错误查找不存在的目录时的开发。

Assuming you want to serve the static files directly from Sinatra (i.e. you don't have Apache or Nginx set up to serve them) you might be able to use Rack::Static. This will only work if the static files are all served from subdirectories of the other location. For example, if you have a bunch of mp3s stored in /home/other_user/files/mp3s and you access them like http://example.com/mp3s/foo.mp3 then you could use the middleware like this:

use Rack::Static, :urls => ['/mp3s'], :root => '/home/other_user/files/mp3s'

If you have other subdirectories you want to serve files from, e.g. '/images', simply add them to the :urls array: :urls => ['/mp3s', '/images'].

If you want to be able to access the files at the 'base' of the url, like http://example.com/foo.mp3, then Rack::Static wont work. In this case we need to check each request against the directory, and if a matching file exists serve it otherwise continue on with the request. (Sinatra itself has to do something like this when checking its public directory).

The easiest way to achieve this is probably to use Rack::TryStatic from rack-contrib:

require 'rack/contrib' #install rack-contrib first
use Rack::TryStatic, :urls => ['/'], :root => '/home/other_user/files/mp3s'

If you use either of these, you'll likely need to make sure you only configure the middleware in the right environment, otherwise you'll get errors in development when non-existent directories are looked for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文