gpt4 book ai didi

node.js - Azure Function Linux Nodejs 和 @azure/storage-blob 给出错误 : Cannot read property 'startsWith' of undefined

转载 作者:行者123 更新时间:2023-12-03 06:10:05 25 4
gpt4 key购买 nike

我正在本地测试我的 Azure Nodejs Linux 功能,代码如下:

import { BlobServiceClient } from "@azure/storage-blob";

const connectionString = process.env[
"AZURE_STORAGE_CONNECTION_STRING"
] as string;

export const blobServiceClient =
BlobServiceClient.fromConnectionString(connectionString);

正在生成此错误消息:

john@MacBook-Pro:~/myApp/code/seal/azure_function$ rm -rf dist/ && npm run-script build &&  func start 

> <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dcbda6a9aeb983baa9b2bfa8b5b3b29cedf2ecf2ec" rel="noreferrer noopener nofollow">[email protected]</a> build /Users/john/myApp/code/seal/azure_function
> tsc && tsc-alias


Azure Functions Core Tools
Core Tools Version: 4.0.5198 Commit hash: N/A (64-bit)
Function Runtime Version: 4.21.1.20667


Functions:

sealworker: [GET,POST] http://localhost:7071/api/sealworker

For detailed output, run func with --verbose flag.
[2023-08-15T09:06:03.440Z] Worker was unable to load entry point "dist/sealworker/index.js": Cannot read property 'startsWith' of undefined
[2023-08-15T09:06:03.551Z] Worker process started and initialized.
[2023-08-15T09:06:03.564Z] Worker was unable to load function sealworker: 'Cannot read property 'startsWith' of undefined'
[2023-08-15T09:06:03.568Z] Worker failed to load function: 'sealworker' with functionId: 'c146c9d1-05ef-4aa2-bb18-d436186b7f65'.
[2023-08-15T09:06:03.568Z] Result: Failure
[2023-08-15T09:06:03.568Z] Exception: Worker was unable to load function sealworker: 'Cannot read property 'startsWith' of undefined'
[2023-08-15T09:06:03.568Z] Stack: TypeError: Worker was unable to load function sealworker: 'Cannot read property 'startsWith' of undefined'
[2023-08-15T09:06:03.568Z] at extractConnectionStringParts (/Users/john/myApp/code/seal/azure_function/node_modules/@azure/storage-blob/dist/index.js:13654:26)
[2023-08-15T09:06:03.568Z] at Function.fromConnectionString (/Users/john/myApp/code/seal/azure_function/node_modules/@azure/storage-blob/dist/index.js:24328:32)
[2023-08-15T09:06:03.569Z] at Object.<anonymous> (/Users/john/myApp/code/seal/azure_function/dist/sealworker/utils/Azure/blobServiceClient.js:6:62)
[2023-08-15T09:06:03.569Z] at Module._compile (internal/modules/cjs/loader.js:1068:30)
[2023-08-15T09:06:03.569Z] at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
[2023-08-15T09:06:03.569Z] at Module.load (internal/modules/cjs/loader.js:933:32)
[2023-08-15T09:06:03.569Z] at Function.Module._load (internal/modules/cjs/loader.js:774:14)
[2023-08-15T09:06:03.569Z] at Module.require (internal/modules/cjs/loader.js:957:19)
[2023-08-15T09:06:03.569Z] at Object.apply (/usr/local/Cellar/azure-functions-core-tools@4/4.0.5198/workers/node/dist/src/worker-bundle.js:2:44135)
[2023-08-15T09:06:03.569Z] at require (internal/modules/cjs/helpers.js:88:18)
[2023-08-15T09:06:03.569Z] at Object.<anonymous> (/Users/john/myApp/code/seal/azure_function/dist/sealworker/services/attachment.js:58:29)
[2023-08-15T09:06:03.571Z] at Module._compile (internal/modules/cjs/loader.js:1068:30)
[2023-08-15T09:06:03.571Z] at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
[2023-08-15T09:06:03.571Z] at Module.load (internal/modules/cjs/loader.js:933:32)
[2023-08-15T09:06:03.571Z] at Function.Module._load (internal/modules/cjs/loader.js:774:14)
[2023-08-15T09:06:03.571Z] at Module.require (internal/modules/cjs/loader.js:957:19).
[2023-08-15T09:06:03.785Z] Host lock lease acquired by instance ID '000000000000000000000000DB4F1B81'.
^C[2023-08-15T09:34:22.578Z] Language Worker Process exited. Pid=53985.
[2023-08-15T09:34:22.578Z] node exited with code 130 (0x82). .

connectionString 定义为字符串:

const connectionString = process.env[
"AZURE_STORAGE_CONNECTION_STRING"
] as string;

但是提示是无法读取未定义的属性“startsWith”

不知道我现在应该做什么...有什么提示吗?

最佳答案

为了在 Http 触发器中使用 azure blob 包,请使用以下代码:-

Typescript 函数 Http 触发器:-

index.ts:-

import { AzureFunction, Context, HttpRequest } from "@azure/functions";
import { BlobServiceClient } from "@azure/storage-blob";

const httpTrigger: AzureFunction = async function (
context: Context,
req: HttpRequest
): Promise<void> {
context.log("HTTP trigger function processed a request.");

const connectionString = process.env[
"AZURE_STORAGE_CONNECTION_STRING"
] as string;

const blobServiceClient =
BlobServiceClient.fromConnectionString(connectionString);

// Your code here

context.res = {
// Your response here
};
};

export default httpTrigger;

我的 local.settings.json:-

{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=<strgaccountname>;AccountKey=xxxxxxKImyt8HTQ6Dp3zVaDMTexxxUwASRw==;EndpointSuffix=core.windows.net",
"FUNCTIONS_WORKER_RUNTIME": "node",
"kamstore_STORAGE": "DefaultEndpointsProtocol=https;AccountName=<strgaccountname>;AccountKey=xxxxzVaDMTelk/ae+AStUwASRw==;EndpointSuffix=core.windows.net",
"AZURE_STORAGE_CONNECTION_STRING": "DefaultEndpointsProtocol=https;AccountName=<strgaccountname>;AccountKey=xxxxHTQ6Dp3zVaDMTelk/ae+AStUwASRw==;EndpointSuffix=core.windows.net"
}
}

输出:-

本地:-

enter image description here

enter image description here

另一个选项是使用以下命令在终端中设置设置:-

export  AZURE_STORAGE_CONNECTION_STRING="<your_connection_string>"

在您的函数应用程序中,您可以在配置选项卡中添加此设置,如下所示:-

enter image description here

引用 - Get started with Azure Blob Storage and TypeScript - Azure Storage | Microsoft Learn

或者您可以使用blob trigger1blob trigger2使用 Typescript,当在存储帐户中添加新的 blob 时会触发 azure 函数并执行处理。

nodejs Linux 功能应用程序中的门户:-

enter image description here

HttpTrigger:-

enter image description here

BlobTrigger:-

enter image description here

关于node.js - Azure Function Linux Nodejs 和 @azure/storage-blob 给出错误 : Cannot read property 'startsWith' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76905034/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com