gpt4 book ai didi

java - 使用 SAS 从 Java 批量插入 Azure 表失败

转载 作者:行者123 更新时间:2023-12-02 11:46:15 27 4
gpt4 key购买 nike

如果使用 SAS(“共享访问签名”),尝试对 Azure 表进行批量插入会失败。当使用帐户 key (我猜这不太安全)时,它可以工作。

示例代码:

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

CloudTableClient cloudTableClient = new CloudTableClient(new URI("https://<storage account>.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());

//prepare 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");

我得到的错误是:

com.microsoft.azure.storage.StorageException: Unsupported Media Type
at com.microsoft.azure.storage.StorageException.translateException(StorageException.java:89)
at com.microsoft.azure.storage.core.StorageRequest.materializeException(StorageRequest.java:315)
at com.microsoft.azure.storage.core.ExecutionEngine.executeWithRetry(ExecutionEngine.java:175)
at com.microsoft.azure.storage.table.TableBatchOperation.execute(TableBatchOperation.java:418)
at com.microsoft.azure.storage.table.CloudTable.execute(CloudTable.java:475)
at com.microsoft.azure.storage.table.CloudTable.execute(CloudTable.java:432)
at com.bgprotect.azurestorage.Test.main(Test.java:49)

SAS

sig=<sig>&se=2020-01-01T00%3A00%3A00Z&sv=2015-04-05&tn=<table name>&sp=raud

最佳答案

基于 Github 上的问题,请尝试更改以下代码行:

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

至:

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

基本上不要在 URI 中包含表的名称。它只能是 https://account-name.table.core.windows.net

附注我没有意识到您也在 Github 上提出了问题关于这一点:)。

关于java - 使用 SAS 从 Java 批量插入 Azure 表失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48185970/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com