问题
I am creating application where I upload, download files in my azure storage account through java services. When I upload files with name having no space in their name(example : "image.png"), program returned me token successfully.
But when I try to get token while uploading or downloading for file names having spaces in between them like "new image.png" , it gives me error at particular line.
My code is :
CloudFile cloudFile = directory.getFileReference(fileNameWithExtension);
String tokenKey = testFileSAS(share, cloudFile);
System.out.println(cloudFile.toString());
cloudFile.downloadToFile(DownloadTo);
Where as code to get the token is :
@Test
public String testFileSAS(CloudFileShare share, CloudFile file) throws InvalidKeyException, IllegalArgumentException, StorageException, URISyntaxException, InterruptedException {
SharedAccessFilePolicy policy = createSharedAccessPolicy(
EnumSet.of(SharedAccessFilePermissions.READ,
SharedAccessFilePermissions.LIST,
SharedAccessFilePermissions.WRITE),
100);
FileSharePermissions perms = new FileSharePermissions();
perms.getSharedAccessPolicies().put("readperm", policy);
share.uploadPermissions(perms);
CloudFile sasFile = new CloudFile(new URI(file.getUri().toString()
+ "?" + file.generateSharedAccessSignature(null, "readperm")));
System.out.println("sasFile==========="+sasFile.getName());
sasFile.download(new ByteArrayOutputStream());
CloudFile fileFromUri = new CloudFile(PathUtility.addToQuery(file.getStorageUri(),
file.generateSharedAccessSignature(null, "readperm")));
assertEquals(StorageCredentialsSharedAccessSignature.class.toString(),
fileFromUri.getServiceClient().getCredentials().getClass().toString());
StorageCredentials creds = new StorageCredentialsSharedAccessSignature(
file.generateSharedAccessSignature(policy, null, null));
System.out.println("Generated SAS token is : " + file.generateSharedAccessSignature(policy, null, null));
String token = file.generateSharedAccessSignature(policy, null, null);
CloudFileClient client = new CloudFileClient(sasFile.getServiceClient().getStorageUri(), creds);
CloudFile fileFromClient = client.getShareReference(file.getShare().getName()).getRootDirectoryReference()
.getFileReference(file.getName());
assertEquals(StorageCredentialsSharedAccessSignature.class.toString(),
fileFromClient.getServiceClient().getCredentials().getClass().toString());
assertEquals(client, fileFromClient.getServiceClient());
return token;
}
I get error at this line :
sasFile.download(new ByteArrayOutputStream());
I think it is because when there is space in file names, it takes %20 in place of space and because of this, it can't get the file and gives the error.
What should I do to perform desired operation?
回答1:
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());
来源:https://stackoverflow.com/questions/44450473/how-to-download-file-having-spaces-in-its-name-from-azure-by-java-service