Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/12.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和azure blob存储sdk中上载带有子目录的目录_Java_Azure_Azure Storage Blobs - Fatal编程技术网

在java和azure blob存储sdk中上载带有子目录的目录

在java和azure blob存储sdk中上载带有子目录的目录,java,azure,azure-storage-blobs,Java,Azure,Azure Storage Blobs,我使用此代码将文件上载到azure blob存储,但当我尝试加载带有子目录的目录时,会出现错误“FileNotFoundException:C:\upload\bin”:(访问被拒绝),是否有任何解决方案可以加载源目录中的文件和目录 try { CloudStorageAccount account = CloudStorageAccount.parse(storageConnectionString); CloudBlobClient serviceClient

我使用此代码将文件上载到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);
    }

正如@ZhaoxingLu微软所说,
source.listFiles()
生成的
file
对象足以通过
file.getAbsolutePath()
获取绝对文件路径,因此您可以编写如下代码

if (blob.exists() == false) {
    blob.uploadFromFile(file.getAbsolutePath());
} else System.out.println("File " + file.getAbsolutePath() + " Already exist in storage");
我在我的环境中测试您的代码,它也可以工作。但是,根据我的经验,您遇到的问题
FileNotFoundException:C:\upload\bin:(访问被拒绝)
是由于缺少访问
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存储上的上述操作

这是我的完整代码

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);
            }
        }

    }
}
private static final String path=“D:\\upload\\”;
私有静态最终字符串storageConnectionString=“”;
私有静态最终字符串containerName=“”;
私有静态CloudBlobClient服务客户端;
公共静态无效上载(文件文件)引发InvalidKeyException、URISyntaxException、StorageException、IOException{
//容器名称必须小写。
CloudBlobContainer container=serviceClient.getContainerReference(containerName);
container.createIfNotExists();
字符串blobName=file.getAbsolutePath().replace(路径“”);
CloudBlockBlob blob=container.getBlockBlobReference(blobName);
if(blob.exists()==false){
uploadFromFile(file.getAbsolutePath());
}否则{
System.out.println(“文件”+File.getAbsolutePath()+“已存在于存储器中”);
}
}
公共静态void main(字符串[]args)
抛出URI语法异常、StorageException、InvalidKeyException、IOException{
CloudStorageAccount=CloudStorageAccount.parse(storageConnectionString);
serviceClient=account.createCloudBlobClient();
文件源=新文件(路径);
对于(文件文件管理器:source.listFiles()){
布尔值isFile=fileorder.isFile();
if(isFile){
上传(文件管理器);
}否则{
对于(文件:fileorder.listFiles()){
上传(文件);
}
}
}
}

我猜
source+“\\\”+file.getName()
不包含所列文件的子目录?难道
文件
还不够吗,为什么需要构造另一个变量
源文件
?@ZhaoxingLu Microsoft感谢您的评论我的第一个代码可以完美地处理上载文件,但有任何错误,当我将目录放入子目录时出现错误,我将代码更改为@P例如:
if(blob.exists()==false){System.out.println(file.getAbsolutePath());blob.uploadFromFile(file.getAbsolutePath());}else System.out.println(“文件”+file.getAbsolutePath()+“已存在于存储器中”)
但它只上传文件@ЮЮЮЮЮЮЮ你想上传名为
upload/bin/
/
的blob吗?在目录C:\upload中我有子目录bin,backup,archive,还有一些文件,我想上传这个结构到blob storage我想在blob中得到这样的结构:bin/-file1-file2-fileN backup/-file1-file2-fileN`@我更新了我的帖子。希望它能帮助你。