请看这2张图片
对于 lambda 代码本身,这是我在 Node.js 中与 API 网关连接的 S3 Lambda 代码
const AWS = require('aws-sdk');
const XRay = require('aws-xray-sdk'); // Import the X-Ray SDK
const S3 = XRay.captureAWSClient(new AWS.S3());
exports.handler = async (event) => {
try {
// Extract picture data from the event (e.g., base64 encoded data)
const pictureData = event.pictureData.toString();
// Specify the S3 bucket and object key
const bucketName = 'MyBucketName';
const objectKey = event.pictureKey + '.jpg'; // Adjust the file extension accordingly
// Convert the picture data to a buffer
const buffer = Buffer.from(pictureData.replace(/^data:image\/\w+;base64,/, ""), 'base64');
console.log(buffer);
console.log(pictureData);
await XRay.captureAsyncFunc('S3Upload', async (subsegment) => {
// Upload the picture to S3
await S3.upload({
Bucket: bucketName,
Key: objectKey,
Body: buffer,
ACL: 'public-read',
ContentType: 'image/jpeg',
ContentEncoding: 'base64'
}).promise();
subsegment.close(); // Close the subsegment when the operation is done
});
const s3ImageLink = 'https://giftcardshop-group55-ddac.s3.amazonaws.com/' + objectKey;
return {
statusCode: 200,
body: JSON.stringify(s3ImageLink),
};
} catch (err) {
console.error('Error uploading picture:', err);
return {
statusCode: 500,
body: JSON.stringify('Error uploading picture'),
};
}
};
代码有效,它在 API 调用上将数据插入到我的 S3 存储桶中,但它在 CloudWatch 服务地图上不可见,有人知道如何解决此问题吗?
这是
S3.upload
命令的一个已知问题(由于它是在 AWS SDK 库中设计的),并记录在此 Github 问题中。您可以尝试将此作为解决方法:
const XRay = require('aws-xray-sdk');
const AWS = XRay.captureAWS(require('aws-sdk'));
const S3 = new AWS.S3();
这个问题似乎已在 AWS SDK v3 中得到解决,现在推荐使用 v2。