简体   繁体   中英

Where to put the Azure Storage connection string in a Node app that uses a config file rather than environment variables?

I need to access Azure Storage's BLOB storage to insert some JSON. However, in the official documentation, in order to first connect with my database, I need the connection string to be recognized by Azure. Traditionally. Azure would look at the environment variables for the connection string, but in this app, we're using a config file with the keys laid out as such...

module.exports = {
  AZURE_STORAGE_CONNECTION_STRING: AZURE_STORAGE_CONNECTION_STRING,
};

It does not appear that Azure recognizes the keys in this way. Rather than making an environment file for this one string, is it possible to pass the string in elsewhere? Say when we first call

const storage = require('azure-storage');
const blobService = storage.createBlobService();

Or possibly when the storage method is used?

   const uploadToBlob = async (containerName, filePath) => {

        await blobService.createBlockBlobFromLocalFile(containerName, blobName, fullPath, err => {

Thanks for your time peeps!

The answer was super simple. The credentials key does not need to be in a .env file at all, but can be kept in the config folder and handed to Azure when the blob service is first instantiated.

const keys = require('../config/keys');
const blobService = storage.createBlobService(keys.AZURE_STORAGE_CONNECTION_STRING);

Based on the documentation, you could use a .env file which would include the connection string:

you must provide the connection string for your storage account. The sample repository includes a file named .env.example. You can rename this file by removing the .example extension, which results in a file named .env. Inside the .env file, add your connection string value after the AZURE_STORAGE_CONNECTION_STRING key.

I'd recommend checking the code samples in this repo

The format that's supported is:

AZURE_STORAGE_CONNECTION_STRING=<replace with your storage connection string>

Most likely you could do some string manipulation to substitute the ":" to a =, then pass it to a variable for use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM