Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R2图床插件上传的PNG文件,Content-Type是application/octet-stream #118

Open
LamiaLoveless opened this issue Mar 6, 2025 · 7 comments
Labels
bug Something isn't working

Comments

@LamiaLoveless
Copy link

LamiaLoveless commented Mar 6, 2025

你当前使用的Elog版本

0.14.7

Elog配置文件

const r2 = require('@elog/plugin-img-r2')
module.exports = {
  write: {
    platform: 'notion',
    notion: {
      token: process.env.NOTION_TOKEN,
      databaseId: process.env.NOTION_DATABASE_ID,
      filter: {
        property: "status",
        status: {
          equals: "Done",
        },
      },
    },
  },
  deploy: {
    platform: 'local',
    local: {
      outputDir: './src/content/post',
      filename: 'urlname',
      frontMatter: {
        enable: true,
      },
      formatExt: "elog.format.js",
    },
  },
  image: {
    enable: true,
    plugin: r2,
    r2: {
      accessKeyId: process.env.R2_ACCESSKEYID,
      secretAccessKey: process.env.R2_SECRET_ACCESSKEY,
      bucket: process.env.R2_BUCKET,
      endpoint: process.env.R2_ENDPOINT,
      host: process.env.R2_HOST,
      prefixKey: 'elog'
    },
  },
};

发生了什么?

R2图床插件上传的PNG文件,Content-Type是application/octet-stream,而不是有效的值

Elog错误日志

附加信息

No response

@LamiaLoveless LamiaLoveless added the bug Something isn't working label Mar 6, 2025
@LetTTGACO
Copy link
Owner

R2 的 SDK 我用的是官方的 SDK @aws-sdk/client-s3,应该不太会出现这个问题才对。

上传的错误日志有吗?可以贴一下。

我手头暂时没有 R2 的图床进行测试,你可以对照你的日志错误在官方 github issue 中找找答案,如果确认是版本的问题,我这边可以尝试升级一下 官方sdk 的版本

@LetTTGACO
Copy link
Owner

可以看下我图床插件的代码

https://github.com/LetTTGACO/elog/blob/master/plugins/plugin-img-r2/index.js

@LamiaLoveless
Copy link
Author

@aws-sdk/client-s3 不会自动检测Content-Type

@LamiaLoveless
Copy link
Author

LamiaLoveless commented Mar 6, 2025

const mime = require('mime-types'); // 引入 mime-types 库

async uploadImg(imgBuffer, fileName) {
try {
// 根据文件名获取 MIME 类型
const mimeType = mime.lookup(fileName) || 'application/octet-stream';

  const params = {
    Bucket: this.config.bucket,
    Key: this.config.prefixKey + fileName,
    Body: imgBuffer,
    ContentType: mimeType, // 设置正确的 Content-Type
  };
  await this.s3Client.send(new PutObjectCommand(params));
  return `https://${this.config.host}/${this.config.prefixKey + fileName}`;
} catch (err) {
  // eslint-disable-next-line no-console
  console.error('上传出错', err.message);
}

}
}

@LetTTGACO
Copy link
Owner

非常感谢。
我确认了一下文档,好像确实需要手动指定Content-Type,有兴趣提个 PR 吗?欢迎参与开发

@LamiaLoveless
Copy link
Author

呃 ,我是问的deepseek

@LetTTGACO
Copy link
Owner

@elog/plugin-img-r2 更新到 0.1.2-beta.0 版本试一下,没问题的话我就发正式版本了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants