【问题标题】:Why is my image showing status 200 but not displaying image from s3为什么我的图像显示状态 200 但不显示来自 s3 的图像
【发布时间】:2021-08-28 22:19:31
【问题描述】:

This image shows that the image is uploaded, and fetched successfully but its nit displaying to the user, Can someone help

这是写的代码

// Downloads from s3
function getFileStream(key) {
  const downloadParams = {
    Key: key,
    Bucket: 'icon-path-bucket',
  }

  return s3.getObject(downloadParams).createReadStream()
}

exports.getFileStream = getFileStream

这就是我请求图片的方式

const { getFileStream } = require('./s3bucket')
app.get('/images/:key', (req, res) => {
  const key = req.params.key

  const readStream = getFileStream(key)

  readStream.pipe(res)
})

这里可以做什么?

【问题讨论】:

  • 如果您在浏览器中导航到这些图像的 URL,图像会显示吗?
  • 您的代码对我来说运行良好。请再次检查图像。您还可以在浏览器网络选项卡中查看响应。

标签: javascript node.js express amazon-s3 ejs


【解决方案1】:

你好像错过了add the attachement

添加附件的代码是


        app.get("/images/:key", (req, res) => {
            const key = req.params.key

            const readStream = getFileStream(key) 

            res.attachment(key);
            readStream.pipe(res)
        })

【讨论】:

    【解决方案2】:

    为了在 Amazon S3 存储桶中显示专辑的内容,应用程序的 viewAlbum 函数采用专辑名称并为该专辑创建 Amazon S3 密钥。然后该函数调用 AWS.S3 服务对象的 listObjects 方法来获取相册中所有对象(照片)的列表。

    该函数的其余部分获取相册中的对象列表,并生成在网页上显示照片所需的 HTML。

    相关 AWS 文档:Viewing Photos in an Amazon S3 Bucket from a Browser

    【讨论】:

      猜你喜欢
      • 2021-09-07
      • 2018-08-05
      • 2011-06-22
      • 2015-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多