批量生成Azure IoT Hub设备标识时出错

时间:2017-11-08 16:35:43

标签: java azure azure-storage-blobs azure-iot-hub

我正在关注https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-bulk-identity-mgmt在Azure IoT Hub中批量上传设备标识。这里给出的所有代码都是在C#中,所以我将其转换为JAVA等价物。

使用导入设备示例 - 批量设备配置我正在关注json -

{"id":"d3d78b0d-6c8c-4ef5-a321-91fbb6a4b7d1","importMode":"create","status":"enabled","authentication":{"symmetricKey":{"primaryKey":"f8/UZcYbhPxnNdbSl2J+0Q==","secondaryKey":"lbq4Y4Z8qWmfUxAQjRsDjw=="}}}
{"id":"70bbe407-8d65-4f57-936f-ef402aa66d07","importMode":"create","status":"enabled","authentication":{"symmetricKey":{"primaryKey":"9e7fDNIFbMu/NmOfxo/vGg==","secondaryKey":"nwFiKR4HV9KYHzkeyu8nLA=="}}}

从blob跟随函数导入文件 -

CompletableFuture<JobProperties> importJob = registryManager
    .importDevicesAsync(inURI, outURI);

在上面的代码中,我们需要提供带有SAS代码的URI,因为获取容器SAS URI 等效代码如下 -

static String GetContainerSasUri(CloudBlobContainer container) {
    SharedAccessBlobPolicy sasConstraints = new SharedAccessBlobPolicy();
    sasConstraints.setSharedAccessExpiryTime(new Date(new Date().getTime() + TimeUnit.DAYS.toMillis(1)));
    sasConstraints.setPermissions(EnumSet.of(SharedAccessBlobPermissions.READ, SharedAccessBlobPermissions.WRITE,
            SharedAccessBlobPermissions.LIST, SharedAccessBlobPermissions.DELETE));

    BlobContainerPermissions permissions = new BlobContainerPermissions();
    permissions.setPublicAccess(BlobContainerPublicAccessType.CONTAINER);
    permissions.getSharedAccessPolicies().put("testpolicy", sasConstraints);
    try {
        container.uploadPermissions(permissions);
    } catch (StorageException e1) {
        e1.printStackTrace();
    }
    String sasContainerToken = null;
    try {
        sasContainerToken = container.generateSharedAccessSignature(sasConstraints, "testpolicy");
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (StorageException e) {
        e.printStackTrace();
    }
    System.out.println("URI " + container.getUri() +"?"+ sasContainerToken);
    return container.getUri() + "?" + sasContainerToken;
}

现在问题来了。对于输出容器,我收到以下错误 -

java.util.concurrent.ExecutionException: com.microsoft.azure.sdk.iot.service.exceptions.IotHubBadFormatException: Bad message format! ErrorCode:BlobContainerValidationError;Unauthorized to write to output blob container. Tracking ID:2dcb2efbf1e14e33ba60dc8415dc03c3-G:4-TimeStamp:11/08/2017 16:16:10

请帮助我知道为什么我收到错误信息格式错误? SAS密钥生成代码是否有问题,或者我的blob容器没有写入权限?

1 个答案:

答案 0 :(得分:0)

您使用的是服务或帐户级SAS吗?抛出的错误表明服务未经授权或具有写入指定blob容器的委派权限。在此处查看资源,了解如何设置帐户级SAS以及如何在blob容器上委派读取,写入和删除操作。 https://docs.microsoft.com/en-us/rest/api/storageservices/Delegating-Access-with-a-Shared-Access-Signature?redirectedfrom=MSDN“剪切内容:”2015-04-05版本中引入的帐户级SAS。帐户SAS委派对一个或多个存储服务中的资源的访问权限。通过服务SAS提供的所有操作也可通过帐户SAS获得。此外,使用帐户SAS,您可以委派对适用于给定服务的操作的访问权限,例如获取/设置服务属性和获取服务统计信息。您还可以在对服务SAS不允许的blob容器,表,队列和文件共享上委派对读取,写入和删除操作的访问权限。有关帐户SAS的详细信息,请参阅构建帐户SAS。“