-
Notifications
You must be signed in to change notification settings - Fork 18
/
bucket.ts
138 lines (115 loc) · 5.01 KB
/
bucket.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
// Copyright 2016-2018, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import * as azure from "@pulumi/azure";
import * as serverless from "@pulumi/azure-serverless";
import * as cloud from "@pulumi/cloud";
import * as pulumi from "@pulumi/pulumi";
import * as stream from "stream";
import * as shared from "./shared";
import * as azStorage from "azure-storage";
import * as streamBuffers from "stream-buffers";
export class Bucket extends pulumi.ComponentResource implements cloud.Bucket {
public readonly storageAccount: azure.storage.Account;
public readonly container: azure.storage.Container;
// deployment-time api:
public onPut: (name: string, handler: cloud.BucketHandler, filter: cloud.BucketFilter | undefined) => void;
public onDelete: (name: string, handler: cloud.BucketHandler, filter: cloud.BucketFilter | undefined) => void;
// run-time api
public get: (key: string) => Promise<Buffer>;
public put: (key: string, contents: Buffer) => Promise<void>;
public delete: (key: string) => Promise<void>;
public constructor(name: string, opts?: pulumi.ResourceOptions) {
super("cloud:bucket:Bucket", name, {}, opts);
const preventDestroy = opts && opts.protect;
const resourceGroupName = shared.globalResourceGroupName;
const storageAccount = shared.getGlobalStorageAccount();
this.storageAccount = storageAccount;
const container = new azure.storage.Container(name, {
resourceGroupName: resourceGroupName,
storageAccountName: storageAccount.name,
}, { parent: this, protect: preventDestroy });
this.container = container;
this.get = async (key) => {
const writableStream = new streamBuffers.WritableStreamBuffer();
const service = new azStorage.BlobService(storageAccount.name.get());
await new Promise((resolve, reject) => {
service.getBlobToStream(container.name.get(), key, writableStream, err => {
if (err) {
return reject(err);
}
resolve();
});
});
return writableStream.getContents() || Buffer.from([]);
};
this.put = async (key, contents) => {
const service = new azStorage.BlobService(storageAccount.name.get());
const readableStream = new ReadableStream(contents);
await new Promise((resolve, reject) => {
service.createBlockBlobFromStream(container.name.get(), key, readableStream, contents.length, err => {
if (err) {
return reject(err);
}
resolve();
});
});
};
this.delete = async (key) => {
const service = new azStorage.BlobService(storageAccount.name.get());
await new Promise((resolve, reject) => {
service.deleteBlob(container.name.get(), key, (err: Error) => {
if (err) {
return reject(err);
}
resolve();
});
});
};
this.onPut = async (putName, handler, filter) => {
filter = filter || {};
serverless.storage.onBlobEvent(putName, storageAccount, {
...shared.defaultSubscriptionArgs,
func: (context, buffer) => {
handler({
key: context.bindingData.blobTrigger,
eventTime: context.bindingData.sys.utcNow,
size: buffer.length,
}).then(() => context.done());
},
storageAccount: storageAccount,
containerName: container.name,
filterPrefix: filter.keyPrefix,
filterSuffix: filter.keySuffix,
},
{ parent: this });
};
this.onDelete = async (delName, handler, filter) => {
throw new Error("@Bucket.onDelete is not yet implemented for @pulumi/cloud-azure.");
};
this.registerOutputs({
container: this.container,
});
}
}
class ReadableStream extends stream.Readable {
public constructor(private buffer: Buffer | undefined) {
super();
}
public _read() {
if (this.buffer) {
this.push(this.buffer);
this.buffer = undefined;
}
}
}