s3proxy
s3proxy copied to clipboard
Bug: S3Proxy does not support images.
Hello,
I set up a very simple lambda to proxy requests from s3. It uses the aws serverless express library to setup a basic express app, and then I leverage this library to handle the proxy. It is a very small app, here is the basics of it:
const awsServerlessExpress = require('aws-serverless-express');
const express = require('express');
const S3Proxy = require('s3proxy');
const app = express();
const proxy = new S3Proxy({ bucket: config.bucket });
proxy.init();
app.route('/*').get(proxyToS3);
function proxyToS3(req, res) {
proxy.get(req,res).on('error', () => res.end()).pipe(res);
}
const server = awsServerlessExpress.createServer(app);
exports.handler = (event, context) => awsServerlessExpress.proxy(server, event, context);
The problem is that lambda does not support streaming binary like data. So if I ever try to proxy an image it gets corrupted and the browser can never display it. To get around this I now do an s3 head request first to determine the type of object, and then updated my proxyToS3 function to look like
function proxyToS3(req, res) {
<do s3 head request to get metadata>
if (metadata.ContentType.match(/image/)) {
const params = {
Bucket: config.bucket,
Key: req.url
};
s3.getSignedUrl('getObject', params, function (err, url) {
if (err) {
res.end();
} else {
res.redirect(url);
}
});
} else {
proxy.get(req,res).on('error', () => res.end()).pipe(res);
}
}
What are people's thoughts about including a redirect functionality within the proxy that either we can call directly, or we can configure to tell it to redirect on certain object types instead of actually proxying?
Thanks,
Mark