无法访问存储帐户 blob 获取 BlobContainerValidationError 错误 c#

unable to access storage account blob getting BlobContainerValidationError error c#

我正在尝试从 blob 存储中读取数据以使用 iothub 导入作业。

我可以成功写入 blob 文件,但从 blob 读取时出现以下异常 -

 await registryManager.ImportDevicesAsync(containerSasUri, containerSasUri);

{"{\"Message\":\"ErrorCode:BlobContainerValidationError;Failed to read devices blob from input container.\",\"ExceptionMessage\":\"Tracking ID:6f06c1ce39f04494b929a2249ce069f2-G:9-TimeStamp:01/06/2019 11:57:23\"}"}

 static string GetContainerSasUri(CloudBlobContainer container)
            {
                // Set the expiry time and permissions for the container.
                // In this case no start time is specified, so the
                // shared access signature becomes valid immediately.
                var sasConstraints = new SharedAccessBlobPolicy();
                sasConstraints.SharedAccessExpiryTime = DateTime.UtcNow.AddHours(24);
                sasConstraints.Permissions =
                  SharedAccessBlobPermissions.Write |
                  SharedAccessBlobPermissions.Read |
                  SharedAccessBlobPermissions.Delete | SharedAccessBlobPermissions.Add | SharedAccessBlobPermissions.Create;

                // Generate the shared access signature on the container,
                // setting the constraints directly on the signature.
                string sasContainerToken = container.GetSharedAccessSignature(sasConstraints);

                // Return the URI string for the container,
                // including the SAS token.
                return container.Uri + sasContainerToken;
            }


   registryManager = RegistryManager.CreateFromConnectionString(connectionString);

                CloudStorageAccount storageAccount = CloudStorageAccount.Parse("connection-string");
                // Create a blob client.

                CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

                CloudBlobContainer container = blobClient.GetContainerReference("test");

                CloudBlockBlob blob = container.GetBlockBlobReference("demo123.txt");

                var containerSasUri = GetContainerSasUri(container);

                // Provision 1,000 more devices
                  serializedDevices = new List<string>();

                for (var i = 0; i < 5; i++)
                {
                    // Create a new ExportImportDevice
                    // CryptoKeyGenerator is in the Microsoft.Azure.Devices.Common namespace
                    var deviceToAdd = new ExportImportDevice()
                    {
                        Id = i+"look",
                        Status = DeviceStatus.Enabled,
                        Authentication = new AuthenticationMechanism()
                        {
                            SymmetricKey = new SymmetricKey()
                            {
                                PrimaryKey = CryptoKeyGenerator.GenerateKey(32),
                                SecondaryKey = CryptoKeyGenerator.GenerateKey(32)
                            }
                        },
                        ImportMode = ImportMode.Create
                    };

                    // Add device to the list
                    serializedDevices.Add(JsonConvert.SerializeObject(deviceToAdd));
                }
                var tt = serializedDevices;
               // Write the list to the blob
               var sb = new StringBuilder();
                serializedDevices.ForEach(serializedDevice => sb.AppendLine(serializedDevice));
                //await blob.DeleteIfExistsAsync();

                using (CloudBlobStream stream = await blob.OpenWriteAsync())
                {
                    byte[] bytes = Encoding.UTF8.GetBytes(sb.ToString());
                    for (var i = 0; i < bytes.Length; i += 500)
                    {
                        int length = Math.Min(bytes.Length - i, 500);
                        await stream.WriteAsync(bytes, i, length);
                    }
                }

                // Call import using the blob to add new devices
                // Log information related to the job is written to the same container
                // This normally takes 1 minute per 100 devices
                JobProperties importJob =
                   await registryManager.ImportDevicesAsync(containerSasUri, containerSasUri);

尝试设置:

 sasConstraints.SharedAccessStartTime = DateTimeOffset.UtcNow.AddMinutes(-5); 

更新:

默认的输入 blob 名称是 devices.txt。在您的实现中,输入 blob 的名称是 demo123.txt,因此您必须更改:

await registryManager.ImportDevicesAsync(containerSasUri, containerSasUri, "demo123.txt");