微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用CloudFileClient从Azure文件存储中获取文件内容字节数组?

如何解决如何使用CloudFileClient从Azure文件存储中获取文件内容字节数组?

我在azure上有文件存储,并且正在连接并遍历该目录成功。但是我无法获取文件内容。为了获得FileClientReference,我使用以下代码

public CloudFileClient getFileClientReference() {
    log.info("Logging into azure file storage:");
    CloudFileClient cloudFileClient = null;

    CloudStorageAccount storageAccount;
    try {
        storageAccount = CloudStorageAccount.parse(storageConnectionString);
        cloudFileClient = storageAccount.createCloudFileClient();
    } catch (IllegalArgumentException | URISyntaxException e) {
        log.error("Connection string specifies an invalid URI.");
        log.error("Please confirm the connection string is in the Azure connection string format.");
        throw new AzureFileStorageNotAvailableException("Failed to login to azure file storage.");
    } catch (InvalidKeyException e) {
        log.error("Connection string specifies an invalid key.");
        log.error("Please confirm the AccountName and AccountKey in the connection string are valid.");
        throw new AzureFileStorageNotAvailableException("Failed to login to azure file storage.");
    }
    log.info("Logged into azure file storage.");
    return cloudFileClient;
}

我已经测试了此代码,并且工作正常。我用它遍历所有目录。 我现在想做的是给定的URL获取文件内容。我用来获取网址的代码是:

Iterable<ListFileItem> results = rootDir.listFilesAndDirectories();
    for (ListFileItem item : results) {
        boolean isDirectory = item.getClass() == CloudFileDirectory.class;
        final String uri = item.getUri().toString();
        if (isDirectory && uri.contains("myPath")) {
            traverseDirectories((CloudFileDirectory) item,azureFiles);
        } else if (!isDirectory) {
            handleFile(item,uri,azureFiles);
        }
    }

最后结果是:

https://appnamedev.file.core.windows.net/mystorage/2018/status/somepdf.pdf

现在,我想使用此url稍后以字节数组的形式获取文件内容,为此,我正在使用以下代码

 fileClientReference.getShareReference(document.getPath())
                 .getRootDirectoryReference().getFileReference(document.getFileName()).openRead();

document.getPath()将指向上述路径,而document.getFileName()将给出文件名:somepdf.pdf。

调用方法时出现错误

Method threw 'com.microsoft.azure.storage.StorageException' exception.
The specifed resource name contains invalid characters.

pdf可以,但是我不知道如何访问pdf并获取内容

解决方法

如果有人也在试图弄清楚如何做到这一点,那就是一个答案: 首先,当您调用方法时:

fileClientReference.getShareReference(document.getPath())

路径应采用以下格式:

/folder1/folder2/folder3/

没有天蓝色的前缀:

https://appnamedev.file.core.windows.net

,并且没有我以前尝试的文件名。我通过在字符串上调用replaceAll进行了解析。

我有一种方法:

CloudFile cloudFile;
    try {
        String fileLocation = document.getPath().replaceAll(AZURE_FILE_STORAGE_URL_PREFIX + "|" + document.getFileName(),"");
        final CloudFileShare fileShare = fileClientReference.getShareReference(fileLocation);
        cloudFile = fileShare.getRootDirectoryReference().getFileReference(document.getFileName());
        return new AzureFile(document.getFileName(),readFileContent(cloudFile));
    } catch (URISyntaxException | StorageException e) {
        log.error("Failed to retrieve file for document with id: {}",documentId,e);
        throw new AzureFileStorageNotAvailableException("Failed to retrieve file");
    }

readFileContent方法是:

private ByteArrayResource readFileContent(CloudFile cloudFile) {
    try (final FileInputStream fileInputStream = cloudFile.openRead()) {
        final byte[] content = fileInputStream.readAllBytes();
        return new ByteArrayResource(content);
    } catch (StorageException | IOException e) {
        log.error("Failed to read file content",e);
        throw new AzureFileStorageNotAvailableException("Failed to read file");
    }
}

AzureFile是我需要传递文件名和内容以来自行创建的实体:

@Data
@AllArgsConstructor
public class AzureFile {

    private String fileName;
    private ByteArrayResource content;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。