简体   繁体   English

nodejs表示服务文件夹和存储在DigitalOcean / S3中的文件

[英]nodejs express serving folder and files stored in DigitalOcean/S3

I would like to ask help on serving files stored via object storage in DigitalOcean/S3` using nodejs. 我想寻求有关使用nodejs通过DigitalOcean / S3`中的对象存储存储文件的服务的帮助。

My current app checks the ./public/ folder inside the nodejs app folder as follows: 我现在的应用程序检查./public/的应用程序的NodeJS文件夹内的文件夹,如下所示:

in app.js 在app.js中

app.use(express.static(path.join(__dirname, 'public')));

and in routes/index.js 并在route / index.js中

var express = require('express');
var router = express.Router();
router.get('/', function(req, res) {
res.render('home', { title: 'homepage' });
});
module.exports = router;

How can I let the app serve files located in the ./public/ directory in a bucket. 如何让应用程序提供存储桶中./public/目录中的文件。

DIR   s3://my***/public/

Your help is greatly appreciated. 非常感谢您的帮助。

I think you're confusing the purpose of S3, it's not like a database when you'll proxy calls to the data (s3) and hand it back to the client. 我认为您混淆了S3的用途,当您代理对数据(s3)的调用并将其交还给客户端时,它不像数据库。 Generally you'll expose the S3 bucket in a way that lets anyone view individual items from it but not index or update it. 通常,您将以某种方式公开S3存储桶,以使任何人都可以从中查看单个项目,但不能索引或更新它。

From there you can just lookup the full file URL and pass it back to the user / browser / app. 您可以从那里查找完整的文件URL,并将其传递回用户/浏览器/应用。 For example, I usually store the S3 location within my database, I'll then pull this out and append it on the end of the S3 bucket URL and then hand that back to the frontend to render my images directly from S3. 例如,我通常将S3位置存储在数据库中,然后将其拉出并附加到S3存储桶URL的末尾,然后将其移回前端以直接从S3渲染图像。

If you want to attach your own domain name to S3, you can checkout AWS Cloudfront and use the CDN feature. 如果要将自己的域名附加到S3,则可以签出AWS Cloudfront并使用CDN功能。 This allows you to attach your own domain name to the bucket and serve things up super quickly from anywhere across the globe. 这样一来,您便可以将自己的域名附加到存储桶中,并在全球范围内的任何地方快速提供服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM