'How to open an S3 pre-signed URL in another tab?
How do I open an S3 file in another tab and not download it? I have this feature that generates a pre-signed URL. The front-end then opens this URL. However, what I would like to happen is to open the file in another tab so that the user can see the file before downloading it.
Backend
static async downloadFile(req, res, next) {
s3.getSignedUrl('getObject', {
Bucket: process.env.BUCKET_NAME,
Key: req.body.fileName
}, (error, url) => {
if (error) {
res.status(403).json({
err
})
} else {
res.status(200).json({
url
})
}
})
}
Frontend
User.downloadFile(fileName) // HTTP request was made in another file using axios
.then((res) => {
window.open(res.data.url)
})
Any advice is highly appreciated.
Solution 1:[1]
You need to set contentType of the image to the correct one while uploading the image to s3. like, set contentType to image/png
if your image has .png
format. If the contentType is not correct or not set then the image will be directly downloaded instead of displaying it in the browser. This should solve your problem.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | ShuLaPy |