如何通过java服务从azure下载其中包含空格的文件?

时间:2017-06-09 06:10:32

标签: java azure testing logic

我正在创建我上传的应用程序,通过java服务在我的azure存储帐户中下载文件。当我上传姓名中没有空格的文件时(例如:" image.png"),程序成功返回了我的令牌。

但是当我尝试在上传或下载时获取令牌,因为文件名之间有空格,例如" new image.png" ,它给了我特定行的错误。

我的代码是:

CloudFile cloudFile = directory.getFileReference(fileNameWithExtension);
String tokenKey = testFileSAS(share, cloudFile);                                    
System.out.println(cloudFile.toString());
cloudFile.downloadToFile(DownloadTo);

获取令牌的代码在哪里:

@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;
}

我在这一行得到错误:

sasFile.download(new ByteArrayOutputStream());

我认为这是因为当文件名中有空格时,它需要%20代替空格,因此,它无法获取文件并给出错误。

我该怎么做才能执行所需的操作?

1 个答案:

答案 0 :(得分:0)

我尝试成功重现您的问题,并通过Fiddler获取错误信息,如下所示。

<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>

问题是由不正确的SAS令牌引起的,而不是由具有空格的文件名引起的。因此,当我设置策略的到期时间时,我将更改下面的代码以使其有效,尽管我不知道您的方法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());