我使用此代码将文件上传到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);
}
答案 0 :(得分:0)
正如@ ZhaoxingLu-Microsoft所说,由file
生成的source.listFiles()
对象足以通过file.getAbsolutePath()
获取绝对文件路径,因此您可以编写如下代码。
if (blob.exists() == false) {
blob.uploadFromFile(file.getAbsolutePath());
} else System.out.println("File " + file.getAbsolutePath() + " Already exist in storage");
我在我的环境中测试您的代码,它也可以工作。但是,根据我的经验,您的问题FileNotFoundException encountered: C:\upload\bin" :(Access is denied)
是由于缺乏访问C:
或C:\upload\bin
下的文件的权限引起的。因此,您需要在当前Windows环境中以管理员身份运行代码,如下图。
图1.如果使用IntelliJ,请以管理员身份运行代码
图2.如果使用Eclipse,请以管理员身份运行代码
图3.通过命令提示符以管理员身份运行代码
更新:
在Azure Blob存储上,文件和目录结构取决于Blob名称。因此,如果您要查看如下图所示的文件结构,可以使用代码String blobName = file.getAbsolutePath().replace(path, "");
来获取Blob名称。
图5.通过Azure Storage Explorer在Azure Blob存储上的上述操作相同
这是我完整的代码。
private static final String path = "D:\\upload\\";
private static final String storageConnectionString = "<your storage connection string>";
private static final String containerName = "<your container for uploading>";
private static CloudBlobClient serviceClient;
public static void upload(File file) throws InvalidKeyException, URISyntaxException, StorageException, IOException {
// Container name must be lower case.
CloudBlobContainer container = serviceClient.getContainerReference(containerName);
container.createIfNotExists();
String blobName = file.getAbsolutePath().replace(path, "");
CloudBlockBlob blob = container.getBlockBlobReference(blobName);
if (blob.exists() == false) {
blob.uploadFromFile(file.getAbsolutePath());
} else {
System.out.println("File " + file.getAbsolutePath() + " Already exist in storage");
}
}
public static void main(String[] args)
throws URISyntaxException, StorageException, InvalidKeyException, IOException {
CloudStorageAccount account = CloudStorageAccount.parse(storageConnectionString);
serviceClient = account.createCloudBlobClient();
File source = new File(path);
for (File fileOrDir : source.listFiles()) {
boolean isFile = fileOrDir.isFile();
if(isFile) {
upload(fileOrDir);
} else {
for(File file: fileOrDir.listFiles()) {
upload(file);
}
}
}
}