我正在尝试使用Google App Engine flexible部署的API,该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)
}
首先感谢支持帮助。