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

How to get file content (byte array) from azure file storage using CloudFileClient?

我在 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 获取文件内容。我用来获取 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/

没有来自 azure 的前缀:

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