-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathanonymousAuth.js
More file actions
46 lines (35 loc) · 1.41 KB
/
anonymousAuth.js
File metadata and controls
46 lines (35 loc) · 1.41 KB
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
/**
* @summary authenticate anonymously using a SAS-encoded URL
*/
const { BlobServiceClient, AnonymousCredential } = require("@azure/storage-blob");
// Load the .env file if it exists
require("dotenv/config");
async function main() {
// Enter your storage account name and SAS
const account = process.env.ACCOUNT_NAME || "<account name>";
const accountSas = process.env.ACCOUNT_SAS || "<account SAS>";
// List containers
const blobServiceClient = new BlobServiceClient(
// When using AnonymousCredential, following url should include a valid SAS or support public access
`https://${account}.blob.core.windows.net${accountSas}`,
new AnonymousCredential(),
);
console.log("Containers:");
for await (const container of blobServiceClient.listContainers()) {
console.log(`- ${container.name}`);
}
// Create a container
const containerName = `newcontainer${new Date().getTime()}`;
const containerClient = blobServiceClient.getContainerClient(containerName);
const createContainerResponse = await containerClient.create();
console.log(`Created container ${containerName} successfully`, createContainerResponse.requestId);
// Delete container
await containerClient.delete();
console.log("Deleted container:", containerClient.containerName);
}
main().catch((error) => {
console.error(error);
process.exit(1);
});