-
Notifications
You must be signed in to change notification settings - Fork 47
/
blob-delete.ts
174 lines (151 loc) · 4.61 KB
/
blob-delete.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import {
BlobDeleteIfExistsResponse,
BlobDeleteOptions,
BlobDeleteResponse,
BlobServiceClient,
BlobUndeleteOptions,
BlobUndeleteResponse,
BlockBlobClient,
BlockBlobUploadOptions,
ContainerClient,
ContainerCreateOptions,
Tags
} from '@azure/storage-blob';
import * as dotenv from 'dotenv';
dotenv.config();
// Get BlobServiceClient
import { getBlobServiceClientFromDefaultAzureCredential } from './auth-get-client';
const blobServiceClient: BlobServiceClient =
getBlobServiceClientFromDefaultAzureCredential();
// <snippet_deleteBlob>
async function deleteBlob(
containerClient: ContainerClient,
blobName
): Promise<void> {
// Create blob client from container client
const blockBlobClient: BlockBlobClient =
await containerClient.getBlockBlobClient(blobName);
// include: Delete the base blob and all of its snapshots.
// only: Delete only the blob's snapshots and not the blob itself.
const options: BlobDeleteOptions = {
deleteSnapshots: 'include' // or 'only'
};
const blobDeleteResponse: BlobDeleteResponse = await blockBlobClient.delete(
options
);
if (!blobDeleteResponse.errorCode) {
console.log(`deleted blob ${blobName}`);
}
}
// </snippet_deleteBlob>
// <snippet_deleteBlobIfExists>
async function deleteBlobIfItExists(
containerClient: ContainerClient,
blobName
): Promise<void> {
// Create blob client from container client
const blockBlobClient = await containerClient.getBlockBlobClient(blobName);
// include: Delete the base blob and all of its snapshots.
// only: Delete only the blob's snapshots and not the blob itself.
const options: BlobDeleteOptions = {
deleteSnapshots: 'include' // or 'only'
};
const blobDeleteIfExistsResponse: BlobDeleteIfExistsResponse =
await blockBlobClient.deleteIfExists(options);
if (!blobDeleteIfExistsResponse.errorCode) {
console.log(`deleted blob ${blobName}`);
}
}
// </snippet_deleteBlobIfExists>
// <snippet_undeleteBlob>
async function undeleteBlob(
containerClient: ContainerClient,
blobName
): Promise<void> {
// Create blob client from container client
const blockBlobClient: BlockBlobClient =
await containerClient.getBlockBlobClient(blobName);
const options: BlobUndeleteOptions = {};
const blobUndeleteResponse: BlobUndeleteResponse =
await blockBlobClient.undelete(options);
if (!blobUndeleteResponse.errorCode) {
console.log(`undeleted blob ${blobName}`);
}
}
// </snippet_undeleteBlob>
// containerName: string
// blobName: string, includes file extension if provided
// fileContentsAsString: blob content
async function createBlobFromString(
client: ContainerClient,
blobName,
fileContentsAsString,
uploadOptions: BlockBlobUploadOptions
): Promise<void> {
// Create blob client from container client
const blockBlobClient: BlockBlobClient = await client.getBlockBlobClient(
blobName
);
console.log(`uploading blob ${blobName}`);
// Upload string
const uploadResults = await blockBlobClient.upload(
fileContentsAsString,
fileContentsAsString.length,
uploadOptions
);
// do something with blob
// ...
if (!uploadResults.errorCode) {
console.log(`Success: ${uploadResults.date}`);
}
}
async function main(blobServiceClient: BlobServiceClient): Promise<void> {
const length = 2;
const blobs = new Array(length);
// create container
const timestamp = Date.now();
const containerName = `delete-blob-${timestamp}`;
console.log(`creating container ${containerName}`);
const containerOptions: ContainerCreateOptions = {
access: 'container'
};
const { containerClient } = await blobServiceClient.createContainer(
containerName,
containerOptions
);
console.log('container creation succeeded');
// create 10 blobs with Promise.all
for (let i = 0; i < length; i++) {
const tags: Tags = {
tag1: 'value1'
};
const uploadOptions: BlockBlobUploadOptions = {
metadata: {
blobNumber: i.toString()
},
tags,
tier: 'Cool'
};
const pCreate = createBlobFromString(
containerClient,
`${containerName}-${i}.txt`,
`Hello from a string ${i}`,
uploadOptions
);
blobs.push(pCreate);
}
await Promise.all(blobs);
// delete blob
await deleteBlob(containerClient, `${containerName}-0.txt`);
// delete blob if it exists
await deleteBlobIfItExists(containerClient, `${containerName}-1.txt`);
// restore deleted blob
await undeleteBlob(containerClient, `${containerName}-1.txt`);
}
main(blobServiceClient)
.then(() => console.log('success'))
.catch((err: unknown) => {
if (err instanceof Error) {
console.log(err.message);
}
});