Skip to content

Commit 375e701

Browse files
committed
Merge pull request #833 from ParsePlatform/flovilmart.S3Environment
Improves loading of Push Adapter, fix loading of S3Adapter
2 parents 00d0d3e + a44b1d9 commit 375e701

File tree

5 files changed

+60
-26
lines changed

5 files changed

+60
-26
lines changed

Diff for: README.md

+14
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,20 @@ PARSE_SERVER_MAX_UPLOAD_SIZE
135135

136136
```
137137

138+
##### Configuring S3 Adapter
139+
140+
You can use the following environment variable setup the S3 adapter
141+
142+
```js
143+
S3_ACCESS_KEY
144+
S3_SECRET_KEY
145+
S3_BUCKET
146+
S3_REGION
147+
S3_BUCKET_PREFIX
148+
S3_DIRECT_ACCESS
149+
150+
```
151+
138152
## Contributing
139153

140154
We really want Parse to be yours, to see it grow and thrive in the open source community. Please see the [Contributing to Parse Server guide](CONTRIBUTING.md).

Diff for: spec/AdapterLoader.spec.js

+25
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11

22
var loadAdapter = require("../src/Adapters/AdapterLoader").loadAdapter;
33
var FilesAdapter = require("../src/Adapters/Files/FilesAdapter").default;
4+
var ParsePushAdapter = require("../src/Adapters/Push/ParsePushAdapter");
5+
var S3Adapter = require("../src/Adapters/Files/S3Adapter").default;
46

57
describe("AdapterLoader", ()=>{
68

@@ -84,4 +86,27 @@ describe("AdapterLoader", ()=>{
8486
}).not.toThrow("foo is required for that adapter");
8587
done();
8688
});
89+
90+
it("should load push adapter from options", (done) => {
91+
var options = {
92+
ios: {
93+
bundleId: 'bundle.id'
94+
}
95+
}
96+
expect(() => {
97+
var adapter = loadAdapter(undefined, ParsePushAdapter, options);
98+
expect(adapter.constructor).toBe(ParsePushAdapter);
99+
expect(adapter).not.toBe(undefined);
100+
}).not.toThrow();
101+
done();
102+
});
103+
104+
it("should load S3Adapter from direct passing", (done) => {
105+
var s3Adapter = new S3Adapter("key", "secret", "bucket")
106+
expect(() => {
107+
var adapter = loadAdapter(s3Adapter, FilesAdapter);
108+
expect(adapter).toBe(s3Adapter);
109+
}).not.toThrow();
110+
done();
111+
})
87112
});

Diff for: src/Adapters/AdapterLoader.js

+1-8
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,8 @@ export function loadAdapter(adapter, defaultAdapter, options) {
2828
return loadAdapter(adapter.class, undefined, adapter.options);
2929
} else if (adapter.adapter) {
3030
return loadAdapter(adapter.adapter, undefined, adapter.options);
31-
} else {
32-
// Try to load the defaultAdapter with the options
33-
// The default adapter should throw if the options are
34-
// incompatible
35-
try {
36-
return loadAdapter(defaultAdapter, undefined, adapter);
37-
} catch (e) {};
3831
}
39-
// return the adapter as is as it's unusable otherwise
32+
// return the adapter as provided
4033
return adapter;
4134
}
4235

Diff for: src/Adapters/Files/S3Adapter.js

+18-17
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,33 @@ import requiredParameter from '../../requiredParameter';
88

99
const DEFAULT_S3_REGION = "us-east-1";
1010

11-
function parseS3AdapterOptions(...options) {
12-
if (options.length === 1 && typeof options[0] == "object") {
13-
return options;
11+
function requiredOrFromEnvironment(env, name) {
12+
let environmentVariable = process.env[env];
13+
if (!environmentVariable) {
14+
requiredParameter(`S3Adapter requires an ${name}`);
1415
}
15-
16-
const additionalOptions = options[3] || {};
17-
18-
return {
19-
accessKey: options[0],
20-
secretKey: options[1],
21-
bucket: options[2],
22-
region: additionalOptions.region
16+
return environmentVariable;
17+
}
18+
19+
function fromEnvironmentOrDefault(env, defaultValue) {
20+
let environmentVariable = process.env[env];
21+
if (environmentVariable) {
22+
return environmentVariable;
2323
}
24+
return defaultValue;
2425
}
2526

2627
export class S3Adapter extends FilesAdapter {
2728
// Creates an S3 session.
2829
// Providing AWS access and secret keys is mandatory
2930
// Region and bucket will use sane defaults if omitted
3031
constructor(
31-
accessKey = requiredParameter('S3Adapter requires an accessKey'),
32-
secretKey = requiredParameter('S3Adapter requires a secretKey'),
33-
bucket,
34-
{ region = DEFAULT_S3_REGION,
35-
bucketPrefix = '',
36-
directAccess = false } = {}) {
32+
accessKey = requiredOrFromEnvironment('S3_ACCESS_KEY', 'accessKey'),
33+
secretKey = requiredOrFromEnvironment('S3_SECRET_KEY', 'secretKey'),
34+
bucket = fromEnvironmentOrDefault('S3_BUCKET', undefined),
35+
{ region = fromEnvironmentOrDefault('S3_REGION', DEFAULT_S3_REGION),
36+
bucketPrefix = fromEnvironmentOrDefault('S3_BUCKET_PREFIX', ''),
37+
directAccess = fromEnvironmentOrDefault('S3_DIRECT_ACCESS', false) } = {}) {
3738
super();
3839

3940
this._region = region;

Diff for: src/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ function ParseServer({
133133
const filesControllerAdapter = loadAdapter(filesAdapter, () => {
134134
return new GridStoreAdapter(databaseURI);
135135
});
136-
const pushControllerAdapter = loadAdapter(push, ParsePushAdapter);
136+
// Pass the push options too as it works with the default
137+
const pushControllerAdapter = loadAdapter(push && push.adapter, ParsePushAdapter, push);
137138
const loggerControllerAdapter = loadAdapter(loggerAdapter, FileLoggerAdapter);
138139
const emailControllerAdapter = loadAdapter(emailAdapter);
139140
// We pass the options and the base class for the adatper,

0 commit comments

Comments
 (0)