forked from Azure/azure-event-hubs-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
azureBlob.ts
105 lines (86 loc) · 4.12 KB
/
azureBlob.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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
import { BlobService, CreateContainerResult } from "./blobService";
import { BlobService as StorageBlobService } from "azure-storage";
import { Dictionary } from "@azure/event-hubs";
/**
* @ignore
*/
export class AzureBlob {
private _blobService: BlobService;
private _containerName: string;
private _blobPath: string;
private _containerAndBlobExist: boolean = false;
constructor(hostName: string, connectionString: string, containerName: string,
blob: string, blobService?: BlobService) {
this._blobPath = blob;
this._containerName = containerName;
this._blobService = blobService || BlobService.create(hostName, connectionString);
}
async ensureContainerAndBlobExist(): Promise<void> {
try {
if (!this._containerAndBlobExist) {
await this._blobService.ensureContainerAndBlobExist(this._containerName, this._blobPath);
this._containerAndBlobExist = true;
}
} catch (err) {
const msg = `An error occurred while ensuring that the container and blob exists. ` +
`It is: \n${err ? err.stack : JSON.stringify(err)}`;
throw new Error(msg);
}
}
async ensureContainerExists(): Promise<CreateContainerResult> {
return await this._blobService.ensureContainerExists(this._containerName);
}
async doesContainerExist(): Promise<boolean> {
return await this._blobService.doesContainerExist(this._containerName);
}
async doesBlobExist(): Promise<boolean> {
return await this._blobService.doesBlobExist(this._containerName, this._blobPath);
}
async ensureBlobExists(text: string): Promise<void> {
return await this._blobService.ensureBlobExists(this._containerName, this._blobPath, text);
}
async renewLease(leaseId: string,
options: StorageBlobService.LeaseRequestOptions): Promise<StorageBlobService.LeaseResult> {
return await this._blobService.renewLease(this._containerName, this._blobPath, leaseId, options);
}
async releaseLease(leaseId: string,
options?: StorageBlobService.LeaseRequestOptions): Promise<StorageBlobService.LeaseResult> {
return await this._blobService.releaseLease(this._containerName, this._blobPath, leaseId, options);
}
async updateContent(text: string,
options?: StorageBlobService.CreateBlobRequestOptions): Promise<StorageBlobService.BlobResult> {
return await this._blobService.updateContent(this._containerName, this._blobPath, text, options);
}
async getContent(options?: StorageBlobService.GetBlobRequestOptions): Promise<string> {
return await this._blobService.getContent(this._containerName, this._blobPath, options);
}
async changeLease(currentLeaseId: string,
proposedLeaseId: string): Promise<StorageBlobService.LeaseResult> {
return await this._blobService.changeLease(this._containerName,
this._blobPath, currentLeaseId, proposedLeaseId);
}
async getBlobProperties(): Promise<StorageBlobService.BlobResult> {
return await this._blobService.getBlobProperties(this._containerName, this._blobPath);
}
async getBlobMetadata(): Promise<StorageBlobService.BlobResult> {
return await this._blobService.getBlobMetadata(this._containerName, this._blobPath);
}
async setBlobMetadata(metadata: Dictionary<string>,
options?: StorageBlobService.BlobRequestOptions): Promise<StorageBlobService.BlobResult> {
return await this._blobService.setBlobMetadata(this._containerName, this._blobPath,
metadata, options);
}
async listBlobsSegmented(options?: StorageBlobService.ListBlobsSegmentedRequestOptions):
Promise<StorageBlobService.ListBlobsResult> {
return await this._blobService.listBlobsSegmented(this._containerName, options);
}
async acquireLease(options: StorageBlobService.AcquireLeaseRequestOptions):
Promise<StorageBlobService.LeaseResult> {
return await this._blobService.acquireLease(this._containerName, this._blobPath, options);
}
async deleteBlobIfExists(): Promise<void> {
return await this._blobService.deleteBlobIfExists(this._containerName, this._blobPath);
}
}