【问题标题】:Why does S3 download the image instead of viewing it?为什么 S3 下载图像而不是查看它?
【发布时间】:2021-05-03 11:10:02
【问题描述】:

我正在使用 AWS S3 上传图像,它确实会上传,但是当我查看上传图像的位置时,它不会在浏览器中显示它,而是下载它。我如何解决它?我已将内容类型设置为“image/jpg”。

          const s3 = new AWS.S3({
            accessKeyId: process.env.AMAZON_ACCESS_KEY!,
            secretAccessKey: process.env.AMAZON_SECRET_KEY!,
          });

          const fileContent = fs.readFileSync(
            path.join(__dirname, "../../", "uploads/" + file.filename)
          );

          const params = {
            Bucket: "sometic",
            Key: file.filename,
            Body: fileContent,
            ContentType: "image/" + path.extname(file.filename),
          };

          const { Location } = await s3.upload(params).promise();

          console.log(Location);

【问题讨论】:

    标签: node.js amazon-web-services amazon-s3


    【解决方案1】:

    我已将内容类型设置为“image/jpg”

    不,你没有。看看documentation of Node's path.extname(string)。它返回扩展名包括点。你必须先去掉那个点。

    所以你的 ContentType 当前读取为image/.jpg,这是无效的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-02
      • 1970-01-01
      • 1970-01-01
      • 2021-06-02
      • 2014-07-07
      • 2011-04-08
      • 1970-01-01
      相关资源
      最近更新 更多