【发布时间】:2019-06-22 17:37:59
【问题描述】:
我使用此代码将文件上传到 azure blob 存储,但是当我尝试使用子目录加载目录时出现错误“遇到 FileNotFoundException:C:\upload\bin”:(访问被拒绝),是否有任何解决方案加载源目录中的文件和目录?
try {
CloudStorageAccount account = CloudStorageAccount.parse(storageConnectionString);
CloudBlobClient serviceClient = account.createCloudBlobClient();
// Container name must be lower case.
CloudBlobContainer container = serviceClient.getContainerReference(containerName);
container.createIfNotExists();
File source = new File(path);
if (source.list().length > 0) {
for (File file : source.listFiles()) {
CloudBlockBlob blob = container.getBlockBlobReference(file.getName());
if (blob.exists() == false) {
File sourceFile = new File(source + "\\" + file.getName());
blob.upload(new FileInputStream(sourceFile), sourceFile.length());
System.out.println("File " + source + "\\" + file.getName() + " Load to blob storage");
} else System.out.println("File " + source + "\\" + file.getName() + " Already exist in storage");
}
} else System.out.println("In folder " + path + " are no files ");
} catch (FileNotFoundException fileNotFoundException) {
System.out.print("FileNotFoundException encountered: ");
System.out.println(fileNotFoundException.getMessage());
System.exit(-1);
} catch (StorageException storageException) {
System.out.print("StorageException encountered: ");
System.out.println(storageException.getMessage());
System.exit(-1);
} catch (Exception e) {
System.out.print("Exception encountered: ");
System.out.println(e.getMessage());
System.exit(-1);
}
【问题讨论】:
-
我猜
source + "\\" + file.getName()不包含列出文件的子目录?file还不够,为什么还要构造另一个变量sourceFile? -
@ZhaoxingLu-Microsoft 感谢您的评论
标签: java azure azure-blob-storage