VS Code中Azure Function报错:inputStream应位于流起始位置
问题:Blob触发Azure Function在VS Code中报错"inputStream should be at start of stream"
我开发了一个Blob触发的Azure Function,功能是对入站容器接收的文件进行PGP加密,再将加密后的文件放入出站容器。该函数在Visual Studio 2022本地运行正常,但在VS Code中复制相同代码后,抛出错误:
inputStream should be at start of stream.
函数代码:
using System; using System.IO; using System.Text; using System.Threading.Tasks; using Azure.Identity; using Azure.Security.KeyVault.Secrets; using Microsoft.Azure.WebJobs; using Microsoft.Extensions.Logging; using Azure.Storage.Blobs; using Azure.Storage.Blobs.Models; using PgpCore; public class Function1 { private readonly string keyVaultName = "MyKV"; private readonly string keyName = "SKEY"; private readonly string outboundContainerName = "OutBound"; [FunctionName("BlobTriggerFunction")] public async Task Run( [BlobTrigger("Inbound/{name}.txt", Connection = "Devsandpit")] Stream inblobStream, string name, ILogger log) { log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {inblobStream.Length} Bytes"); // Get PGP public key from Azure Key Vault var client = new SecretClient(new Uri($"https://{keyVaultName}.vault.azure.net/"), new DefaultAzureCredential()); KeyVaultSecret secret = await client.GetSecretAsync(keyName); string publicKey = secret.Value; // Decode the Base64 encoded public key byte[] publicKeyBytes = Convert.FromBase64String(publicKey); // Using MemoryStream for the public key using (MemoryStream publicKeyStream = new MemoryStream(publicKeyBytes)) using (Stream outputFileStream = new MemoryStream()) { // Initialize PGP encryption keys and PGP utility EncryptionKeys encryptionKeys = new EncryptionKeys(publicKeyStream); PGP pgp = new PGP(encryptionKeys); // Prepare the input data for encryption MemoryStream memoryStream = new MemoryStream(); await inblobStream.CopyToAsync(memoryStream); memoryStream.Position = 0; // Encrypt the data stream await pgp.EncryptStreamAsync(inblobStream, outputFileStream); outputFileStream.Position = 0; // Upload encrypted file to outbound container BlobServiceClient blobServiceClient = new BlobServiceClient(Environment.GetEnvironmentVariable("Devsandpit")); BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(outboundContainerName); BlobClient blobClient = containerClient.GetBlobClient(name); await blobClient.UploadAsync(outputFileStream, new BlobHttpHeaders { ContentType = "application/octet-stream" }); } } }
解决方案
问题根源
你已经将inblobStream的内容复制到memoryStream并重置了位置,但加密时却传入了读取到末尾的inblobStream——流被读取后,位置指针会移动到流的末尾,此时PGP加密方法无法读取任何数据,触发"inputStream should be at start of stream"错误。
修改方案
直接使用你已经准备好的memoryStream作为加密输入(推荐此方案,彻底规避流位置问题):
修改后的代码:
using System; using System.IO; using System.Text; using System.Threading.Tasks; using Azure.Identity; using Azure.Security.KeyVault.Secrets; using Microsoft.Azure.WebJobs; using Microsoft.Extensions.Logging; using Azure.Storage.Blobs; using Azure.Storage.Blobs.Models; using PgpCore; public class Function1 { private readonly string keyVaultName = "MyKV"; private readonly string keyName = "SKEY"; private readonly string outboundContainerName = "OutBound"; [FunctionName("BlobTriggerFunction")] public async Task Run( [BlobTrigger("Inbound/{name}.txt", Connection = "Devsandpit")] Stream inblobStream, string name, ILogger log) { log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {inblobStream.Length} Bytes"); // Get PGP public key from Azure Key Vault var client = new SecretClient(new Uri($"https://{keyVaultName}.vault.azure.net/"), new DefaultAzureCredential()); KeyVaultSecret secret = await client.GetSecretAsync(keyName); string publicKey = secret.Value; // Decode the Base64 encoded public key byte[] publicKeyBytes = Convert.FromBase64String(publicKey); // Using MemoryStream for the public key using (MemoryStream publicKeyStream = new MemoryStream(publicKeyBytes)) using (Stream outputFileStream = new MemoryStream()) { // Initialize PGP encryption keys and PGP utility EncryptionKeys encryptionKeys = new EncryptionKeys(publicKeyStream); PGP pgp = new PGP(encryptionKeys); // Prepare the input data for encryption MemoryStream memoryStream = new MemoryStream(); await inblobStream.CopyToAsync(memoryStream); memoryStream.Position = 0; // 修改:使用已重置位置的memoryStream作为加密输入 await pgp.EncryptStreamAsync(memoryStream, outputFileStream); outputFileStream.Position = 0; // Upload encrypted file to outbound container BlobServiceClient blobServiceClient = new BlobServiceClient(Environment.GetEnvironmentVariable("Devsandpit")); BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(outboundContainerName); BlobClient blobClient = containerClient.GetBlobClient(name); await blobClient.UploadAsync(outputFileStream, new BlobHttpHeaders { ContentType = "application/octet-stream" }); } } }
备选方案(不推荐)
如果一定要使用原inblobStream,则需要在调用加密方法前重置流的位置:
// 在EncryptStreamAsync前添加 inblobStream.Position = 0; await pgp.EncryptStreamAsync(inblobStream, outputFileStream);
此方案依赖流支持重置操作,部分场景下可能不适用,因此优先推荐使用memoryStream的方案。
内容的提问来源于stack exchange,提问作者user26496939
相关产品推荐
相关产品推荐

