1

我正在尝试使用 Google App Engine 灵活部署 API,将文件上传到 Google Storage。当我使用localhost它运行 API 时,它运行良好。但是使用已部署的应用程序,我收到此错误:

ApiError: Error during request.
at Object.parseHttpRespBody (/srv/node_modules/@google-cloud/common/src/util.js:187:32)
at Object.handleResp (/srv/node_modules/@google-cloud/common/src/util.js:131:18)

我向服务帐户授予了存储管理员权限,但也出现了同样的错误。

export function toStorage(request: Request, response: Response, next: NextFunction) {
  const storage = Storage({
    projectId: process.env.GCLOUD_PROJECT
  })
  // A bucket is a container for objects (files).
  const bucket = storage.bucket(process.env.OUTPUT)
  console.log(process.env.GCLOUD_PROJECT)
  console.log(process.env.OUTPUT)
  if (!request.file) {
    response.status(400).send('No file uploaded.')
    return
  }

  // Create a new blob in the bucket and upload the file data.
  const blob = bucket.file(request.file.originalname)
  const blobStream = blob.createWriteStream({
    resumable: false
  })

  blobStream.on('error', (err: Error) => {
    next(err)
  })

  blobStream.on('finish', () => {
    // The public URL can be used to directly access the file via HTTP.
    const publicUrl = format(`https://storage.googleapis.com/${bucket.name}/${blob.name}`)
    sendResponse(response, publicUrl)
  })

  blobStream.end(request.file.buffer)

}

在此先感谢支持帮助。

4

0 回答 0