Google Cloud Function在尝试连接到Google CloudSQL (mysql)实例时返回模糊'Connection error'



我在谷歌试用这个代码库代码片段。我想以某种方式对其进行修改,以便使用CloudSQL服务将Vision API元数据存储在关系MySQL中——下面是他们关于如何将云函数与CloudSQL连接的示例。

我最终部署了代码,但在触发该功能时(通过上传新图像(,我在日志中得到了一个模糊的"连接错误",没有更多信息。这是我此刻的代码:

const vision = require('@google-cloud/vision');
const Storage = require('@google-cloud/storage');

const client = new vision.ImageAnnotatorClient();
const winston = require('winston');
const {LoggingWinston} = require('@google-cloud/logging-winston');
const loggingWinston = new LoggingWinston();
const logger = winston.createLogger({
level: 'info',
transports: [new winston.transports.Console(), loggingWinston],
});
const createUnixSocketPool = async (config) => {
const dbSocketPath = "/cloudsql"
return await mysql.createPool({
user: 'root',
password: 'mypassword',
database: 'mydatabase',
socketPath: `${dbSocketPath}/cs-03-282615:europe-west1:mydatabase`,
...config
});
}

const createPool = async () => {
const config = {
connectionLimit: 5,
connectTimeout: 10000,
acquireTimeout: 10000,
waitForConnections: true,
queueLimit: 0,
}
return await createUnixSocketPool(config); 
};
let pool;
const poolPromise = createPool()
.then(async (pool) => {
return pool;
})
.catch((err) => {
logger.error(err);
process.exit(1)
});

exports.vision_analysis = async (event, context, pool) => {
console.log(`Event: ${JSON.stringify(event)}`);

const filename = event.name;
const filebucket = event.bucket;

console.log(`New picture uploaded ${filename} in ${filebucket}`);

const request = {
image: { source: { imageUri: `gs://${filebucket}/${filename}` } },
features: [
{ type: 'LABEL_DETECTION' },
{ type: 'IMAGE_PROPERTIES' },
{ type: 'SAFE_SEARCH_DETECTION' }
]
};

// invoking the Vision API
const [response] = await client.annotateImage(request);
console.log(`Raw vision output for: ${filename}: ${JSON.stringify(response)}`);

if (response.error === null) {
// listing the labels found in the picture
const labels = response.labelAnnotations
.sort((ann1, ann2) => ann2.score - ann1.score)
.map(ann => ann.description)
console.log(`Labels: ${labels.join(', ')}`);

// retrieving the dominant color of the picture
const color = response.imagePropertiesAnnotation.dominantColors.colors
.sort((c1, c2) => c2.score - c1.score)[0].color;
const colorHex = decColorToHex(color.red, color.green, color.blue);
console.log(`Colors: ${colorHex}`);

// determining if the picture is safe to show
const safeSearch = response.safeSearchAnnotation;
const isSafe = ["adult", "spoof", "medical", "violence", "racy"].every(k =>
!['LIKELY', 'VERY_LIKELY'].includes(safeSearch[k]));
console.log(`Safe? ${isSafe}`);

if (isSafe) {
const pool = await poolPromise();


const stmt = 'INSERT INTO sc_03_metadata_schema (labels, color, created) VALUES (?, ?, ?)';
await pool.query(stmt, [labels, colorHex, NOW()]);

console.log("Stored metadata in CloudSQL");
}
} else {
throw new Error(`Vision API error: code ${response.error.code}, message: "${response.error.message}"`);
}
};


function decColorToHex(r, g, b) {
return '#' + Number(r).toString(16).padStart(2, '0') +
Number(g).toString(16).padStart(2, '0') +
Number(b).toString(16).padStart(2, '0');
}

完整错误如下:错误日志

我知道您希望通过Cloud Function连接到您的Cloud SQL MySQL实例。从引用公共文档"从云函数连接到云SQL"可以看出,在实例化dbSocketPath时似乎没有包括"process.env.DB_SOCKET_PATH ||"。

关于模糊的错误消息,这是一个已知的问题,云功能专家目前正在调查其中一个问题。我建议您查看公共跟踪器,单击星形图标订阅该问题以获取进一步的更新,并单击钟形图标以通过电子邮件收到更新通知。我们目前还没有关于解决这个问题的预计到达时间。

相关内容

  • 没有找到相关文章

最新更新