我正在将 MS Intune 的 MSI 文件上传到 Azure 存储。我将文件分成块并使用 SAS 链接将它们上传到存储库中。当我上传文件时,我得到了成功的响应。Intune 确认文件已成功上传。
但是当计算机与 Intune 同步时,不会安装该文件。Intune 报告安装文件时发生错误(未指定代码或原因)。
在我看来,问题是将文件拆分为 chuks。但是我所有解决这个问题的努力都失败了。
public async Task UploadFile(string fullFilePath, string blobSasUri, int timeout = 60)
{
var BlockSize = 1024 * 1024;
var blocks = new List<String>();
using (var fileStream = new FileStream(fullFilePath, FileMode.Open, FileAccess.Read))
{
var buffer = new byte[BlockSize];
var bytesRead = 0;
var blockNumber = 0;
while ((bytesRead = fileStream.Read(buffer, 0, BlockSize)) > 0)
{
var actualBytesRead = new byte[bytesRead];
Buffer.BlockCopy(buffer, 0, actualBytesRead, 0, bytesRead);
var chunk = blockNumber++;
var blockId = Convert.ToBase64String(Encoding.UTF8.GetBytes(chunk.ToString("0000")));
blocks.Add(blockId);
await UploadBlock(blobSasUri, blockId, actualBytesRead, timeout);
}
}
await CommitAllBlocks(blobSasUri, blocks, timeout);
}
private async Task UploadBlock(String sasUrl, String blockId, byte[] contentBytes, int timeout)
{
using (var client = GetHttpClient(timeout))
{
var blockUrl = String.Format("{0}&comp=block&blockid={1}", sasUrl, blockId);
var iso = Encoding.GetEncoding("ISO-8859-1");
var encodedBody = iso.GetString(contentBytes);
HttpContent content = new StringContent(encodedBody, iso );
client.DefaultRequestHeaders.Add("x-ms-blob-type", "BlockBlob");
var result = await client.PutAsync(blockUrl, content);
}
}
private HttpClient GetHttpClient(int timeout)
{
var client = new HttpClient();
client.Timeout = GetTimeout(timeout);
return client;
}
private TimeSpan GetTimeout(int specifiedTimeout)
{
if (specifiedTimeout == 0)
{
return TimeSpan.FromMilliseconds(Timeout.Infinite);
}
return TimeSpan.FromMinutes(specifiedTimeout);
}
private async Task CommitAllBlocks(string sasUrl, List<string> blockIds, int timeout)
{
using (var client = GetHttpClient(timeout))
{
var contentString = @"<?xml version=""1.0"" encoding=""utf-8""?><BlockList>";
foreach (var blockId in blockIds)
{
contentString += $"<Latest>{blockId}</Latest>";
}
contentString += "</BlockList>";
HttpContent content = new StringContent(contentString);
var commitUrl = $"{sasUrl}&comp=blocklist";
await client.PutAsync(commitUrl, content);
}
}
作为示例,我使用了 Microsoft 的示例。我检查了它的性能,它运行良好。该文件已安装在计算机上。 https://github.com/microsoftgraph/powershell-intune-samples/blob/master/LOB_Application/Application_LOB_Add.ps1