为什么在此片段中未调用s3.pupobject
当ran不执行上传时,此节点脚本,而只是直接进入抛出的错误。有什么想法吗?
const AWS = require('aws-sdk')
const fs = require('fs')
const s3 = new AWS.S3()
const filePathName="/tmp/readme.txt"
var numberOfErrors=1
fs.readFile(filePathName, (err,data) => {
const fileDate = new Date(Date.now()).toISOString()
const bucket = 'test-bucket';
console.log('Preparing file...')
var fileData = new Buffer.from(data);
const params = {
Bucket: bucket,
Key: `readme.txt`,
ContentType: 'text/html',
Body: fileData
}
if (numberOfErrors > 0) {
s3.putObject(params, (err) => {
if (err) { throw err; }
console.log("File uploaded to S3 " + bucket)
})
throw new Error('There were errors')
} else {
s3.putObject(params, (err) => {
if (err) { throw err; }
console.log("File uploaded to S3 " + bucket)
})
console.log('There were no errors')
}
})
输出如下
Preparing file...
sample_test.js:28
throw new Error('There were errors')
^
Error: There were errors
at sample_test.js:28:15
at FSReqCallback.readFileAfterClose [as oncomplete] (node:internal/fs/read_file_context:68:3)
Node.js v17.4.0
如果我评论丢弃错误的行,它将成功上传。任何帮助都非常感谢。
This node script when ran does not execute the upload, but simply goes straight to the thrown error. Any ideas why?
const AWS = require('aws-sdk')
const fs = require('fs')
const s3 = new AWS.S3()
const filePathName="/tmp/readme.txt"
var numberOfErrors=1
fs.readFile(filePathName, (err,data) => {
const fileDate = new Date(Date.now()).toISOString()
const bucket = 'test-bucket';
console.log('Preparing file...')
var fileData = new Buffer.from(data);
const params = {
Bucket: bucket,
Key: `readme.txt`,
ContentType: 'text/html',
Body: fileData
}
if (numberOfErrors > 0) {
s3.putObject(params, (err) => {
if (err) { throw err; }
console.log("File uploaded to S3 " + bucket)
})
throw new Error('There were errors')
} else {
s3.putObject(params, (err) => {
if (err) { throw err; }
console.log("File uploaded to S3 " + bucket)
})
console.log('There were no errors')
}
})
Output is as follows
Preparing file...
sample_test.js:28
throw new Error('There were errors')
^
Error: There were errors
at sample_test.js:28:15
at FSReqCallback.readFileAfterClose [as oncomplete] (node:internal/fs/read_file_context:68:3)
Node.js v17.4.0
if I comment out the line where I throw the error it uploads successfully. Any help much appreciated thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论