获取 SignatureDoesNotMatch:使用 multers3 node.js 将图像上传到 DigitalOcean Spaces 时出现 null 错误

问题描述 投票:0回答:3
Error: code:'SignatureDoesNotMatch'
extendedRequestId:undefined
message:null
name:'SignatureDoesNotMatch'
region:null
// Load dependencies
const aws = require('aws-sdk');
const express = require('express');
const multer = require('multer');
const multerS3 = require('multer-s3');

const app = express();

// Set S3 endpoint to DigitalOcean Spaces
const spacesEndpoint = new aws.Endpoint('fra1.digitaloceanspaces.com');
const s3 = new aws.S3({
    endpoint: spacesEndpoint,
    accessKeyId: '*************',
    secretAccessKey: '**********************************'
});

// Change bucket property to your Space name
const upload = multer({
    storage: multerS3({
        s3: s3,
        bucket: 'bucket',
        acl: 'public-read',
        key: function (request, file, cb) {
            console.log(file);
            cb(null, file.originalname);
        }
    })
}).array('upload', 1);

// Views in public directory
app.use(express.static('public'));

// Main, error and success views
app.get('/', function (request, response) {
    response.sendFile(__dirname + '/public/index.html');
});

app.get("/success", function (request, response) {
    response.sendFile(__dirname + '/public/success.html');
});

app.get("/error", function (request, response) {
    response.sendFile(__dirname + '/public/error.html');
});

app.post('/upload', function (request, response, next) {
    upload(request, response, function (error) {
        if (error) {
            console.log(error);
            return response.redirect("/error");
        }
        console.log('File uploaded successfully.');
        response.redirect("/success");
    });
});

app.listen(3001, function () {
    console.log('Server listening on port 3001.');
});

尝试使用此博客获取数字海洋空间示例

node.js digital-ocean multer spaces multer-s3
3个回答
5
投票

我遇到了同样的问题,唯一有帮助的是重置按键


2
投票

我可以确认 Livius 的回答。我遇到过同样的问题。在不对代码进行任何更改的情况下,我创建了新的访问密钥并且它开始工作。供您参考,这里截取了我的设置代码:

// --------------------------------------------------------
....
var aws = require('aws-sdk');

// --------- AWS SETUP -------------

aws.config.update({
  accessKeyId: '..........',
  secretAccessKey: '.......',
  region: "eu-central-1",
  signatureVersion: 'v4',
  signatureCache: false
});

let s3 = new aws.S3();

.. and so on...

// --------------------------------------------------------

0
投票

4个小时的尝试...我不小心交换了“桶”和“钥匙”的含义。 Bucket是不带文件夹结构的存储部分的简单名称,KEY是包含文件夹结构的文件名。

此错误意味着“未找到存储桶”。

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