Azure / azure-storage-java

Microsoft Azure Storage Library for Java
https://docs.microsoft.com/en-us/java/api/overview/azure/storage
MIT License
189 stars 163 forks source link

When constructing CloudTableClient with a URL of a specific table, only batch fails #249

Open hope4555 opened 6 years ago

hope4555 commented 6 years ago

Trying to use a batch insert to azure table fails if using a SAS ("Shared access signature") When using account key (which is less secure i guess) it works

Example code:


StorageCredentialsSharedAccessSignature credentials = new StorageCredentialsSharedAccessSignature("sig=.....");

CloudTableClient cloudTableClient = new CloudTableClient(new URI("https://<table>.table.core.windows.net/<tablename>"), credentials);

CloudTable cloudTable = cloudTableClient.getTableReference("<tablename>");

//these 2 will be in a batch
TableServiceEntity d1 = new TableServiceEntity("3333333333333", "22222222222222" + System.currentTimeMillis());
TableServiceEntity d2 = new TableServiceEntity("3333333333333", "eeeeeeeeeee" + System.currentTimeMillis());

//single
TableServiceEntity d3 = new TableServiceEntity("ddddddddddddddddddd", "dddddddddd" + System.currentTimeMillis());

//fill the batch
TableBatchOperation batch = new TableBatchOperation();
batch.insert(d1);
batch.insert(d2);
try {
    // this will work (not batch, just to show that regular insert works)
    cloudTable.execute(TableOperation.insert(d3));

    // this will fail
    cloudTable.execute(batch);

} catch (StorageException e) {
    //here we get "Unsupported Media Type" (415 error)
    e.printStackTrace();
    return;
}
System.out.println("OK");

this issue i think is the same for .Net. See below https://github.com/Azure/azure-storage-net/issues/584

mirobers commented 6 years ago

Hi, I believe the issue is that the URL passed to the constructor of CloudTableClient in the second line contains the name of the table. The CloudTableClient constructor takes the base URL of the table service, i.e. "https://myaccount.table.core.windows.net". Then you can use GetTableReference to work with a specific table in the account.

Is there an online example or tutorial you were following that has the wrong URL? If so, please let me know and I will make sure it is corrected.

hope4555 commented 6 years ago

Ahh you are correct, without the table name the batch operation seems to work. We have used the url with the table name for some time now (years.) I guess that we got confused since adding a single entity (not in a batch) (cloudTable.execute(TableOperation.insert(d3)); is working even if the URL has the table name in it

Thanks

mirobers commented 6 years ago

It's possibly a defect that it works in some cases but not others. I'll rename the issue and leave it open to track.

bsdworkin commented 4 years ago

Been stuck on this problem as well luckily I found this thread. @mirobers Some clarification on this page(https://docs.microsoft.com/en-us/azure/cosmos-db/table-storage-how-to-use-java) would help as there isn't specific instruction to omit the TableName when doing batch operations