如何通过java服务从azure下载名称中包含空格的文件? [英] How to download file having spaces in it's name from azure by java service?

查看:139
本文介绍了如何通过java服务从azure下载名称中包含空格的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

我的代码是:

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代替空格因此,它无法获取文件并给出错误。

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?

推荐答案

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

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>

问题是由不正确的SAS令牌引起的,而不是由具有空格的文件名引起的。因此,当我设置策略的到期时间时,我将更改下面的代码以使其正常工作,尽管我不知道您的方法的实现 createSharedAccessPolicy

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());

这篇关于如何通过java服务从azure下载名称中包含空格的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆