Skip to content

Commit

Permalink
Merge pull request #146 from humanmade/custom-s3-client-args
Browse files Browse the repository at this point in the history
Allow passing custom client arguments to S3 client
  • Loading branch information
shadyvb authored May 17, 2022
2 parents d82520f + 7d52170 commit 428be9d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
22 changes: 22 additions & 0 deletions docs/server.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,28 @@ Populate the `config.json` with the AWS region and bucket name you want to use,

These can also be passed via the `AWS_REGION`, and `AWS_S3_BUCKET` environment variables if required.

To use a custom endpoint URL, you can use set the `endpoint` key in `config.json` or the environment variable `AWS_S3_ENDPOINT` to point to the endpoint URL.

You can also use the `configArgs` key in `config.json` or environment variable `AWS_S3_CLIENT_ARGS` to pass arbitrary options to the AWS SDK client, as follows:

```json
{
"region": "eu-west-1",
"bucket": "hmn-uploads-eu",
"endpoint": "project-name.localhost",
"clientArgs": {
"s3BucketEndpoint": true,
}
}
```

OR

```bash
export AWS_S3_ENDPOINT="project-name.localhost"
export AWS_S3_CLIENT_ARGS="s3BucketEndpoint=true"
```


### Running the server

Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports = {};
module.exports.s3 = function(config, key, args, callback) {
AWS.config.region = config.region;

var s3config = {};
var s3config = Object.assign( {}, config.clientArgs );
if (config.endpoint) {
s3config.endpoint = config.endpoint;
}
Expand Down
2 changes: 1 addition & 1 deletion proxy-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ 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(config);
const s3 = new AWS.S3( Object.assign( {}, config, config.clientArgs ) );

let request;
if ( authenticatedRequest ) {
Expand Down
5 changes: 5 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ if ( process.env.AWS_REGION && process.env.AWS_S3_BUCKET ) {
config = JSON.parse( fs.readFileSync( 'config.json' ) );
}

config.clientArgs = config.clientArgs || {};
if ( process.env.AWS_S3_CLIENT_ARGS ) {
( new url.URLSearchParams( process.env.AWS_S3_CLIENT_ARGS ) ).forEach( ( value, key ) => config.clientArgs[ key ] = value );
}

http.createServer( function( request, response ) {
var params = url.parse( request.url, true );

Expand Down

0 comments on commit 428be9d

Please sign in to comment.