在 java 和 azure blob storage 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);

    }


茅侃侃
浏览 149回答 1
1回答

largeQ

正如@ZhaoxingLu-Microsoft 所说,file生成的对象source.listFiles()足以通过 获取绝对文件路径file.getAbsolutePath(),因此您可以编写如下代码。if (blob.exists() == false) {&nbsp; &nbsp; 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 名称。图 4. 在我的本地机器上构建的文件和目录结构&nbsp;图 5. 通过 Azure 存储资源管理器在 Azure Blob 存储上进行上述操作&nbsp;这是我的完整代码。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 {&nbsp; &nbsp; // Container name must be lower case.&nbsp; &nbsp; CloudBlobContainer container = serviceClient.getContainerReference(containerName);&nbsp; &nbsp; container.createIfNotExists();&nbsp; &nbsp; String blobName = file.getAbsolutePath().replace(path, "");&nbsp; &nbsp; CloudBlockBlob blob = container.getBlockBlobReference(blobName);&nbsp; &nbsp; if (blob.exists() == false) {&nbsp; &nbsp; &nbsp; &nbsp; blob.uploadFromFile(file.getAbsolutePath());&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("File " + file.getAbsolutePath() + " Already exist in storage");&nbsp; &nbsp; }}public static void main(String[] args)&nbsp; &nbsp; &nbsp; &nbsp; throws URISyntaxException, StorageException, InvalidKeyException, IOException {&nbsp; &nbsp; CloudStorageAccount account = CloudStorageAccount.parse(storageConnectionString);&nbsp; &nbsp; serviceClient = account.createCloudBlobClient();&nbsp; &nbsp; File source = new File(path);&nbsp; &nbsp; for (File fileOrDir : source.listFiles()) {&nbsp; &nbsp; &nbsp; &nbsp; boolean isFile = fileOrDir.isFile();&nbsp; &nbsp; &nbsp; &nbsp; if(isFile) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; upload(fileOrDir);&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(File file: fileOrDir.listFiles()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; upload(file);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java