azureStorageClientUtils.ts 9.39 KB
Newer Older
SparkSnail's avatar
SparkSnail committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
 * Copyright (c) Microsoft Corporation
 * All rights reserved.
 *
 * MIT License
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
 * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

'use strict';

22
23
import * as azureStorage from 'azure-storage';
import * as fs from 'fs';
SparkSnail's avatar
SparkSnail committed
24
25
import * as path from 'path';
import { Deferred } from 'ts-deferred';
26
import { String } from 'typescript-string-operations';
SparkSnail's avatar
SparkSnail committed
27
28
29
import { getLogger } from '../../common/log';
import { mkDirP } from '../../common/utils';

30
// tslint:disable: no-redundant-jsdoc no-any no-unsafe-any
SparkSnail's avatar
SparkSnail committed
31
export namespace AzureStorageClientUtility {
32

SparkSnail's avatar
SparkSnail committed
33
34
    /**
     * create azure share
35
36
     * @param fileServerClient
     * @param azureShare
SparkSnail's avatar
SparkSnail committed
37
     */
38
    export async function createShare(fileServerClient: any, azureShare: any): Promise<void> {
SparkSnail's avatar
SparkSnail committed
39
        const deferred: Deferred<void> = new Deferred<void>();
40
41
42
43
44
45
46
        fileServerClient.createShareIfNotExists(azureShare, (error: any, result: any, response: any) => {
            if (error) {
                getLogger()
                  .error(`Create share failed:, ${error}`);
                deferred.reject(error);
            } else {
                deferred.resolve();
SparkSnail's avatar
SparkSnail committed
47
            }
48
49
        });

SparkSnail's avatar
SparkSnail committed
50
51
        return deferred.promise;
    }
52

SparkSnail's avatar
SparkSnail committed
53
54
    /**
     * Create a new directory (NOT recursively) in azure file storage.
55
56
57
     * @param fileServerClient
     * @param azureFoler
     * @param azureShare
SparkSnail's avatar
SparkSnail committed
58
     */
59
    export async function createDirectory(fileServerClient: azureStorage.FileService, azureFoler: any, azureShare: any): Promise<void> {
SparkSnail's avatar
SparkSnail committed
60
        const deferred: Deferred<void> = new Deferred<void>();
61
62
63
64
        fileServerClient.createDirectoryIfNotExists(azureShare, azureFoler, (error: any, result: any, response: any) => {
            if (error) {
                getLogger()
                  .error(`Create directory failed:, ${error}`);
SparkSnail's avatar
SparkSnail committed
65
                deferred.reject(error);
66
            } else {
SparkSnail's avatar
SparkSnail committed
67
68
                deferred.resolve();
            }
69
70
        });

SparkSnail's avatar
SparkSnail committed
71
72
73
74
75
76
        return deferred.promise;
    }

    /**
     * Create a new directory recursively in azure file storage
     * @param fileServerClient
77
     * @param azureDirectory
SparkSnail's avatar
SparkSnail committed
78
     */
79
80
    export async function createDirectoryRecursive(fileServerClient: azureStorage.FileService, azureDirectory: string,
                                                   azureShare: any): Promise<void> {
SparkSnail's avatar
SparkSnail committed
81
        const deferred: Deferred<void> = new Deferred<void>();
82
83
84
        const directories: string[] = azureDirectory.split('/');
        let rootDirectory: string = '';
        for (const directory of directories) {
SparkSnail's avatar
SparkSnail committed
85
86
87
88
89
            rootDirectory += directory;
            await createDirectory(fileServerClient, rootDirectory, azureShare);
            rootDirectory += '/';
        }
        deferred.resolve();
90

SparkSnail's avatar
SparkSnail committed
91
92
        return deferred.promise;
    }
93

SparkSnail's avatar
SparkSnail committed
94
95
    /**
     * upload a file to azure storage
96
97
98
99
100
     * @param fileServerClient
     * @param azureDirectory
     * @param azureFileName
     * @param azureShare
     * @param localFilePath
SparkSnail's avatar
SparkSnail committed
101
     */
102
103
    async function uploadFileToAzure(fileServerClient: any, azureDirectory: string, azureFileName: any, azureShare: any,
                                     localFilePath: string): Promise<void> {
SparkSnail's avatar
SparkSnail committed
104
        const deferred: Deferred<void> = new Deferred<void>();
105
106
107
108
109
        await fileServerClient.createFileFromLocalFile(azureShare, azureDirectory, azureFileName, localFilePath,
                                                       (error: any, result: any, response: any) => {
            if (error) {
                getLogger()
                  .error(`Upload file failed:, ${error}`);
SparkSnail's avatar
SparkSnail committed
110
                deferred.reject(error);
111
            } else {
SparkSnail's avatar
SparkSnail committed
112
113
                deferred.resolve();
            }
114
115
        });

SparkSnail's avatar
SparkSnail committed
116
117
        return deferred.promise;
    }
118

SparkSnail's avatar
SparkSnail committed
119
120
    /**
     * download a file from azure storage
121
122
123
124
125
     * @param fileServerClient
     * @param azureDirectory
     * @param azureFileName
     * @param azureShare
     * @param localFilePath
SparkSnail's avatar
SparkSnail committed
126
     */
127
128
    async function downloadFile(fileServerClient: any, azureDirectory: string, azureFileName: any, azureShare: any,
                                localFilePath: string): Promise<void> {
SparkSnail's avatar
SparkSnail committed
129
        const deferred: Deferred<void> = new Deferred<void>();
130
131
132
133
134
135
        // tslint:disable-next-line:non-literal-fs-path
        await fileServerClient.getFileToStream(azureShare, azureDirectory, azureFileName, fs.createWriteStream(localFilePath),
                                               (error: any, result: any, response: any) => {
            if (error) {
                getLogger()
                  .error(`Download file failed:, ${error}`);
SparkSnail's avatar
SparkSnail committed
136
                deferred.reject(error);
137
            } else {
138
                deferred.resolve();
SparkSnail's avatar
SparkSnail committed
139
            }
140
141
        });

SparkSnail's avatar
SparkSnail committed
142
143
144
145
146
147
148
149
150
151
        return deferred.promise;
    }

    /**
     * Upload a directory to azure file storage
     * @param fileServerClient : the client of file server
     * @param azureDirectory : the directory in azure file storage
     * @param azureShare : the azure share used
     * @param localDirectory : local directory to be uploaded
     */
152
153
154
    // tslint:disable:non-literal-fs-path
    export async function uploadDirectory(fileServerClient: azureStorage.FileService, azureDirectory: string, azureShare: any,
                                          localDirectory: string): Promise<void> {
SparkSnail's avatar
SparkSnail committed
155
156
157
        const deferred: Deferred<void> = new Deferred<void>();
        const fileNameArray: string[] = fs.readdirSync(localDirectory);
        await createDirectoryRecursive(fileServerClient, azureDirectory, azureShare);
158
        for (const fileName of fileNameArray) {
SparkSnail's avatar
SparkSnail committed
159
160
            const fullFilePath: string = path.join(localDirectory, fileName);
            try {
161
162
                if (fs.lstatSync(fullFilePath)
                      .isFile()) {
SparkSnail's avatar
SparkSnail committed
163
164
165
                    await uploadFileToAzure(fileServerClient, azureDirectory, fileName, azureShare, fullFilePath);
                } else {
                    // If filePath is a directory, recuisively copy it to azure
166
                    await uploadDirectory(fileServerClient, String.Format('{0}/{1}', azureDirectory, fileName), azureShare, fullFilePath);
SparkSnail's avatar
SparkSnail committed
167
                }
168
            } catch (error) {
SparkSnail's avatar
SparkSnail committed
169
                deferred.reject(error);
170

SparkSnail's avatar
SparkSnail committed
171
172
173
174
175
                return deferred.promise;
            }
        }
        // All files/directories are copied successfully, resolve
        deferred.resolve();
176

SparkSnail's avatar
SparkSnail committed
177
178
        return deferred.promise;
    }
179

SparkSnail's avatar
SparkSnail committed
180
181
    /**
     * downlod a directory from azure
182
183
184
185
     * @param fileServerClient
     * @param azureDirectory
     * @param azureShare
     * @param localDirectory
SparkSnail's avatar
SparkSnail committed
186
     */
187
188
    export async function downloadDirectory(fileServerClient: any, azureDirectory: string, azureShare: any, localDirectory: string):
     Promise<void> {
SparkSnail's avatar
SparkSnail committed
189
        const deferred: Deferred<void> = new Deferred<void>();
190
191
192
193
194
195
        await mkDirP(localDirectory);
        fileServerClient.listFilesAndDirectoriesSegmented(azureShare, azureDirectory, 'null',
                                                          async (error: any, result: any, response: any) => {
            if (('entries' in result) === false) {
                getLogger()
                  .error(`list files failed, can't get entries in result`);
SparkSnail's avatar
SparkSnail committed
196
197
198
                throw new Error(`list files failed, can't get entries in result`);
            }

199
200
201
            if (('files' in result.entries) === false) {
                getLogger()
                  .error(`list files failed, can't get files in result['entries']`);
SparkSnail's avatar
SparkSnail committed
202
203
204
                throw new Error(`list files failed, can't get files in result['entries']`);
            }

205
206
207
            if (('directories' in result.directories) === false) {
                getLogger()
                  .error(`list files failed, can't get directories in result['entries']`);
SparkSnail's avatar
SparkSnail committed
208
209
210
                throw new Error(`list files failed, can't get directories in result['entries']`);
            }

211
            for (const fileName of result.entries.files) {
SparkSnail's avatar
SparkSnail committed
212
                const fullFilePath: string = path.join(localDirectory, fileName.name);
213
                await downloadFile(fileServerClient, azureDirectory, fileName.name, azureShare, fullFilePath);
SparkSnail's avatar
SparkSnail committed
214
            }
215

216
217
218
219
            for (const directoryName of result.entries.directories) {
                const fullDirectoryPath: string = path.join(localDirectory, directoryName.name);
                const fullAzureDirectory: string = path.join(azureDirectory, directoryName.name);
                await downloadDirectory(fileServerClient, fullAzureDirectory, azureShare, fullDirectoryPath);
SparkSnail's avatar
SparkSnail committed
220
221
            }
            deferred.resolve();
222
223
        });

SparkSnail's avatar
SparkSnail committed
224
225
226
        return deferred.promise;
    }
}
227
// tslint:enable: no-redundant-jsdoc no-any no-unsafe-any