Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用CloudFileClient从azure文件存储中获取文件内容(字节数组)?_Java_Azure_Azure Files - Fatal编程技术网

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

Java 如何使用CloudFileClient从azure文件存储中获取文件内容(字节数组)?,java,azure,azure-files,Java,Azure,Azure Files,我在azure上有文件存储,我正在成功连接和遍历这些目录。但我无法获取文件的内容。为了获取FileClientReference,我使用以下代码: public CloudFileClient getFileClientReference() { log.info("Logging into azure file storage:"); CloudFileClient cloudFileClient = null; CloudStorageAccou

我在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;
}
 fileClientReference.getShareReference(document.getPath())
                 .getRootDirectoryReference().getFileReference(document.getFileName()).openRead();
我已经测试了这段代码,它运行良好。我用它来遍历所有目录。 我现在要做的是让给定的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);
        }
    }
其中
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的前缀:

没有我以前尝试过的文件名。我通过调用字符串上的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;
}

当我们调用
getShareReference
方法时,我们只需要提供共享名:@JimXu这是文件名吗?