AWS 无服务器图像处理程序错误处理

时间:2020-12-30 22:41:52

标签: amazon-web-services amazon-s3 aws-lambda

我使用的是最新版本的 AWS Serverless Image Handler Stack Formation,根据 documentation AWS Lambda 的请求和响应负载限制为 6MB。我的图片已经存储在 S3 存储桶中,我想修改 Image Handler,如果转换过程中发生任何错误,则返回存储在原始 S3 存储桶中的对象的 URL,即对象 URL对于原始未编辑对象。解决此问题的最佳方法是什么?

1 个答案:

答案 0 :(得分:0)

因此根据 documentation 模板有三个变量

<块引用>

EnableDefaultFallbackImage、FallbackImageS3Bucket 和 FallbackImageS3Key。在启用此功能之前,如果您在后备图像 Amazon S3 存储桶中使用 Amazon S3 存储桶策略,则必须编辑存储桶策略以允许 CustomResourceFunction 和 ImageHandlerFunction AWS Lambda 函数获取默认后备图像对象。

在相应的 code 中,您可以使用上传对象的原始源存储桶以及您尝试修改的密钥。

特别是这部分代码

        try {
        data = await s3.headObject({ Bucket: bucket, Key: key }).promise();
        break;
    } catch (error) {
        if (retry === retryCount || !['AccessDenied', 'Forbidden'].includes(error.code)) {
            console.error(`Either the object does not exist or you don't have permission to access the object: ${bucket}/${key}`);
            throw {
                code: 'FallbackImageError',
                message: `Either the object does not exist or you don't have permission to access the object: ${bucket}/${key}`
            };
        } else {
            console.log('Waiting for retry...');
            await sleep(retry);
        }
    }