1 | 'use strict';
|
2 |
|
3 | const osLocale = require('os-locale');
|
4 | const MNSClient = require('@alicloud/mns');
|
5 | const hashedMachineId = require('node-machine-id').machineId;
|
6 | const pkg = require('../package.json');
|
7 | const CloudAPI = require('@alicloud/cloudapi');
|
8 | const TableStore = require('tablestore');
|
9 | const Log = require('@alicloud/log');
|
10 | const FC = require('@alicloud/fc2');
|
11 | const Pop = require('@alicloud/pop-core');
|
12 | const getProfile = require('./profile').getProfile;
|
13 | const OSS = require('ali-oss');
|
14 | const debug = require('debug');
|
15 |
|
16 | const getRosClient = async () => {
|
17 | return await getPopClient('http://ros.aliyuncs.com', '2019-09-10');
|
18 | };
|
19 |
|
20 | const getOssClient = async (bucket) => {
|
21 | const profile = await getProfile();
|
22 |
|
23 | const location = await OSS({
|
24 | accessKeyId: profile.accessKeyId,
|
25 | accessKeySecret: profile.accessKeySecret,
|
26 | bucket,
|
27 | region: 'oss-' + profile.defaultRegion
|
28 | }).getBucketLocation(bucket);
|
29 |
|
30 | debug('use bucket region %s', location.location);
|
31 |
|
32 | const client = OSS({
|
33 | accessKeyId: profile.accessKeyId,
|
34 | accessKeySecret: profile.accessKeySecret,
|
35 | bucket,
|
36 | region: location.location
|
37 | });
|
38 |
|
39 | return client;
|
40 | };
|
41 |
|
42 | const getFcClient = async (opts = {}) => {
|
43 | const profile = await getProfile();
|
44 |
|
45 | const locale = await osLocale();
|
46 |
|
47 | const mid = await hashedMachineId();
|
48 |
|
49 | FC.prototype.getAccountSettings = function(options = {}, headers = {}) {
|
50 | return this.get('/account-settings', options, headers);
|
51 | };
|
52 |
|
53 | const fc = new FC(profile.accountId, {
|
54 | accessKeyID: profile.accessKeyId,
|
55 | accessKeySecret: profile.accessKeySecret,
|
56 | endpoint: profile.fcEndpoint,
|
57 | region: profile.defaultRegion,
|
58 | timeout: opts.timeout || profile.timeout * 1000,
|
59 | secure: profile.protocol !== 'http',
|
60 | headers: {
|
61 | 'user-agent': `${pkg.name}/v${pkg.version} ( Node.js ${process.version}; OS ${process.platform} ${process.arch}; language ${locale}; mid ${mid})`
|
62 | }
|
63 | });
|
64 |
|
65 | return fc;
|
66 | };
|
67 |
|
68 | const getPopClient = async (endpoint, apiVersion) => {
|
69 | const profile = await getProfile();
|
70 |
|
71 | return new Pop({
|
72 | endpoint: endpoint,
|
73 | apiVersion: apiVersion,
|
74 | accessKeyId: profile.accessKeyId,
|
75 | accessKeySecret: profile.accessKeySecret,
|
76 | opts: {
|
77 | timeout: profile.timeout * 1000
|
78 | }
|
79 | });
|
80 | };
|
81 |
|
82 | const getOtsPopClient = async () => {
|
83 | const profile = await getProfile();
|
84 |
|
85 | return await getPopClient(`http://ots.${profile.defaultRegion}.aliyuncs.com`, '2016-06-20');
|
86 | };
|
87 |
|
88 | const getVpcPopClient = async () => {
|
89 | return await getPopClient('https://vpc.aliyuncs.com', '2016-04-28');
|
90 | };
|
91 |
|
92 | const getEcsPopClient = async () => {
|
93 | return await getPopClient('https://ecs.aliyuncs.com', '2014-05-26');
|
94 | };
|
95 |
|
96 | const getNasPopClient = async () => {
|
97 |
|
98 | const profile = await getProfile();
|
99 |
|
100 | return await getPopClient(`http://nas.${profile.defaultRegion}.aliyuncs.com`, '2017-06-26');
|
101 | };
|
102 |
|
103 | const getOtsClient = async (instanceName) => {
|
104 | const profile = await getProfile();
|
105 |
|
106 | var endpoint = `http://${instanceName}.${profile.defaultRegion}.ots.aliyuncs.com`;
|
107 | return new TableStore.Client({
|
108 | accessKeyId: profile.accessKeyId,
|
109 | secretAccessKey: profile.accessKeySecret,
|
110 | endpoint: endpoint,
|
111 | instancename: instanceName
|
112 | });
|
113 | };
|
114 |
|
115 | const getMnsClient = async (topicName, region) => {
|
116 | const profile = await getProfile();
|
117 |
|
118 | return new MNSClient(profile.accountId, {
|
119 | region: region,
|
120 | accessKeyId: profile.accessKeyId,
|
121 | accessKeySecret: profile.accessKeySecret,
|
122 |
|
123 | secure: false,
|
124 | internal: false,
|
125 | vpc: false
|
126 | });
|
127 | };
|
128 |
|
129 | const getCloudApiClient = async () => {
|
130 | const profile = await getProfile();
|
131 |
|
132 | return new CloudAPI({
|
133 | accessKeyId: profile.accessKeyId,
|
134 | accessKeySecret: profile.accessKeySecret,
|
135 | endpoint: `http://apigateway.${profile.defaultRegion}.aliyuncs.com`,
|
136 | opts: {
|
137 | timeout: profile.timeout * 1000
|
138 | }
|
139 | });
|
140 | };
|
141 |
|
142 | const getSlsClient = async () => {
|
143 | const profile = await getProfile();
|
144 |
|
145 | return new Log({
|
146 | region: profile.defaultRegion,
|
147 | accessKeyId: profile.accessKeyId,
|
148 | accessKeySecret: profile.accessKeySecret
|
149 | });
|
150 | };
|
151 |
|
152 | module.exports = {
|
153 | getFcClient,
|
154 | getOtsClient,
|
155 | getOtsPopClient,
|
156 | getMnsClient,
|
157 | getCloudApiClient,
|
158 | getSlsClient,
|
159 | getPopClient,
|
160 | getVpcPopClient,
|
161 | getEcsPopClient,
|
162 | getNasPopClient,
|
163 | getOssClient,
|
164 | getRosClient
|
165 | }; |
\ | No newline at end of file |