在java和azure blob存储sdk中上载包含子目录的目录
我使用此代码将文件上载到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);
}
# 1 楼答案
正如@ZhaoxingLu Microsoft所说,
source.listFiles()
生成的file
对象足以通过file.getAbsolutePath()
获取绝对文件路径,因此您可以编写如下代码我在我的环境中测试您的代码,它也可以工作。但是,根据我的经验,您的问题
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名称图4。在本地计算机上构建的文件和目录结构
图5。通过Azure存储资源管理器在Azure Blob存储上执行上述操作
这是我的完整代码