These are the relevant dependencies in my package.json:
"dependencies": {
"@aws-sdk/client-s3": "^3.52.0",
"@aws-sdk/node-http-handler": "^3.52.0",
"@aws-sdk/s3-request-presigner": "^3.52.0",
"axios": "^0.26.0",
"body-parser": "^1.19.1",
"cors": "^2.8.5",
"express": "^4.17.2",
"express-fileupload": "^1.3.1",
"http-proxy-agent": "^5.0.0",
"lodash": "^4.17.21",
"morgan": "^1.10.0",
"node-fetch": "^2.6.7",
"proxy-agent": "^5.0.0"
}
This is my code:
const express = require('express');
const fileUpload = require('express-fileupload');
const cors = require('cors');
const bodyParser = require('body-parser');
const morgan = require('morgan');
const _ = require('lodash');
const path = require('path');
const app = express();
const fs = require('fs')
//s3 relevant imports
const { S3Client, PutObjectCommand } = require("@aws-sdk/client-s3");
const { getSignedUrl } = require("@aws-sdk/s3-request-presigner");
const fetch = require('node-fetch');
const HttpProxyAgent = require('http-proxy-agent');
let s3 = null
app.listen(port, async () => {
console.log(`App is listening on port ${port}. Static ${root}`)
try {
s3 = new S3Client({
region: 'ap-southeast-2',
credentials: {
accessKeyId: accessKey.data.access_key,
secretAccessKey: accessKey.data.secret_key
}
});
} catch (e) {
console.error(e)
}
});
app.get('/uploadS3Get', async (req, res) => {
try {
const presignedS3Url = await getSignedUrl(s3, new PutObjectCommand({
Bucket: 'xyz-unique-bucketname',
Key: 'test/carlos-test.txt',
}) );
const optionsForFetch = {
method: 'PUT',
body: fs.readFileSync('carlos-test.txt'),
agent: new HttpProxyAgent ('http://username:password@proxy.com:8080')
}
const respFromUpload = await fetch(presignedS3Url, optionsForFetch).catch( err => {
console.log("error catch from fetch")
console.log(err);
return null;
});
console.log("completed so this is the respFrom Upload")
console.log(respFromUpload)
console.log("sending the response back now")
res.status(200).send(respFromUpload);
} catch (e) {
console.log("general catch error")
console.log(e)
res.status(500).send(e);
}
})
I get 200 from node-fetch and this is when it gets printed out:
Response {
size: 0,
timeout: 0,
[Symbol(Body internals)]: {
body: PassThrough {
_readableState: [ReadableState],
_events: [Object: null prototype],
_eventsCount: 5,
_maxListeners: undefined,
_writableState: [WritableState],
allowHalfOpen: true,
[Symbol(kCapture)]: false,
[Symbol(kTransformState)]: [Object]
},
disturbed: false,
error: null
},
[Symbol(Response internals)]: {
url: 'https://xyz-unique-bucketname.s3.ap-southeast-2.amazonaws.com/test/carlos-test.txt?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=omitted%2F20220223%2Fap-southeast-2
%2Fs3%2Faws4_request&X-Amz-Date=20220223T022913Z&X-Amz-Expires=900&X-Amz-Signature=sigommitted&X-Amz-SignedHeaders=content-length%3Bhost&x-id=PutObject',
status: 200,
statusText: 'OK',
headers: Headers { [Symbol(map)]: [Object: null prototype] },
counter: 0
}
}
However, even if the node-fetch PUT using the signed url is returning 200, the file itself is not in the bucket.
What is the wrong with my code and why is AWS SDK misleading by saying 200 response but file is missing?