Reactjs App - 从AWS而非服务器文件夹提供图像

问题描述 投票:0回答:1

我有一个反应应用程序,用户可以发布图像。当用户发布图像时,我的SQL表会更新。 imgSRC列使用文件名更新。例如 - mysite_1536290516498.jpg

如何将以下代码修改为来自AWS的服务器映像而不是destFile:${root}/dist/posts/${filename}

我发布图像和更新数据库的服务器代码是:

  const app = require('express').Router(),
  db = require('../../../config/db'),
  Post = require('../../../config/Post'),
  User = require('../../../config/User'),
  root = process.cwd(),
  upload = require('multer')({
    dest: `${root}/dist/temp/`,
  }),
  { ProcessImage, DeleteAllOfFolder } = require('handy-image-processor')

// POST [REQ = DESC, FILTER, LOCATION, TYPE, GROUP, IMAGE(FILE) ]
app.post('/post-it', upload.single('image'), async (req, res) => {
  try {
    let { id } = req.session,
      { desc, filter, location, type, group } = req.body,
      filename = `mysite_${new Date().getTime()}.jpg`,
      obj = {
        srcFile: req.file.path,
        destFile: `${root}/dist/posts/${filename}`,
      },
      insert = {
        user: id,
        description: desc,
        imgSrc: filename,
        filter,
        location,
        type,
        group_id: group,
        post_time: new Date().getTime(),
      }

    await ProcessImage(obj)
    DeleteAllOfFolder(`${root}/dist/temp/`)

    let { insertId } = await db.query('INSERT INTO posts SET ?', insert),
      fullname = await User.getWhat('fullname', id)

    await db.toHashtag(desc, id, insertId)
    await User.mentionUsers(desc, id, insertId, 'post')

    res.json({
      success: true,
      mssg: 'Posted!!',
      post_id: insertId,
      fullname,
      filename,
    })
  } catch (error) {
    db.catchError(error, res)
  }
})
reactjs
1个回答
0
投票

您可以使用您的Http服务器执行此操作,您可以在apache http服务器中访问.htaccess文件或httpd.conf文件。

只需将/ dist / posts /的所有请求重定向到AWS Path即可。

在此之前,您需要确保在AWs S3存储桶中具有相同的目录结构。

因此,当客户请求来自/ dist / posts / image时,它将转到http://aws/dist/posts/

如果你谷歌,这也称为CDN。

© www.soinside.com 2019 - 2024. All rights reserved.