forked from binuks/tachyon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy-file.js
35 lines (27 loc) · 870 Bytes
/
proxy-file.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const AWS = require( 'aws-sdk' );
const authenticatedRequest = !!process.env.S3_AUTHENTICATED_REQUEST ? process.env.S3_AUTHENTICATED_REQUEST.toLowerCase() === 'true' : false;
function sendOriginal( config, bucket, key, callback ) {
const s3 = new AWS.S3( Object.assign( {}, config, config.clientArgs ) );
let request;
if ( authenticatedRequest ) {
request = s3.makeRequest( 'getObject', { Bucket: bucket, Key: key } );
} else {
request = s3.makeUnauthenticatedRequest( 'getObject', { Bucket: bucket, Key: key } );
}
request.send( function( err, data ) {
if (err) {
return callback(err);
}
var resp = {
statusCode: 200,
headers: {
'Content-Type': data.ContentType,
},
body: Buffer.from(data.Body).toString('base64'),
isBase64Encoded: true,
};
callback(null, resp);
} );
return request;
}
module.exports = sendOriginal;