Merge pull request #833 from ParsePlatform/flovilmart.S3Environment
Improves loading of Push Adapter, fix loading of S3Adapter
This commit is contained in:
14
README.md
14
README.md
@@ -135,6 +135,20 @@ PARSE_SERVER_MAX_UPLOAD_SIZE
|
||||
|
||||
```
|
||||
|
||||
##### Configuring S3 Adapter
|
||||
|
||||
You can use the following environment variable setup the S3 adapter
|
||||
|
||||
```js
|
||||
S3_ACCESS_KEY
|
||||
S3_SECRET_KEY
|
||||
S3_BUCKET
|
||||
S3_REGION
|
||||
S3_BUCKET_PREFIX
|
||||
S3_DIRECT_ACCESS
|
||||
|
||||
```
|
||||
|
||||
## Contributing
|
||||
|
||||
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).
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
|
||||
var loadAdapter = require("../src/Adapters/AdapterLoader").loadAdapter;
|
||||
var FilesAdapter = require("../src/Adapters/Files/FilesAdapter").default;
|
||||
var ParsePushAdapter = require("../src/Adapters/Push/ParsePushAdapter");
|
||||
var S3Adapter = require("../src/Adapters/Files/S3Adapter").default;
|
||||
|
||||
describe("AdapterLoader", ()=>{
|
||||
|
||||
@@ -84,4 +86,27 @@ describe("AdapterLoader", ()=>{
|
||||
}).not.toThrow("foo is required for that adapter");
|
||||
done();
|
||||
});
|
||||
|
||||
it("should load push adapter from options", (done) => {
|
||||
var options = {
|
||||
ios: {
|
||||
bundleId: 'bundle.id'
|
||||
}
|
||||
}
|
||||
expect(() => {
|
||||
var adapter = loadAdapter(undefined, ParsePushAdapter, options);
|
||||
expect(adapter.constructor).toBe(ParsePushAdapter);
|
||||
expect(adapter).not.toBe(undefined);
|
||||
}).not.toThrow();
|
||||
done();
|
||||
});
|
||||
|
||||
it("should load S3Adapter from direct passing", (done) => {
|
||||
var s3Adapter = new S3Adapter("key", "secret", "bucket")
|
||||
expect(() => {
|
||||
var adapter = loadAdapter(s3Adapter, FilesAdapter);
|
||||
expect(adapter).toBe(s3Adapter);
|
||||
}).not.toThrow();
|
||||
done();
|
||||
})
|
||||
});
|
||||
|
||||
@@ -28,15 +28,8 @@ export function loadAdapter(adapter, defaultAdapter, options) {
|
||||
return loadAdapter(adapter.class, undefined, adapter.options);
|
||||
} else if (adapter.adapter) {
|
||||
return loadAdapter(adapter.adapter, undefined, adapter.options);
|
||||
} else {
|
||||
// Try to load the defaultAdapter with the options
|
||||
// The default adapter should throw if the options are
|
||||
// incompatible
|
||||
try {
|
||||
return loadAdapter(defaultAdapter, undefined, adapter);
|
||||
} catch (e) {};
|
||||
}
|
||||
// return the adapter as is as it's unusable otherwise
|
||||
// return the adapter as provided
|
||||
return adapter;
|
||||
}
|
||||
|
||||
|
||||
@@ -8,19 +8,20 @@ import requiredParameter from '../../requiredParameter';
|
||||
|
||||
const DEFAULT_S3_REGION = "us-east-1";
|
||||
|
||||
function parseS3AdapterOptions(...options) {
|
||||
if (options.length === 1 && typeof options[0] == "object") {
|
||||
return options;
|
||||
function requiredOrFromEnvironment(env, name) {
|
||||
let environmentVariable = process.env[env];
|
||||
if (!environmentVariable) {
|
||||
requiredParameter(`S3Adapter requires an ${name}`);
|
||||
}
|
||||
|
||||
const additionalOptions = options[3] || {};
|
||||
|
||||
return {
|
||||
accessKey: options[0],
|
||||
secretKey: options[1],
|
||||
bucket: options[2],
|
||||
region: additionalOptions.region
|
||||
return environmentVariable;
|
||||
}
|
||||
|
||||
function fromEnvironmentOrDefault(env, defaultValue) {
|
||||
let environmentVariable = process.env[env];
|
||||
if (environmentVariable) {
|
||||
return environmentVariable;
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
export class S3Adapter extends FilesAdapter {
|
||||
@@ -28,12 +29,12 @@ export class S3Adapter extends FilesAdapter {
|
||||
// Providing AWS access and secret keys is mandatory
|
||||
// Region and bucket will use sane defaults if omitted
|
||||
constructor(
|
||||
accessKey = requiredParameter('S3Adapter requires an accessKey'),
|
||||
secretKey = requiredParameter('S3Adapter requires a secretKey'),
|
||||
bucket,
|
||||
{ region = DEFAULT_S3_REGION,
|
||||
bucketPrefix = '',
|
||||
directAccess = false } = {}) {
|
||||
accessKey = requiredOrFromEnvironment('S3_ACCESS_KEY', 'accessKey'),
|
||||
secretKey = requiredOrFromEnvironment('S3_SECRET_KEY', 'secretKey'),
|
||||
bucket = fromEnvironmentOrDefault('S3_BUCKET', undefined),
|
||||
{ region = fromEnvironmentOrDefault('S3_REGION', DEFAULT_S3_REGION),
|
||||
bucketPrefix = fromEnvironmentOrDefault('S3_BUCKET_PREFIX', ''),
|
||||
directAccess = fromEnvironmentOrDefault('S3_DIRECT_ACCESS', false) } = {}) {
|
||||
super();
|
||||
|
||||
this._region = region;
|
||||
|
||||
@@ -133,7 +133,8 @@ function ParseServer({
|
||||
const filesControllerAdapter = loadAdapter(filesAdapter, () => {
|
||||
return new GridStoreAdapter(databaseURI);
|
||||
});
|
||||
const pushControllerAdapter = loadAdapter(push, ParsePushAdapter);
|
||||
// Pass the push options too as it works with the default
|
||||
const pushControllerAdapter = loadAdapter(push && push.adapter, ParsePushAdapter, push);
|
||||
const loggerControllerAdapter = loadAdapter(loggerAdapter, FileLoggerAdapter);
|
||||
const emailControllerAdapter = loadAdapter(emailAdapter);
|
||||
// We pass the options and the base class for the adatper,
|
||||
|
||||
Reference in New Issue
Block a user