Node.js Node:等待python脚本写入文件,然后将其上载到s3

Node.js Node:等待python脚本写入文件,然后将其上载到s3,node.js,express,amazon-s3,Node.js,Express,Amazon S3,我已经完成了以下代码。在这里,我通过python脚本创建一个文件,然后将其上载到S3,然后让用户能够下载它 exports.createFeature = async (req, res, next) => { let retourUrl = await uploadFile(req.body) res.status(201).json(retourUrl) }; function uploadFile(feature) { return new Promis

我已经完成了以下代码。在这里,我通过python脚本创建一个文件,然后将其上载到S3,然后让用户能够下载它

 exports.createFeature = async (req, res, next) => {
  
  let retourUrl = await uploadFile(req.body)

  res.status(201).json(retourUrl)

 };

function uploadFile(feature) {

  return new Promise(async (resolve, reject) => {
    
    let options = {
      scriptPath: 'pathToDcript',
      args: [arg1, arg2, arg3]
  };
   
  PythonShell.run('script.py', options, function (err) {
    if (err) throw err;
    console.log('file has been created !');

    //read the file 
    let contents = fs.readFileSync('pathToFile', {encoding:'utf8', flag:'r'});

    
    //convert it to buffer
    const fileContent = Buffer.from(contents, "utf-8");

    // Setting up S3 upload parameters
    let key = keyUserData+feature.userId+'/fileName'
    const params = {
        Bucket: bucket,
        Key:  key, // File name you want to save as in S3
        Body: fileContent
    };

    // Uploading files to the bucket
    s3.upload(params, function(err, data) {
        if (err) {
            throw err;
        }
        //console.log(`File uploaded successfully. ${data.Location}`);
    });
    
    // delete the file 
    fs.unlinkSync('pathToFile');
    
    //get url for download
    const presignedURL = s3.getSignedUrl('getObject', {
      Bucket: bucket,
      Key: key,
      Expires: 60*5
    })
    resolve(presignedURL)
  })
  });
    
}

但是在文件上传到S3之前,我已经有了下载url,你知道如何让它等到全部完成吗?

AWS SDK的S3
上传方法返回了一个可以期待的承诺

例如:

await s3.upload(...)
PythonShell.run('script.py', options, async function (err)
注意,在这种情况下,Python脚本的回调函数应该更改为
async
函数,以便允许
wait
语法。例如:

await s3.upload(...)
PythonShell.run('script.py', options, async function (err)

如果要使用
s3.upload
和回调。您需要更改代码,如下所述

exports.createFeature = async (req, res, next) => {

  let retourUrl = await uploadFile(req.body)

  res.status(201).json(retourUrl)

};

function uploadFile(feature) {

  return new Promise((resolve, reject) => {

    let options = {
      scriptPath: 'pathToDcript',
      args: [arg1, arg2, arg3]
    };

    PythonShell.run('script.py', options, function (err) {
      if (err) throw err;
      console.log('file has been created !');

      //read the file 
      let contents = fs.readFileSync('pathToFile', { encoding: 'utf8', flag: 'r' });


      //convert it to buffer
      const fileContent = Buffer.from(contents, "utf-8");

      // Setting up S3 upload parameters
      let key = keyUserData + feature.userId + '/fileName'
      const params = {
        Bucket: bucket,
        Key: key, // File name you want to save as in S3
        Body: fileContent
      };

      // Uploading files to the bucket
      s3.upload(params, function (err, data) {
        if (err) {
          throw err;
        }
        // delete the file 
        fs.unlinkSync('pathToFile');
  
        //get url for download
        const presignedURL = s3.getSignedUrl('getObject', {
          Bucket: bucket,
          Key: key,
          Expires: 60 * 5
        })
        //console.log(`File uploaded successfully. ${data.Location}`);
        resolve(presignedURL)
      });
    })
  });

}

谢谢你的回答!我以前试过,但没用。我明白了。所以这可能是真正的问题。考虑改变你的问题,包括你所拥有的问题。例如,您收到了什么错误消息?或者,如果您希望使用回调进行上载,则需要将所有相关代码移到该回调中。见阿卡什的回答。