Google Cloud Run集成Cloudinary文件上传故障排查求助
问题描述
我在Google Cloud Run上部署的Node.js服务(基于Cloudinary实现文件上传功能)近期突然失效,但该服务在本地环境及Render云服务上均可正常运行。最初使用Express.js搭配Multer处理上传,后简化代码,仅保留一个不依赖Multer、直接调用Cloudinary SDK的上传API,问题仍未解决。
相关代码
const express = require('express'); const cloudinary = require('cloudinary').v2; const dotenv = require('dotenv'); dotenv.config(); // Configure Cloudinary cloudinary.config({ cloud_name: process.env.CLOUDINARY_CLOUD_NAME, api_key: process.env.CLOUDINARY_API_KEY, api_secret: process.env.CLOUDINARY_SECRET }); // Create Express app const app = express(); // Enable JSON parsing middleware app.use(express.json()); // POST route for uploading a PNG file to Cloudinary app.post('/upload', (req, res) => { // Check if a file was uploaded if (!req.body || !req.body.data) { return res.status(400).json({ error: 'No file uploaded' }); } // Upload file to Cloudinary cloudinary.uploader.upload( req.body.data, { resource_type: 'image', format: 'png' }, (error, result) => { if (error) { console.error('Upload error:', error); return res.status(500).json({ error: 'Failed to upload file' }); } // Return the Cloudinary URL of the uploaded image res.json({ url: result.secure_url }); } ); }); // Start the server app.listen(process.env.PORT, () => { console.log('Server started on port 3000'); });
请求示例
curl --location 'https://fileuploadwithoutmulter-ssnwhxubxq-em.a.run.app/upload' \ --header 'Content-Type: application/json' \ --data '{\n "data":"data:image/jpg;base64,<BASE64_CODE>" }'
Cloud Run日志
2023-05-19 02:06:43.937 IST POST500760 B2 sPostmanRuntime/7.32.2 https://fileuploadwithoutmulter-ssnwhxubxq-em.a.run.app/upload 2023-05-19 02:06:45.966 IST Upload error: Error [ERR_SOCKET_CONNECTION_TIMEOUT]: Socket connection timeout 2023-05-19 02:06:45.966 IST at new NodeError (node:internal/errors:399:5) 2023-05-19 02:06:45.966 IST at internalConnectMultiple (node:net:1099:20) 2023-05-19 02:06:45.966 IST at Timeout.internalConnectMultipleTimeout (node:net:1638:3) 2023-05-19 02:06:45.966 IST at listOnTimeout (node:internal/timers:575:11) 2023-05-19 02:06:45.966 IST at process.processTimers (node:internal/timers:514:7) { 2023-05-19 02:06:45.966 IST code: 'ERR_SOCKET_CONNECTION_TIMEOUT' 2023-05-19 02:06:45.966 IST }
package.json
{ "name": "fileuploadwithoutmulter", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC", "dependencies": { "cloudinary": "^1.37.0", "dotenv": "^16.0.3", "express": "^4.18.2" } }
补充信息
- Cloudinary SDK版本:1.37.0
- 请求时间:约印度标准时间2:05
解决方案
检查Cloud Run网络配置
确认Cloud Run服务的出站流量规则是否允许访问Cloudinary的域名(api.cloudinary.com):- 若使用默认公网访问,确保无全局防火墙规则拦截HTTPS(443端口)请求
- 若启用VPC连接器,检查连接器的防火墙规则是否放行对Cloudinary域名的出站请求
调整Cloudinary SDK超时参数
旧版本SDK默认超时时间较短,在Cloud Run网络环境下易触发超时,手动增加超时配置:cloudinary.config({ cloud_name: process.env.CLOUDINARY_CLOUD_NAME, api_key: process.env.CLOUDINARY_API_KEY, api_secret: process.env.CLOUDINARY_SECRET, timeout: 30000 // 设置30秒超时,可根据实际情况调整 });升级Cloudinary SDK版本
当前使用的1.37.0版本可能存在网络兼容问题,升级至最新稳定版:npm install cloudinary@latest新版本SDK通常会修复Socket连接相关bug,优化网络请求逻辑。
验证Cloud Run环境变量配置
检查Cloud Run控制台中服务的环境变量(CLOUDINARY_CLOUD_NAME、CLOUDINARY_API_KEY、CLOUDINARY_SECRET)是否完全正确,无拼写错误、多余空格或特殊字符。调整Cloud Run资源配额
若服务分配的CPU/内存过低,处理上传时可能因资源不足导致连接超时,尝试调整配置:- CPU至少设置为1vCPU
- 内存至少设置为512MB
调整后重新部署服务测试
内容的提问来源于stack exchange,提问作者Shubham Aggarwal
相关产品推荐
相关产品推荐

