1
/*
2
 * Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
5
 * the License. A copy of the License is located at
6
 *
7
 *     http://aws.amazon.com/apache2.0/
8
 *
9
 * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
10
 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
11
 * and limitations under the License.
12
 */
13

14 1
import { ConsoleLogger as Logger, Parser } from '@aws-amplify/core';
15 1
import { AWSS3Provider } from './providers';
16
import { StorageProvider } from './types';
17

18 1
const logger = new Logger('StorageClass');
19

20 1
const DEFAULT_PROVIDER = 'AWSS3';
21
/**
22
 * Provide storage methods to use AWS S3
23
 */
24 1
export class Storage {
25
	/**
26
	 * @private
27
	 */
28
	private _config;
29
	private _pluggables: StorageProvider[];
30

31
	/**
32
	 * @public
33
	 */
34
	public vault: Storage;
35

36
	/**
37
	 * Initialize Storage
38
	 * @param {Object} config - Configuration object for storage
39
	 */
40
	constructor() {
41 1
		this._config = {};
42 1
		this._pluggables = [];
43 1
		logger.debug('Storage Options', this._config);
44

45 1
		this.get = this.get.bind(this);
46 1
		this.put = this.put.bind(this);
47 1
		this.remove = this.remove.bind(this);
48 1
		this.list = this.list.bind(this);
49
	}
50

51 1
	public getModuleName() {
52 1
		return 'Storage';
53
	}
54

55
	/**
56
	 * add plugin into Storage category
57
	 * @param {Object} pluggable - an instance of the plugin
58
	 */
59 1
	public addPluggable(pluggable: StorageProvider) {
60 1
		if (pluggable && pluggable.getCategory() === 'Storage') {
61 1
			this._pluggables.push(pluggable);
62 1
			let config = {};
63

64 1
			config = pluggable.configure(this._config[pluggable.getProviderName()]);
65

66 1
			return config;
67
		}
68
	}
69

70
	/**
71
	 * Get the plugin object
72
	 * @param providerName - the name of the plugin
73
	 */
74 1
	public getPluggable(providerName: string) {
75 1
		const pluggable = this._pluggables.find(
76 1
			pluggable => pluggable.getProviderName() === providerName
77
		);
78 1
		if (pluggable === undefined) {
79 1
			logger.debug('No plugin found with providerName', providerName);
80 1
			return null;
81 1
		} else return pluggable;
82
	}
83

84
	/**
85
	 * Remove the plugin object
86
	 * @param providerName - the name of the plugin
87
	 */
88 1
	public removePluggable(providerName: string) {
89 1
		this._pluggables = this._pluggables.filter(
90 1
			pluggable => pluggable.getProviderName() !== providerName
91
		);
92 1
		return;
93
	}
94

95
	/**
96
	 * Configure Storage
97
	 * @param {Object} config - Configuration object for storage
98
	 * @return {Object} - Current configuration
99
	 */
100 1
	configure(config?) {
101 1
		logger.debug('configure Storage');
102 1
		if (!config) return this._config;
103

104 1
		const amplifyConfig = Parser.parseMobilehubConfig(config);
105

106 1
		const storageKeysFromConfig = Object.keys(amplifyConfig.Storage);
107

108 1
		const storageArrayKeys = [
109
			'bucket',
110
			'region',
111
			'level',
112
			'track',
113
			'customPrefix',
114
			'serverSideEncryption',
115
			'SSECustomerAlgorithm',
116
			'SSECustomerKey',
117
			'SSECustomerKeyMD5',
118
			'SSEKMSKeyId',
119
		];
120

121 1
		const isInStorageArrayKeys = (k: string) =>
122 1
			storageArrayKeys.some(x => x === k);
123 1
		const checkConfigKeysFromArray = (k: string[]) =>
124 1
			k.find(k => isInStorageArrayKeys(k));
125

126 1
		if (
127 1
			storageKeysFromConfig &&
128
			checkConfigKeysFromArray(storageKeysFromConfig) &&
129
			!amplifyConfig.Storage[DEFAULT_PROVIDER]
130
		) {
131 1
			amplifyConfig.Storage[DEFAULT_PROVIDER] = {};
132
		}
133

134 1
		Object.entries(amplifyConfig.Storage).map(([key, value]) => {
135 1
			if (key && isInStorageArrayKeys(key) && value !== undefined) {
136 1
				amplifyConfig.Storage[DEFAULT_PROVIDER][key] = value;
137 1
				delete amplifyConfig.Storage[key];
138
			}
139
		});
140

141
		// only update new values for each provider
142 1
		Object.keys(amplifyConfig.Storage).forEach(providerName => {
143 1
			if (typeof amplifyConfig.Storage[providerName] !== 'string') {
144 1
				this._config[providerName] = {
145
					...this._config[providerName],
146
					...amplifyConfig.Storage[providerName],
147
				};
148
			}
149
		});
150

151 1
		this._pluggables.forEach(pluggable => {
152 1
			pluggable.configure(this._config[pluggable.getProviderName()]);
153
		});
154

155 1
		if (this._pluggables.length === 0) {
156 1
			this.addPluggable(new AWSS3Provider());
157
		}
158

159 1
		return this._config;
160
	}
161

162
	/**
163
	 * Get a presigned URL of the file or the object data when download:true
164
	 *
165
	 * @param {string} key - key of the object
166
	 * @param {Object} [config] - { level : private|protected|public, download: true|false }
167
	 * @return - A promise resolves to either a presigned url or the object
168
	 */
169 1
	public async get(key: string, config?): Promise<String | Object> {
170 1
		const { provider = DEFAULT_PROVIDER } = config || {};
171 1
		const prov = this._pluggables.find(
172 1
			pluggable => pluggable.getProviderName() === provider
173
		);
174 1
		if (prov === undefined) {
175 0
			logger.debug('No plugin found with providerName', provider);
176 0
			Promise.reject('No plugin found in Storage for the provider');
177
		}
178 1
		return prov.get(key, config);
179
	}
180

181
	/**
182
	 * Put a file in storage bucket specified to configure method
183
	 * @param {string} key - key of the object
184
	 * @param {Object} object - File to be put in bucket
185
	 * @param {Object} [config] - { level : private|protected|public, contentType: MIME Types,
186
	 *  progressCallback: function }
187
	 * @return - promise resolves to object on success
188
	 */
189 1
	public async put(key: string, object, config?): Promise<Object> {
190 1
		const { provider = DEFAULT_PROVIDER } = config || {};
191 1
		const prov = this._pluggables.find(
192 1
			pluggable => pluggable.getProviderName() === provider
193
		);
194 1
		if (prov === undefined) {
195 0
			logger.debug('No plugin found with providerName', provider);
196 0
			Promise.reject('No plugin found in Storage for the provider');
197
		}
198 1
		return prov.put(key, object, config);
199
	}
200

201
	/**
202
	 * Remove the object for specified key
203
	 * @param {string} key - key of the object
204
	 * @param {Object} [config] - { level : private|protected|public }
205
	 * @return - Promise resolves upon successful removal of the object
206
	 */
207 1
	public async remove(key: string, config?): Promise<any> {
208 1
		const { provider = DEFAULT_PROVIDER } = config || {};
209 1
		const prov = this._pluggables.find(
210 1
			pluggable => pluggable.getProviderName() === provider
211
		);
212 1
		if (prov === undefined) {
213 0
			logger.debug('No plugin found with providerName', provider);
214 0
			Promise.reject('No plugin found in Storage for the provider');
215
		}
216 1
		return prov.remove(key, config);
217
	}
218

219
	/**
220
	 * List bucket objects relative to the level and prefix specified
221
	 * @param {String} path - the path that contains objects
222
	 * @param {Object} [config] - { level : private|protected|public, maxKeys: NUMBER }
223
	 * @return - Promise resolves to list of keys for all objects in path
224
	 */
225 1
	public async list(path, config?): Promise<any> {
226 1
		const { provider = DEFAULT_PROVIDER } = config || {};
227 1
		const prov = this._pluggables.find(
228 1
			pluggable => pluggable.getProviderName() === provider
229
		);
230 1
		if (prov === undefined) {
231 0
			logger.debug('No plugin found with providerName', provider);
232 0
			Promise.reject('No plugin found in Storage for the provider');
233
		}
234 1
		return prov.list(path, config);
235
	}
236 1
}
237

238
/**
239
 * @deprecated use named import
240
 */
241 1
export default Storage;

Read our documentation on viewing source code .

Loading