无法成功上传图片到s3然后查看
Unable to succesfully upload images to s3 and then view them
我正在尝试将图像上传到 s3 存储桶作为应用程序的一部分。
index.js
function upImg(req) {
if(req.files.img) {
var img = req.files.image;
var name = Math.round(Math.random()*10000).toString(); // Returns a random 5 digit number
if(myDB.uploadImg(img, name)) {
return name;
} else {
return "";
}
} else {
return "";
}
}
app.post('/newEV*', isLoggedIn, function(req, res) {
var myURL = req.path.replace('/newEV', '');
var imgPath = upImg(req);
fetch(myURL).then(function (events){
var myID;
var x = 0;
while(!myID) {
if(!events[x]) {
myID = x;
} else {
x++;
}
}
myDB.newEvent(myURL, req.body.name, req.body.desc, req.body.loc, imgPath, req.body.link, req.body.cap, req.body.date, req.body.time, myID, events);
res.redirect('/edit' + myURL);
});
});
myDB 文件
function signs3(file, name) {
devs3();
const s3 = new aws.S3();
const s3Params = {
Body: file,
Bucket: S3_BUCKET,
Key: name
};
s3.putObject(s3Params, function(err, data) {
if(err) {
throw err;
} else {
console.log("Data from putObject:" + JSON.stringify(data));
}
});
}
module.exports = {
uploadImg : function(file, name) {
var nName = "imgs/" + name;
console.log(nName);
signs3(file, nName);
return true;
}
}
我知道 signs3 函数有效,因为我可以在应用程序的其他部分使用它来上传 JSON 文件。每当我从 post 到 URL 时,奇怪的是我可以在控制台中看到 'data from putObject',但是我看不到的是 nName。我不明白这一点,因为 console.log(nName)
行应该是 运行 在另一行之前。当我去查看存储桶时,图像还没有上传(尽管我从控制台得到了一个 ETag),并且页面没有显示它(我知道这也有效,因为它可以显示已经上传到存储桶的图像).
您想做这样的事情,从 Request object created when you call putObject.
请求事件
const req = s3.putObject( s3Params )
req.on('success', res => {
console.log ('upload complete! );
});
req.on ('error', res => {
console.error (res.error');
});
req.send();
为什么这对小文件(JSON 文件)和大文件(图像)的处理方式不同?因为大文件上传时间比较长。
我正在尝试将图像上传到 s3 存储桶作为应用程序的一部分。
index.js
function upImg(req) {
if(req.files.img) {
var img = req.files.image;
var name = Math.round(Math.random()*10000).toString(); // Returns a random 5 digit number
if(myDB.uploadImg(img, name)) {
return name;
} else {
return "";
}
} else {
return "";
}
}
app.post('/newEV*', isLoggedIn, function(req, res) {
var myURL = req.path.replace('/newEV', '');
var imgPath = upImg(req);
fetch(myURL).then(function (events){
var myID;
var x = 0;
while(!myID) {
if(!events[x]) {
myID = x;
} else {
x++;
}
}
myDB.newEvent(myURL, req.body.name, req.body.desc, req.body.loc, imgPath, req.body.link, req.body.cap, req.body.date, req.body.time, myID, events);
res.redirect('/edit' + myURL);
});
});
myDB 文件
function signs3(file, name) {
devs3();
const s3 = new aws.S3();
const s3Params = {
Body: file,
Bucket: S3_BUCKET,
Key: name
};
s3.putObject(s3Params, function(err, data) {
if(err) {
throw err;
} else {
console.log("Data from putObject:" + JSON.stringify(data));
}
});
}
module.exports = {
uploadImg : function(file, name) {
var nName = "imgs/" + name;
console.log(nName);
signs3(file, nName);
return true;
}
}
我知道 signs3 函数有效,因为我可以在应用程序的其他部分使用它来上传 JSON 文件。每当我从 post 到 URL 时,奇怪的是我可以在控制台中看到 'data from putObject',但是我看不到的是 nName。我不明白这一点,因为 console.log(nName)
行应该是 运行 在另一行之前。当我去查看存储桶时,图像还没有上传(尽管我从控制台得到了一个 ETag),并且页面没有显示它(我知道这也有效,因为它可以显示已经上传到存储桶的图像).
您想做这样的事情,从 Request object created when you call putObject.
请求事件const req = s3.putObject( s3Params )
req.on('success', res => {
console.log ('upload complete! );
});
req.on ('error', res => {
console.error (res.error');
});
req.send();
为什么这对小文件(JSON 文件)和大文件(图像)的处理方式不同?因为大文件上传时间比较长。