为什么在此片段中未调用s3.pupobject

发布于 2025-02-08 12:32:23 字数 1573 浏览 0 评论 0原文

当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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文