Skip to content

Commit 6b5c1f9

Browse files
committed
Synchronise S3 initialisation with ember-cli-deploy-s3
1 parent e32e3e3 commit 6b5c1f9

File tree

4 files changed

+212
-24
lines changed

4 files changed

+212
-24
lines changed

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ The AWS secret for the user that has the ability to upload to the `bucket`. This
7676

7777
*Default:* `undefined`
7878

79+
### sessionToken
80+
81+
The AWS session token for the user that has the ability to upload to the `bucket`. This may be required if you are using the [AWS Security Token Service][6].
82+
This requires both `accessKeyId` and `secretAccessKey` to be defined.
83+
84+
*Default:* `undefined`
85+
7986
### profile
8087

8188
The AWS profile as definied in ~/.aws/credentials. If this is left undefined, the normal [AWS SDK credential resolution][7] will take place.
@@ -155,6 +162,11 @@ If `endpoint` set the `region` option will be ignored.
155162

156163
*Default:* `[region].s3.amazonaws.com`
157164

165+
### proxy
166+
167+
The network proxy url used when sending requests to S3.
168+
169+
*Default:* `undefined`
158170

159171
### serverSideEncryption
160172

lib/s3.js

+48-21
Original file line numberDiff line numberDiff line change
@@ -22,41 +22,68 @@ async function headObject(client, params) {
2222

2323
module.exports = CoreObject.extend({
2424
init: function(options) {
25-
this._super();
26-
var plugin = options.plugin;
27-
var config = plugin.pluginConfig;
28-
var profile = plugin.readConfig('profile');
29-
var endpoint = plugin.readConfig('endpoint');
30-
var credentials;
25+
this._super(options);
3126

32-
this._plugin = plugin;
33-
34-
var providedS3Client = plugin.readConfig("s3Client");
27+
var s3Options = {
28+
region: this.plugin.readConfig('region')
29+
};
3530

31+
const proxy = this.plugin.readConfig('proxy');
32+
if (proxy) {
33+
var agent;
34+
this._proxyAgent = this.plugin.readConfig('proxyAgent');
35+
if (this._proxyAgent) {
36+
agent = this._proxyAgent(proxy);
37+
} else {
38+
const { ProxyAgent } = require('proxy-agent');
39+
agent = new ProxyAgent(proxy);
40+
}
41+
s3Options.httpOptions = {
42+
agent
43+
};
44+
}
3645

37-
if (profile && !providedS3Client) {
38-
this._plugin.log("Using AWS profile from config", { verbose: true });
39-
credentials = fromIni({ profile: profile });
46+
const accessKeyId = this.plugin.readConfig('accessKeyId');
47+
const secretAccessKey = this.plugin.readConfig('secretAccessKey');
48+
const sessionToken = this.plugin.readConfig('sessionToken');
49+
const profile = this.plugin.readConfig('profile');
50+
const signatureVersion = this.plugin.readConfig('signatureVersion');
51+
const endpoint = this.plugin.readConfig('endpoint');
52+
53+
if (accessKeyId && secretAccessKey) {
54+
this.plugin.log('Using AWS access key id and secret access key from config', { verbose: true });
55+
s3Options.credentials = {
56+
accessKeyId: accessKeyId,
57+
secretAccessKey: secretAccessKey,
58+
};
59+
60+
if (sessionToken) {
61+
this.plugin.log('Using AWS session token from config', { verbose: true });
62+
s3Options.credentials.sessionToken = sessionToken;
63+
}
4064
}
4165

42-
if (endpoint) {
43-
this._plugin.log('Using endpoint from config', { verbose: true });
66+
if (signatureVersion) {
67+
this.plugin.log('Using signature version from config', { verbose: true });
68+
s3Options.signatureVersion = signatureVersion;
4469
}
4570

46-
this._client = providedS3Client || new S3(config);
71+
if (profile && !this.plugin.readConfig('s3Client')) {
72+
this.plugin.log('Using AWS profile from config', { verbose: true });
73+
s3Options.credentials = fromIni({ profile: profile });
74+
}
4775

4876
if (endpoint) {
49-
this._client.config.endpoint = endpoint;
50-
}
51-
if (credentials) {
52-
this._client.config.credentials = credentials;
77+
this.plugin.log('Using endpoint from config', { verbose: true });
78+
s3Options.endpoint = endpoint;
5379
}
5480

81+
this._client = this.plugin.readConfig('s3Client') || new S3(s3Options);
5582
},
5683

5784
upload: function(options) {
5885
var client = this._client;
59-
var plugin = this._plugin;
86+
var plugin = this.plugin;
6087
var bucket = options.bucket;
6188
var acl = options.acl;
6289
var cacheControl = options.cacheControl;
@@ -112,7 +139,7 @@ module.exports = CoreObject.extend({
112139
},
113140

114141
activate: function(options) {
115-
var plugin = this._plugin;
142+
var plugin = this.plugin;
116143
var client = this._client;
117144
var bucket = options.bucket;
118145
var acl = options.acl;

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"core-object": "^3.0.0",
2424
"ember-cli-deploy-plugin": "^0.2.9",
2525
"mime-types": "^2.1.27",
26+
"proxy-agent": "^6.5.0",
2627
"rsvp": "^4.8.5"
2728
},
2829
"devDependencies": {

0 commit comments

Comments
 (0)