s3.当使用谷歌云功能时,getObject永远不会响应



我正在尝试呼叫aws s3。getObject (v2)或。send (v3)在Google Cloud Function中,请求总是超时。开发时,我在本地使用一个简单的node/express服务器来创建/测试,然后将代码放入云函数中。下面是我使用express server

的两个工作版本
const params = {
Bucket: process.env.AWS_BUCKET_NAME,
Key: key,
};

V3

const createParams = new GetObjectCommand(params);
const response = await client.send(createParams);

V2

const s3Asset = await s3.getObject(params).promise();

这两种方法都将返回所请求的正确文件,然后我将其作为Buffer发送或在多个文件时创建Zip。

一旦我把它放在Google Cloud Function中,它将永远不会返回和超时。我运行了一个快速的http.url()调用google.com,它返回了一个状态码200,所以我知道它有互联网访问。它有一个VCP连接器来允许所有的流量。env变量设置准确。我还错过了什么?

这是我的谷歌云功能

const s3 = new S3Client({
credentials: {
secretAccessKey: process.env.AWS_ACCESS_KEY,
accessKeyId: process.env.AWS_ACCESS_ID,
},
region: process.env.AWS_REGION,
correctClockSkew: true,
});
exports.downloadAssets = async (req, res) => {
res.set('Access-Control-Allow-Origin', '*');
if (req.method === 'OPTIONS') {
console.log('Hit OPTIONS');
res.set('Access-Control-Allow-Methods', 'POST');
res.set('Access-Control-Allow-Headers', 'Content-Type');
res.set('Access-Control-Max-Age', '3600');
res.status(204).send('');
} else {
console.log('***** Assets to downloadn', req.body?.assets, '*****');
try {
if (req.body?.assets) {
var params = {
Bucket: process.env.AWS_BUCKET_NAME,
Key: key,
};
const s3Asset = await s3.getObject(params).promise(); <<---V2
console.log('*** I have asset *** n',s3Asset) <<<-- this line never hits
res.send({ buffer: s3Asset.Body, name: 'nameOfFile', mimeType: s3Asset.ContentType });
} else {
return res.status(400).json({ error: 'No Assets Sent' });
}
} catch (e) {
console.log('Error downloading assets: n', e);
res.status(400).json({ error: e.message });
}
}
};

更改Cloud Function的出口设置,只路由通过VPC连接器到达私有IP范围的流量。

当您创建VPC无服务器连接器时,App Engine, Cloud Run和Cloud Functions等资源可以连接到VPC内的资源,如VM。

假设您有2个具有内部ip的vm10.128.0.2&10.128.0.3。然后,使CF使用VPC无服务器连接器用于出口流量到内部资源,您可以从函数代码中使用其内部IP调用这些虚拟机(创建函数时请记住使用Route only requests to private IPs through the VPC connector,以便只有到达内部资源的流量使用VPC)。

最新更新