1
votes

I am following https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-bulk-identity-mgmt to do Bulk upload of Device Identities in Azure IoT Hub. All codes given here are in C# so I am converting it to JAVA equivalent.

Using Import devices example – bulk device provisioning I am getting following 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=="}}}

To import the file from blob following function is called-

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

In the above code, we need to provide URI with SAS code, for that Get the container SAS URI equivalent code is below-

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;
}

Now the problem is coming here. For the output container I am getting following error-

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

Please help me to know why I am getting Bad Message Format error? Is there a problem with the SAS key generating code or my blob container is not having Write permission?

1

1 Answers

0
votes

are you using a service or Account-level SAS? The error thrown suggests the service isn't authorized or have the delegated permissions to write to the designated blob container. Check out the resource here on how to setup an account level SAS and how to delegate read, write and delete operations on blob containers. https://docs.microsoft.com/en-us/rest/api/storageservices/Delegating-Access-with-a-Shared-Access-Signature?redirectedfrom=MSDN "snipped content: "An account-level SAS, introduced with version 2015-04-05. The account SAS delegates access to resources in one or more of the storage services. All of the operations available via a service SAS are also available via an account SAS. Additionally, with the account SAS, you can delegate access to operations that apply to a given service, such as Get/Set Service Properties and Get Service Stats. You can also delegate access to read, write, and delete operations on blob containers, tables, queues, and file shares that are not permitted with a service SAS. See Constructing an Account SAS for more information about account SAS."