How to download file having spaces in it's name from azure by java service?

一曲冷凌霜 提交于 2019-12-02 11:51:22

I tried to reproduce your issue successfully, and got the error information via Fiddler as below.

<Error>
    <Code>AuthenticationFailed</Code>
    <Message>
Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature. RequestId:237ce8e7-001a-000d-57df-e4c9be000000 Time:2017-06-14T07:28:17.0520478Z
    </Message>
    <AuthenticationErrorDetail>
Signed expiry must be specified in signature or SAS identifier
    </AuthenticationErrorDetail>
</Error>

The issue was caused by the incorrect SAS token, not by a file name having spaces. So I change your code below to make it works when I set the expiry time for policy, although I don't know the implementation of your method createSharedAccessPolicy.

SharedAccessFilePolicy policy = new SharedAccessFilePolicy();
policy.setPermissions(EnumSet.of(SharedAccessFilePermissions.READ,
               SharedAccessFilePermissions.LIST, 
               SharedAccessFilePermissions.WRITE));
// Set expiry time for policy, and then it works.
policy.setSharedAccessExpiryTime(new Date(new Date().getTime()+3600));
FileSharePermissions perms = new FileSharePermissions();
perms.getSharedAccessPolicies().put("readperm", policy);
share.uploadPermissions(perms); 
String uri = file.getUri().toString() 
        + "?" + file.generateSharedAccessSignature(null, "readperm");
CloudFile sasFile = new CloudFile(new URI(uri));
System.out.println("sasFile==========="+sasFile.getName());
sasFile.download(new ByteArrayOutputStream());
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!