404 error when saving to S3?
Viper512 opened this issue · 2 comments
Viper512 commented
I'm getting a 404 error when I run the following code.
The S3 bucket is new and has no special configuration, clicked through created the bucket.
Any Help would be appreciated!
Thanks.
Code :
const AWS = require("aws-sdk");
AWS.config.region = process.env.region;
AWS.config.update({
region: "us-east-1",
accessKeyId: "....",
secretAccessKey: "....",
});
const s3 = new AWS.S3();
let upload = multer({
storage: multerS3({
s3: s3,
bucket: "mytestbucket",
acl: "public-read",
metadata: function (req, file, callback) {
console.log("file", file);
callback(null, { fieldName: file.fieldname });
},
key: function (req, file, callback) {
callback(null, Date.now().toString());
},
}),
});
app.post(
"/api/upload",
upload.array("files"),
function (error, req, res, next) {
if (error) {
console.log(error);
return res.status(422).json({ ...error });
}
return res.json({ text: `Successfully uploaded files!` });
}
);
Error :
404:
at Request.extractError (....\api\node_modules\aws-sdk\lib\services\s3.js:712:35)
at Request.callListeners (....\api\node_modules\aws-sdk\lib\sequential_executor.js:106:20)
at Request.emit (....\api\node_modules\aws-sdk\lib\sequential_executor.js:78:10)
at Request.emit (....\api\node_modules\aws-sdk\lib\request.js:688:14)
at Request.transition (....\api\node_modules\aws-sdk\lib\request.js:22:10)
at AcceptorStateMachine.runTo (....\api\node_modules\aws-sdk\lib\state_machine.js:14:12)
at ....\api\node_modules\aws-sdk\lib\state_machine.js:26:10
at Request.<anonymous> (....\api\node_modules\aws-sdk\lib\request.js:38:9)
at Request.<anonymous> (....\api\node_modules\aws-sdk\lib\request.js:690:12)
at Request.callListeners (....\api\node_modules\aws-sdk\lib\sequential_executor.js:116:18)
at Request.emit (....\api\node_modules\aws-sdk\lib\sequential_executor.js:78:10)
at Request.emit (....\api\node_modules\aws-sdk\lib\request.js:688:14)
at Request.transition (....\api\node_modules\aws-sdk\lib\request.js:22:10)
at AcceptorStateMachine.runTo (....\api\node_modules\aws-sdk\lib\state_machine.js:14:12)
at ....\api\node_modules\aws-sdk\lib\state_machine.js:26:10
at Request.<anonymous> (....\api\node_modules\aws-sdk\lib\request.js:38:9)
at Request.<anonymous> (....\api\node_modules\aws-sdk\lib\request.js:690:12)
at Request.callListeners (....\api\node_modules\aws-sdk\lib\sequential_executor.js:116:18)
at callNextListener (....\api\node_modules\aws-sdk\lib\sequential_executor.js:96:12)
at IncomingMessage.onEnd (....\api\node_modules\aws-sdk\lib\event_listeners.js:313:13)
at IncomingMessage.emit (events.js:327:22)
at IncomingMessage.EventEmitter.emit (domain.js:467:12)
at endReadableNT (internal/streams/readable.js:1327:12)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
message: null,
code: 404,
region: null,
time: 2021-05-05T14:45:46.472Z,
requestId: 'SNII9QGJUEB7168MVOMF5J8SOVVV4KQNSO5AEMVJF66Q9ASUAAJG',
extendedRequestId: undefined,
cfId: undefined,
statusCode: 404,
retryable: false,
retryDelay: 78.56223248138232,
storageErrors: []
}
Viper512 commented
It's trying to hit aws-sdk is being used to hit https://dynamodb.ca-central-1.amazonaws.com, not s3.
Viper512 commented
I came across the problem.
I had
AWS.config.update(
{
region: process.env.region,
endpoint: process.env.apiUrl // was set to https://dynamodb.ca-central-1.amazonaws.com
});
multer-s3 uses the endpoint in the global config, but I was using for dynamodb config, initially.
I moved dynamodb config to the document client :
var docClient = new AWS.DynamoDB.DocumentClient({
region: process.env.region,
endpoint: process.env.apiUrl});
then added config to S3 client :
const s3 = new AWS.S3({
region: "ca-central-1",
accessKeyId: "....",
secretAccessKey: "...."
});