如何为container.listBlobs()和blob.exists()生成单个帐户SAS

时间:2019-08-07 22:56:04

标签: java azure sas account

我正在开发一个Java应用程序,该应用程序使用Account SAS(共享访问签名)URI执行以下方法:

CLoudBlockBlob blob = container.getBlockBlobReference("tmp/test.json");
blob.exists();
blob.openInputStream();
blob.delete();
container.exists();
container.listBlobs().iterator().next();
container.getDirectoryReference("tmp/").listBlobs().iterator().next()

但是,我找不到一种方法来生成单个帐户SAS来支持上面列出的所有方法。

  1. 使用SharedAccessAccountResourceType.OBJECT生成的帐户SAS:

成功执行:

CLoudBlockBlob blob = container.getBlockBlobReference("tmp/test.json");
blob.exists();
blob.openInputStream();
blob.delete();

失败,错误代码:AuthorizationResourceTypeMismatch

container.exists();
container.listBlobs().iterator().next();
container.getDirectoryReference("tmp/").listBlobs().iterator().next()
  1. 使用SharedAccessAccountResourceType.CONTAINER生成的帐户SAS:

成功执行:

container.exists();
container.listBlobs().iterator().next();
container.getDirectoryReference("tmp/").listBlobs().iterator().next()

失败,错误代码:AuthorizationResourceTypeMismatch

CLoudBlockBlob blob = container.getBlockBlobReference("tmp/test.json");
blob.exists();
blob.openInputStream();
blob.delete();
  1. 使用SharedAccessAccountResourceType.SERVICE生成的帐户SAS 失败,并显示错误代码:上述所有方法的AuthorizationResourceTypeMismatch。

有什么方法可以生成可用于以下所有方法的单个Account SAS?

CLoudBlockBlob blob = container.getBlockBlobReference("tmp/test.json");
blob.exists();
blob.openInputStream();
blob.delete();
container.exists();
container.listBlobs().iterator().next();
container.getDirectoryReference("tmp/").listBlobs().iterator().next()

3 个答案:

答案 0 :(得分:0)

是的,只需在Blob和容器资源上添加正确的权限。请注意,列表不同于创建的列表。

您可以在这里找到它

enter image description here

然后添加权限

enter image description here

答案 1 :(得分:0)

对于blob容器中的listing blobs,您需要将resource type设置为Container。您需要的权限为List

对于checking if blob exists,您需要将resource type设置为Object。您需要的权限为Read

对于deleting blob,您需要将权限设置为Delete。资源类型仍为Object

由于您仅使用Blob存储,因此service type就是Blob

总结:

  • 服务类型:Blob
  • 资源类型:容器(用于列出blob)和Object(用于检查blob的存在并删除blob)
  • 权限:列出,读取和删除

借助此功能,您应该能够实现自己的目标。

答案 2 :(得分:0)

设置两种资源类型可以解决此问题:

sasAccountPolicy.setResourceTypes(EnumSet.of(SharedAccessAccountResourceType.OBJECT, SharedAccessAccountResourceType.CONTAINER));

(我正在开发的应用程序还必须生成SAS URI。)

谢谢。