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