Javascript 从Amazon S3存储桶加载图像时出现问题(不使用URL)
我一直在遵循本网站和froom在线教程上各种用户的建议,但当我尝试从S3 Amazon Bucket加载图像时,本地托管屏幕仅显示“[Object]”而不是图像。帐户的凭据是正确的,并且我能够在仅加载图像本身的html时显示bucket中的图像。然而,我使用的是一个哈巴狗模板,所以我可能遗漏了一些重要的东西来解释增加的复杂性Javascript 从Amazon S3存储桶加载图像时出现问题(不使用URL),javascript,node.js,amazon-web-services,amazon-s3,pug,Javascript,Node.js,Amazon Web Services,Amazon S3,Pug,我一直在遵循本网站和froom在线教程上各种用户的建议,但当我尝试从S3 Amazon Bucket加载图像时,本地托管屏幕仅显示“[Object]”而不是图像。帐户的凭据是正确的,并且我能够在仅加载图像本身的html时显示bucket中的图像。然而,我使用的是一个哈巴狗模板,所以我可能遗漏了一些重要的东西来解释增加的复杂性 router.get('/', function (req, res, next) { AWS.config.update({ accessKeyId: "{{ke
router.get('/', function (req, res, next) {
AWS.config.update({
accessKeyId: "{{key}}",
secretAccessKey: "{{key}}",
defaultRegionName: 'eu-west-2'
});
let s3 = new AWS.S3();
this.image = s3.getObject({
Bucket: 'foodproducts',
Key: 'SainsShroomdogs.jpg'
}, function (errtxt, file) {
if (errtxt) {
console.Log("lireFic", "ERR " + errtxt);
} else {
console.log('lecture OK')
this.image.src = "data:image/png;base64," + encode(file.Body);
}
});
function encode(data) {
var str = data.reduce(function (a, b) {
return a + String.fromCharCode(b)
}, '');
return btoa(str).replace(/.{76}(?=.)/g, '$&\n');
}
res.render('index', {
productName: this.name,
productDescription: this.description,
imageTest: this.image
});
}));
以下是.pug文件代码:
doctype html
head
meta(charset='UTF-8')
title Internet Food Database
style
include ./SearchPageStylesheet.css
.container
.top
p Top
.middle
.left
p Left
.centre
.productImage
.productTitleText
h1= productName
p= productDescription
imageTest= imageTest
.right
p Right
.bottom
p Bottom
如何从私有S3 amazon bucket中检索图像,然后将其存储在this.image中,准备在本地托管的html页面上显示?imageTest对象的内容是什么?您是否尝试将其记录到控制台?您需要的路径或数据可能是正在返回的对象的属性。我已经记录了它,它似乎是以对象形式向amazon发出的请求。请求{正文:“”,终结点:终结点{协议:“https:”,主机:“foodproducts.s3.amazonaws.com”,端口:443,主机名:“foodproducts.s3.amazonaws.com”,路径名:“/”,路径:“/”,_头:“GET/SainsShroomdogs.jpg HTTP/1.1\r\n用户代理:aws sdk nodejs/2.677.0 darwin/v10.16.2回调\r\n内容长度:0\r\n主机:foodproducts.s3.amazonaws.com\r\nX-Amz-Content-Sha256:…这是否需要编码才能显示在页面上?我曾尝试使用上面显示的“encode”函数(如教程中所用),但“data.reduce”或“btoa”被识别为一个函数,因此无法执行将
imageTest=imageTest
替换为img(src=imageTest.src)
错误:18 | h1=productName 19 | p=productDescription>20 | imageTest=img(src=imageTest.src)img不是一个函数,imageTest
对象的内容是什么?您是否尝试将其记录到控制台?您需要的路径或数据可能是正在返回的对象的属性。我已经记录了它,它似乎是以对象形式向amazon发出的请求。请求{body:'',端点:endpoint{协议:“https:”,主机:“foodproducts.s3.amazonaws.com”,端口:443,主机名:“foodproducts.s3.amazonaws.com”,路径名:“/”,路径:“/”,_头:“GET/SainsShroomdogs.jpg HTTP/1.1\r\n用户代理:aws sdk nodejs/2.677.0 darwin/v10.16.2回调\r\n内容长度:0\r\n主机:foodproducts.s3.amazonaws.com\r\nX-Amz-Content-Sha256:…这是否需要编码才能显示在页面上?我曾尝试使用上面显示的“encode”函数(如教程中所用),但“data.reduce”或“btoa”被识别为一个函数,因此无法执行将imageTest=imageTest
替换为img(src=imageTest.src)
错误:18 | h1=productName 19 | p=productDescription>20 | imageTest=img(src=imageTest.src)img不是函数