Skip to content

Commit 5389817

Browse files
📝 Add docstrings to feat/add-azure-storage-connector
Docstrings generation was requested by @yogeshselvarajan. * #126 (comment) The following files were modified: * `examples/06-Storage-no-agent/03-AzureBlobStorage.ts`
1 parent a60550a commit 5389817

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import 'dotenv/config';
2+
import { Storage } from '@smythos/sdk';
3+
4+
/**
5+
* Writes a test blob to Azure Blob Storage, reads it back, and verifies the content matches.
6+
*
7+
* Validates AZURE_STORAGE_ACCOUNT_NAME and AZURE_STORAGE_ACCESS_KEY from environment variables and uses
8+
* AZURE_BLOB_CONTAINER_NAME as the target container. If credentials are missing, the function logs an error and returns early.
9+
*
10+
* @returns A promise that resolves when the example completes.
11+
* @throws Error If the read content does not match the written content.
12+
*/
13+
async function main() {
14+
// Ensure environment variables are loaded
15+
if (!process.env.AZURE_STORAGE_ACCOUNT_NAME || !process.env.AZURE_STORAGE_ACCESS_KEY) {
16+
console.error("Error: Missing Azure credentials in your .env file.");
17+
return;
18+
}
19+
20+
// Initialize the Azure Blob Storage connector with credentials from the .env file
21+
const azureStorage = Storage.AzureBlobStorage({
22+
storageAccountName: process.env.AZURE_STORAGE_ACCOUNT_NAME,
23+
storageAccountAccessKey: process.env.AZURE_STORAGE_ACCESS_KEY,
24+
blobContainerName: process.env.AZURE_BLOB_CONTAINER_NAME!,
25+
});
26+
27+
const resourceId = 'smythos-azure-test.txt';
28+
const content = 'Hello, world from Azure!';
29+
30+
console.log(`=== Running Azure Blob Storage Example ===`);
31+
32+
// Write a file to your container.
33+
console.log(`Writing "${content}" to "${resourceId}"...`);
34+
await azureStorage.write(resourceId, content);
35+
console.log('Write operation complete.');
36+
37+
// Read the file back.
38+
console.log(`Reading "${resourceId}"...`);
39+
const data = await azureStorage.read(resourceId);
40+
console.log('Read operation complete.');
41+
42+
// Log the content to verify it's correct.
43+
const dataAsString = data?.toString();
44+
console.log(`Content read from blob: "${dataAsString}"`);
45+
46+
if (dataAsString !== content) {
47+
throw new Error("Verification failed: Read content does not match written content!");
48+
}
49+
50+
console.log('Verification successful!');
51+
console.log(`=== Example Finished ===`);
52+
}
53+
54+
main().catch(error => {
55+
console.error("An error occurred:", error.message);
56+
});

0 commit comments

Comments
 (0)