Jon*_* G. 13 amazon-s3 amazon-web-services node.js express
我有一个私人 aws s3 容器,里面装满了我想在 SPA 上使用的图像。在后端我使用express来尝试访问这些图像。这是我在节点上的代码,我从 s3 容器获取对象,但我不知道如何从前端显示图像,有办法做到这一点吗?我是否必须公开我的 s3 容器并使用 URL,我希望避免这种情况。谢谢
let express = require('express')
let router = express.Router();
const aws = require('aws-sdk');
require('dotenv').config();
router.get('/', async (req,res) => {
try{
aws.config.setPromisesDependency(); //use so you can promisify to get the actual images
aws.config.update({
accessKeyId: process.env.ACCESSKEYID,
secretAccessKey: process.env.SECRETACCESSKEY,
region: 'us-east-1'
});
const s3 = new aws.S3();
const list = await s3.listObjectsV2({
Bucket: 'xxx-xxx-xxx'
}).promise()
res.send(list)
} catch(e) {
console.log('our error is',e)
}
})
module.exports = router;
Run Code Online (Sandbox Code Playgroud)
难道我必须更改 Content-Type 吗?因为当我收到此响应时,当我在 chrome 开发人员工具中读取它时,它是“application/json; charset=utf-8”。
您可以将存储桶保持私有,您只需使用getSignedURLPromise()aws-SDK 中的存储桶即可。
var params = {Bucket: 'xxx-xx-xxx', Key: '1.jpg'};
var promise = s3.getSignedUrlPromise('getObject', params);
promise.then(function(url) {
res.send(url)
}, function(err) { console.log(err) });
Run Code Online (Sandbox Code Playgroud)
这实际上是获取可以安全使用的 URL 所需的全部内容,您甚至可以对其可用时间设置时间限制。
我在这里找到了答案。
| 归档时间: |
|
| 查看次数: |
18808 次 |
| 最近记录: |