Skip to content

Commit 3da3588

Browse files
committed
Fixup container runtime and add EXPIRES param
1 parent 0e1a464 commit 3da3588

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

Diff for: Dockerfile

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ FROM mhart/alpine-node
33
RUN apk add --no-cache \
44
git
55

6+
COPY . /app
67
WORKDIR /app
78

8-
CMD /bin/sh
9+
RUN npm install
10+
11+
CMD node index.js

Diff for: README.md

+13-8
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,29 @@ both worlds:
1818
docker build -t ubergarm/s3-url-service .
1919
```
2020

21+
## Runtime Configuration
22+
Environment Variable | Description | Default
23+
--- | --- | ---
24+
`JWT_SECRET` | *The plain text HMAC-SHA256 symmetric secret key* | `secret`
25+
`EXPIRES` | *Link expiration and redirect cache duration (in seconds)* | `2592000` (30 days in seconds)
26+
`AWS_DEFAULT_REGION` | *AWS region* | `us-east-1`
27+
`AWS_ACCESS_KEY_ID` | *AWS ID credentials* | n/a
28+
`AWS_SECRET_ACCESS_KEY` | *AWS SECRET credentials* | n/a
29+
2130
## Run
2231
Export your AWS credentials as environment variables then:
2332
```bash
2433
docker run --rm \
2534
-it \
26-
-v `pwd`:/app \
2735
-p 8080:8080 \
2836
-e JWT_SECRET=secret \
37+
-e EXPIRES=86400 \
2938
-e AWS_DEFAULT_REGION \
3039
-e AWS_ACCESS_KEY_ID \
3140
-e AWS_SECRET_ACCESS_KEY \
3241
ubergarm/s3-url-service
3342
```
34-
Once inside the container run:
35-
```bash
36-
npm install # first time only
37-
node index.js
38-
```
43+
*Optionally* you can add `-v $PWD:/app` to test without rebuilding etc...
3944

4045
## Test
4146
Download content:
@@ -90,8 +95,8 @@ sure your user/role has access to S3 from its attached IAM policy as well.)
9095
- [x] Test download
9196
- [x] Test upload
9297
- [x] Give example S3 Bucket Policy
93-
- [ ] Pass in caching parameters as environment variables
94-
- [ ] Cleanup how container starts
98+
- [x] Pass in caching parameters as environment variables
99+
- [x] Cleanup how container starts
95100
- [ ] Look more closely at `http` vs `https` support
96101
- [ ] Find way to cleanup duplicated code
97102
- [ ] Support multiple credentials/buckets secured with JWT claims (you can open a PR for this one! ;) )

Diff for: index.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ AWS.config.update({
1616
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
1717
});
1818

19+
// pull in a global config variable for link and cache expiration (in seconds)
20+
var EXPIRES = parseInt(process.env.EXPIRES) || 2592000 // 30 days (in seconds)
21+
1922
// S3 getObject redirect endpoint GET /:bucket/:key
2023
server.get(/^\/([a-zA-Z0-9_\.-]+)\/(.*)/, function(req, res, next) {
2124
// handle Bearer token claims here
@@ -35,12 +38,12 @@ server.get(/^\/([a-zA-Z0-9_\.-]+)\/(.*)/, function(req, res, next) {
3538
var params = {
3639
Bucket: bucket,
3740
Key: key,
38-
Expires: 2592000 // 30 days (in seconds)
41+
Expires: EXPIRES
3942
};
4043
s3.getSignedUrl('getObject', params, function (err, url) {
4144
if (err)
4245
return next(err);
43-
res.cache({maxAge: 2592000});
46+
res.cache({maxAge: EXPIRES});
4447
res.redirect(307, url, next);
4548
});
4649
});
@@ -64,12 +67,12 @@ server.put(/^\/([a-zA-Z0-9_\.-]+)\/(.*)/, function(req, res, next) {
6467
var params = {
6568
Bucket: bucket,
6669
Key: key,
67-
Expires: 2592000 // 30 days (in seconds)
70+
Expires: EXPIRES
6871
};
6972
s3.getSignedUrl('putObject', params, function (err, url) {
7073
if (err)
7174
return next(err);
72-
res.cache({maxAge: 2592000});
75+
res.cache({maxAge: EXPIRES});
7376
res.redirect(307, url, next);
7477
});
7578
});

0 commit comments

Comments
 (0)