首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用next在AWS S3上托管静态文件?

如何使用next在AWS S3上托管静态文件?
EN

Stack Overflow用户
提问于 2019-07-22 17:09:11
回答 2查看 3.2K关注 0票数 2

我来自Django背景,静态文件主要存储在S3上,我试图了解它是如何在NodeJS上工作的,因为我想将一个应用程序从Django/React迁移到NodeJS/NextJS/ExpressJS/React。

我不知道如何&在生产环境中存储静态文件(客户端js、css、图像)?我想了解如何上传到s3并管理动态文件,因为这些工作是由用户通过express完成的,但我正在寻找可以在部署时将所有公共文件批量上传到s3的东西(这是使用express的正确方法吗?)

由于我想部署到Heroku,我知道他们有一个不保存这些静态文件的策略,(在Django中,我使用“收藏品”命令将我的所有静态文件批量上传到每个部署中的S3 ),如何&从哪里在这里提供这些文件?

任何建议都会有帮助。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-11-21 09:38:43

next.config.js,在生产中将assetPrefix设置为aws s3链接。

代码语言:javascript
复制
const isDev = process.env.NODE_ENV !== 'production';
const version = require('./package.json').version;

assetPrefix: isDev ? '' : `https://${process.env.AWS_REGION}.amazonaws.com/${process.env.AWS_S3_BUCKET_NAME}/${version}`,

collectstatic.js,在构建后版本上运行。

代码语言:javascript
复制
require('dotenv').config();
const fs = require('fs');
const readDir = require('recursive-readdir');
const path = require('path');
const AWS = require('aws-sdk');
const mime = require('mime-types');
const version = require('./package.json').version;

// You will run this script from your CI/Pipeline after build has completed.
// It will read the content of the build directory and upload to S3 (live assets bucket)
// Every deployment is immutable. Cache will be invalidated every time you deploy.
AWS.config.update({
  region: process.env.AWS_S3_REGION,
  accessKeyId: process.env.AWS_S3_ACCESS_KEY,
  secretAccessKey: process.env.AWS_S3_SECRET_KEY,
  maxRetries: 3
});

// Retrive al the files path in the build directory
const getDirectoryFilesRecursive = (dir, ignores = []) => {
  return new Promise((resolve, reject) => {
    readDir(dir, ignores, (err, files) => (err ? reject(err) : resolve(files)));
  });
};

// The Key will look like this: _next/public/<buildid>/pages/index.js
// the <buildid> is exposed by nextJS and it's unique per deployment.
// See: https://nextjs.org/blog/next-7/#static-cdn-support
const generateFileKey = (fileName, toReplace, replaced) => {
  // I'm interested in only the last part of the file: '/some/path/.next/build-manifest.json',
  const S3objectPath = fileName.split(toReplace)[1];
  return version + replaced + S3objectPath;
};

const s3 = new AWS.S3();

const uploadToS3 = async (fileArray, toReplace, replaced) => {
  try {
    fileArray.map(file => {
      // Configuring parameters for S3 Object
      const S3params = {
        Bucket: process.env.AWS_S3_BUCKET_NAME,
        Body: fs.createReadStream(file),
        Key: generateFileKey(file, toReplace, replaced),
        ACL: 'public-read',
        ContentType: String(mime.lookup(file)),
        ContentEncoding: 'utf-8',
        CacheControl: 'immutable,max-age=31536000,public'
      };

      s3.upload(S3params, function(err, data) {
        if (err) {
          // Set the exit code while letting
          // the process exit gracefully.
          console.error(err);
          process.exitCode = 1;
        } else {
          console.log(`Assets uploaded to S3:`, data.key);
        }
      });
    });
  } catch (error) {
    console.error(error);
  }
};

// Start function
// getDirectoryFilesRecursive(path, ignore);
const start = async function(dict) {
  for (var i = 0; i < dict.length; i++) {
    const files = await getDirectoryFilesRecursive(path.resolve(__dirname, dict[i].filePath), ['.DS_Store', 'BUILD_ID']);
    uploadToS3(files, dict[i].toReplace, dict[i].replaced);
  }
}

// Call start
start([
  {
    filePath: '.next',
    toReplace: '.next/',
    replaced: '/_next/'
  }
]);

运行node collectstatic.js将所有资产上载到S3。

票数 1
EN

Stack Overflow用户

发布于 2019-07-26 11:39:51

Heroku有什么“不保存这些静态文件的策略”?

的确,如果您想将图像上传功能添加到应用程序中,那么像S3这样的解决方案可能会有帮助,因为Heroku使用的是dynos (隔离的Linux进程),它不允许动态写入文件系统。

除了那个用例(“用户应该能够上传文件”)之外,对静态文件使用S3似乎是一个不必要的复杂性。

用于服务静态文件的NodeJS API是:

代码语言:javascript
复制
app.use(express.static(path.join(__dirname, 'build')));

为了在Heroku上实验这个API,我将使用NodeJS的静态文件API来部署这个简单示例NodeJS/Express/React

repo使用npm的react-scripts库捆绑和编译React代码,服务器只提供绑定到动态生成的'/build'文件夹中的文件。

因此,您的服务器代码变得非常简单,如:

代码语言:javascript
复制
const express = require('express');
const http = require('http');
const path = require('path');
let app = express();
app.use(express.static(path.join(__dirname, 'build')));
const port = process.env.PORT || '8080';
app.set('port', port);
const server = http.createServer(app);
server.listen(port);

如果您真的希望S3与Node和Express一起运行,我将结帐:这个StackOverflow线程。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57150740

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档