You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS SDK v3 S3上传图片报错:config.endpointDiscoveryEnabled不是函数

AWS SDK v3 S3上传图片报错:TypeError: config.endpointDiscoveryEnabled is not a function

问题根源及修复步骤:

  • 命令混用错误:你用S3Client实例发送Timestream的WriteRecordsCommand,两者完全不匹配,S3上传文件必须用专属的PutObjectCommand(从@aws-sdk/client-s3导入)。
  • 无效配置项:endpointDiscoveryEnabled是Timestream客户端的专属配置,S3Client不支持这个参数,你设为undefined触发了中间件的函数调用错误,直接删除该配置即可。
  • 参数硬编码问题:Key字段写死成"newFileName.png",应该用前面生成的newFileName变量,否则所有上传文件都会被覆盖成同一个名字。

修正后的完整代码:

const { S3Client, PutObjectCommand } = require('@aws-sdk/client-s3');
// 不需要的Timestream相关导入直接删除,避免混淆
// const { TimestreamWriteClient, WriteRecordsCommand,EndpointDiscoveryEnabled } = require("@aws-sdk/client-timestream-write");

const client = new S3Client({
    region: 'eu-central-1',
    accessKeyId: 'access key',
    secretAccessKey: 'secret key',
    // 移除endpointDiscoveryEnabled这个无效配置
});

return new Promise(resolve => {
    try {
        const base64Data = base64Image.replace(/^data:([A-Za-z-+/]+);base64,/, '');
        const type = base64Image.split(';')[0].split('/')[1];

        const newFileName = filename ? `${filename}${thumb === true ? '_thumb' : ''}.${type}` : `${uuidv4()}${thumb === true ? '_thumb' : ''}.${type}`;

        const imageBuffer = Buffer.from(base64Data, 'base64');

        const params = {
            Bucket: "bucket123",
            Key: newFileName, // 用变量替换硬编码的文件名
            ACL: 'public-read',
            Body: imageBuffer,
            ContentEncoding: 'base64',
            ContentType: `image/${type}`, // 补全标准Content-Type格式,避免S3识别错误
        };

        // 替换为S3专属的PutObjectCommand
        const command = new PutObjectCommand(params);

        client.send(command, function (err, data) {
            if (!err) {
                resolve({
                    status: true,
                    message: 'success'
                });
            } else {
                resolve({
                    status: false,
                    message: err.message || err
                });
            }
        });
    } catch (err) {
        resolve({
            status: false,
            message: err.message || err
        });
    }
});

额外提示:

  • 补全ContentType为标准格式(如image/png、image/jpeg),能避免S3对文件类型的识别错误。
  • 若不需要Timestream相关功能,直接删除对应的导入语句,减少不必要的依赖加载。

内容的提问来源于stack exchange,提问作者imrankerimoglu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 00:27:05