-
Notifications
You must be signed in to change notification settings - Fork 0
/
azure.ts
143 lines (133 loc) · 4.42 KB
/
azure.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import * as path from 'path';
import * as azure from 'azure-storage';
import Log from './logger';
import * as btrfs from './btrfs';
export async function listBackups(p:{ log:Log, blobService:azure.BlobService, blobContainer:string, name:string }) : Promise<{ last:btrfs.BackupEntry, lastFull?:btrfs.BackupEntry, list:btrfs.BackupEntry[] }>
{
const blobs = await listBlobs({ log:p.log, blobService:p.blobService, blobContainer:p.blobContainer });
const files = blobs.map( blob=>({ name:blob.name, size:parseInt(blob.contentLength) }) );
return btrfs.createBackupsList({ log:p.log, name:p.name, files, containerDir:'dummy' });
}
/** Either provide parameters ['path'] or ['dir'+'name'] to specify which file to upload */
export async function uploadFile(p:{ log:Log, blobService:azure.BlobService, blobContainer:string, name?:string, dir?:string, path?:string }) : Promise<azure.BlobService.BlobResult>
{
let fileName : string;
let filePath : string;
if( p.path != null )
{
fileName = path.basename( p.path );
filePath = p.path;
}
else if( p.name == null )
throw 'Missing parameter "name"';
else if( p.dir == null )
throw 'Missing parameter "dir"';
else
{
fileName = p.name;
filePath = path.join( p.dir, p.name );
}
const task = new Promise<azure.BlobService.BlobResult>( (resolve,reject)=>
{
p.log.log( `Upload file '${filePath}' (${fileName}) to Azure blob container '${p.blobContainer}'` );
p.blobService.createBlockBlobFromLocalFile( p.blobContainer, fileName, filePath, function(error,result)
{
if( error != null )
{
p.log.log( `Upload failed` );
reject( error );
return;
}
p.log.log( `Upload succeeded` );
resolve( result );
} );
} );
return task;
}
export async function downloadFile(p:{ log:Log, blobService:azure.BlobService, blobContainer:string, name:string, dir:string }) : Promise<azure.BlobService.BlobResult>
{
const outputPath = path.join( p.dir, p.name );
p.log.log( `Downloading file '${outputPath}'` );
return new Promise<azure.BlobService.BlobResult>( (resolve,reject)=>
{
p.blobService.getBlobToLocalFile(p.blobContainer, p.name, outputPath, (error,result)=>
{
if( error )
{
p.log.log( `Download failed` );
reject( error );
return;
}
p.log.log( `Download succeeded` );
resolve( result );
} );
} );
}
export async function listBlobs(p:{ log:Log, blobService:azure.BlobService, blobContainer:string, namePrefix?:string }) : Promise<azure.BlobService.BlobResult[]>
{
let entries : azure.BlobService.BlobResult[] = [];
let token : azure.common.ContinuationToken|undefined = undefined!;
p.log.log( `List blobs of container '${p.blobContainer}'` );
do
{
let task : Promise<azure.BlobService.ListBlobsResult>;
if( p.namePrefix == null )
{
// Use 'listBlobsSegmented'
task = new Promise<azure.BlobService.ListBlobsResult>( (resolve,reject)=>
{
p.blobService.listBlobsSegmented( p.blobContainer, token!, {}, (error,result)=>
{
if( error )
{
reject( error );
return;
}
resolve( result );
} );
} );
}
else
{
// Use 'listBlobsSegmentedWithPrefix'
task = new Promise<azure.BlobService.ListBlobsResult>( (resolve,reject)=>
{
p.blobService.listBlobsSegmentedWithPrefix( p.blobContainer, p.namePrefix!, token!, {}, (error,result)=>
{
if( error )
{
reject( error );
return;
}
resolve( result );
} );
} );
}
const rv = await task;
p.log.log( `Request returned '${rv.entries.length}' entries` );
entries = entries.concat( rv.entries );
token = rv.continuationToken;
}
while( token != null );
p.log.log( `Total: '${entries.length}' entries` );
return entries;
}
export async function deleteBlob(p:{ log:Log, blobService:azure.BlobService, blobContainer:string, name:string }) : Promise<boolean>
{
p.log.log( `Delete blob '${p.name}' from container '${p.blobContainer}'` );
const task = new Promise<boolean>( (resolve,reject)=>
{
p.blobService.deleteBlobIfExists( p.blobContainer, p.name, function(error,result)
{
if( error != null )
{
reject( error );
return;
}
resolve( result );
} );
} );
const rv = await task;
p.log.log( rv ? `Delete succeeded` : 'Delete failed (file does not exist)' );
return rv;
}