【发布时间】:2020-07-29 10:21:25
【问题描述】:
我的目标是使用从 google cloud automl 训练的 Firebase 云函数导出 tflite 模型。
我按照https://cloud.google.com/vision/automl/object-detection/docs/export-edge的文档能够在终端上顺利curl导出模型,但不能使用axios on cloud功能。使用以下代码,我得到了 401 Unauthorized Error,即使我在 .env 中设置了 GOOGLE_APPLICATION_CREDENTIALS 并且需要 dotenv 包。
我的问题:是否可以使用 axios POST 请求导出模型?如果那我做错了什么?
//index.js
require("dotenv").config();
//.env
GOOGLE_APPLICATION_CREDENTIALS="./config.json"
async function exportModel() {
const header = {
"Content-Type": "application/json;charset=utf-8",
Authorization:
"Bearer $(gcloud auth application-default print-access-token)",
};
// Construct request
const request = {
outputConfig: {
modelFormat: "tflite",
gcsDestination: {
outputUriPrefix: `gs://${output-storage-bucket}/`,
},
},
};
axios
.post(
`https://automl.googleapis.com/v1/projects/${projectId}/locations/us-central1/models/${model_id}:export`,
request,
{
headers: header,
}
)
.then((response) => {
console.log(response);
return response;
})
.catch((error) => {
console.log(error);
return error;
});
}
错误
Error: Request failed with status code 401
at createError (/srv/node_modules/axios/lib/core/createError.js:16:15)
at settle (/srv/node_modules/axios/lib/core/settle.js:17:12)
at IncomingMessage.handleStreamEnd (/srv/node_modules/axios/lib/adapters/http.js:236:11)
at emitNone (events.js:111:20)
at IncomingMessage.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1064:12)
at _combinedTickCallback (internal/process/next_tick.js:139:11)
at process._tickDomainCallback (internal/process/next_tick.js:219:9)
【问题讨论】:
标签: javascript axios google-cloud-functions google-cloud-automl