【问题标题】:S3 Upload Failing Silently in ProductionS3 上传在生产中静默失败
【发布时间】:2022-03-08 03:36:16
【问题描述】:

我正在努力调试 NextJS API,该 API 正在开发中(通过 localhost),但默默地在生产中失败。

下面,两个console.log statements 没有返回,所以我怀疑textToSpeech 调用没有正确执行,可能及时吗?

我不确定如何纠正,很高兴按照指示进行调试以解决此问题!

const faunadb = require('faunadb')
const secret = process.env.FAUNADB_SECRET_KEY
const q = faunadb.query
const client = new faunadb.Client({ secret })
const TextToSpeechV1 = require('ibm-watson/text-to-speech/v1')
const { IamAuthenticator } = require('ibm-watson/auth')
const AWS = require('aws-sdk')
const { randomUUID } = require('crypto')
import { requireAuth } from '@clerk/nextjs/api'

module.exports = requireAuth(async (req, res) => {
  try {
    const s3 = new AWS.S3({
      accessKeyId: process.env.AWS_ACCESS_KEY,
      secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
    })

    const textToSpeech = new TextToSpeechV1({
      authenticator: new IamAuthenticator({
        apikey: process.env.IBM_API_KEY
      }),
      serviceUrl: process.env.IBM_SERVICE_URL
    })

    const uuid = randomUUID()

    const { echoTitle, chapterTitle, chapterText } = req.body

    const synthesizeParams = {
      text: chapterText,
      accept: 'audio/mp3',
      voice: 'en-US_KevinV3Voice'
    }

    textToSpeech
      .synthesize(synthesizeParams)
      .then(buffer => {
        const s3Params = {
          Bucket: 'waveforms/audioform',
          Key: `${uuid}.mp3`,
          Body: buffer.result,
          ContentType: 'audio/mp3',
          ACL: 'public-read'
        }

        console.log(buffer.result)
        console.log(s3Params)

        s3.upload(s3Params, function (s3Err, data) {
          if (s3Err) throw s3Err
          console.log(`File uploaded successfully at ${data.Location}`)
        })
      })
      .catch(err => {
        console.log('error:', err)
      })

    const dbs = await client.query(
      q.Create(q.Collection('audioform'), {
        data: {
          title: echoTitle,
          published: 2022,
          leadAuthor: 'winter',
          user: req.session.userId,
          authors: 1,
          playTime: 83,
          chapters: 1,
          gpt3Description: '',
          likes: 20,
          image:
            'https://waveforms.s3.us-east-2.amazonaws.com/images/Mars.jpeg',
          trackURL: `https://waveforms.s3.us-east-2.amazonaws.com/audioform/${uuid}.mp3`,
          albumTracks: [
            {
              title: chapterTitle,
              text: chapterText,
              trackURL: `https://waveforms.s3.us-east-2.amazonaws.com/audioform/${uuid}.mp3`
            }
          ]
        }
      })
    )
    res.status(200).json(dbs.data)
  } catch (e) {
    res.status(500).json({ error: e.message })
  }
})

【问题讨论】:

  • 您似乎没有在等待textToSpeech.synthesize() 结果,因此代码继续到await client.query(),然后返回响应。
  • @jarmod 代码应该是什么样的?是否只需要:await textToSpeech.synthesize()?这是唯一必要的改变吗?
  • @jarmod 我加了await,但是还是一样的问题,S3桶里没有注册上传。
  • 类似const buffer = await textToSpeech.synthesize(synthesizeParams);,然后你可以在s3Params中使用缓冲区,然后你可以await s3.upload(s3Params).promise()
  • 我不完全确定那会是什么样子 - 我收到 await s3... 的类型错误

标签: javascript node.js amazon-web-services amazon-s3 next.js


【解决方案1】:

替换像这样的异步片段,假设它们是按顺序执行的。

try {
  // code removed here for clarity
  const buffer = await textToSpeech.synthesize(synthesizeParams);

  const s3Params = {
    Bucket: 'waveforms/audioform',
    Key: `${uuid}.mp3`,
    Body: buffer.result,
    ContentType: 'audio/mp3',
    ACL: 'public-read'
  }

  await s3.upload(s3Params).promise();

  const dbs = await client.query(...);

  res.status(200).json(dbs.data);
} catch (e) {
  res.status(500).json({ error: e.message });
}

【讨论】:

  • 它在开发中工作。在生产中,我收到以下错误:The request signature we calculated does not match the signature you provided. Check your key and signing method. 我尝试旋转按键,但没有帮助:/
  • 确保您的客户端机器是时间同步的。此外,尝试使用 awscli 从同一客户端计算机上传文件,看看它是否以相同的方式失败。
  • 我正在使用 Vercel 进行构建 - 我不知道是否有我应该运行的命令。我会试试 awscli
  • 关于无效签名herehere的一些想法。
  • 我将密钥作为环境变量存储在 Vercel 中......它可以工作!
猜你喜欢
  • 2019-07-19
  • 1970-01-01
  • 2015-03-17
  • 2017-06-21
  • 1970-01-01
  • 2022-11-03
  • 1970-01-01
  • 2012-06-05
  • 1970-01-01
相关资源
最近更新 更多