Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.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 如何使用dasein API连接Azure云(blob存储)_Java_Azure_Azure Storage Blobs - Fatal编程技术网

Java 如何使用dasein API连接Azure云(blob存储)

Java 如何使用dasein API连接Azure云(blob存储),java,azure,azure-storage-blobs,Java,Azure,Azure Storage Blobs,有人能帮我举个例子或样品吗?我需要在blob存储中获取并放置文件 我成功地编写了以下代码: try { CloudProvider provider = (CloudProvider) Class.forName("org.dasein.cloud.azure.Azure").newInstance(); ProviderContext providerContext = new ProviderContext("DEV","West US"); //providerCo

有人能帮我举个例子或样品吗?我需要在blob存储中获取并放置文件

我成功地编写了以下代码:

try {
    CloudProvider provider = (CloudProvider) Class.forName("org.dasein.cloud.azure.Azure").newInstance();
    ProviderContext providerContext = new ProviderContext("DEV","West US");
    //providerContext.setStorage("");
    providerContext.setStorageAccountNumber("mypackages");
    providerContext.setStoragePublic("XXX".getBytes());
    providerContext.setEndpoint("http://XXX.blob.core.windows.net/");
    providerContext.setStorageX509Key("YYY".getBytes());
    provider.connect(providerContext, provider);

    System.out.println("here "+provider.testContext());
} catch (InstantiationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IllegalAccessException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
在执行上述代码时,我得到如下NPE

org.dasein.cloud.InternalException: java.lang.NullPointerException
    at org.dasein.cloud.azure.AzureX509.<init>(AzureX509.java:64)
    at org.dasein.cloud.azure.AzureMethod.getClient(AzureMethod.java:386)
    at org.dasein.cloud.azure.AzureMethod.getAsStream(AzureMethod.java:124)
    at org.dasein.cloud.azure.Azure.testContext(Azure.java:258)
    at com.gehcit.dasein.App.main(App.java:27)
Caused by: java.lang.NullPointerException
    at java.lang.String.<init>(Unknown Source)
    at org.dasein.cloud.azure.AzureX509.<init>(AzureX509.java:58)
    ... 4 more
org.dasein.cloud.InternalException: java.lang.NullPointerException
    at org.dasein.cloud.azure.AzureX509.<init>(AzureX509.java:64)
    at org.dasein.cloud.azure.AzureMethod.getClient(AzureMethod.java:386)
    at org.dasein.cloud.azure.AzureMethod.getAsStream(AzureMethod.java:124)
    at org.dasein.cloud.azure.Azure.testContext(Azure.java:258)
    at com.gehcit.dasein.App.main(App.java:27)
Caused by: java.lang.NullPointerException
    at java.lang.String.<init>(Unknown Source)
    at org.dasein.cloud.azure.AzureX509.<init>(AzureX509.java:58)
    ... 4 more
org.dasein.cloud.InternalException:java.lang.NullPointerException
位于org.dasein.cloud.azure.AzureX509。(AzureX509.java:64)
位于org.dasein.cloud.azure.AzureMethod.getClient(AzureMethod.java:386)
位于org.dasein.cloud.azure.AzureMethod.getAsStream(AzureMethod.java:124)
位于org.dasein.cloud.azure.azure.testContext(azure.java:258)
位于com.gehcit.dasein.App.main(App.java:27)
原因:java.lang.NullPointerException
位于java.lang.String。(未知源)
位于org.dasein.cloud.azure.AzureX509。(AzureX509.java:58)
... 4更多
org.dasein.cloud.InternalException:java.lang.NullPointerException
位于org.dasein.cloud.azure.AzureX509。(AzureX509.java:64)
位于org.dasein.cloud.azure.AzureMethod.getClient(AzureMethod.java:386)
位于org.dasein.cloud.azure.AzureMethod.getAsStream(AzureMethod.java:124)
位于org.dasein.cloud.azure.azure.testContext(azure.java:258)
位于com.gehcit.dasein.App.main(App.java:27)
原因:java.lang.NullPointerException
位于java.lang.String。(未知源)
位于org.dasein.cloud.azure.AzureX509。(AzureX509.java:58)
... 4更多
这对我很有用:

public static final String storageConnectionString =
    "DefaultEndpointsProtocol=http;"
    + "AccountName=<Your accountname>;"
    + "AccountKey=<Your key>";

public static synchronized String upLoadSelected(String containername, String path, String directory, String pathPartRemover) {
    List<File> filListe = new ArrayList<>();
    if (storageConnectionString.isEmpty() != true) {
        String respons = "";
        try {

            CloudStorageAccount account = CloudStorageAccount.parse(storageConnectionString);
            CloudBlobClient serviceClient = account.createCloudBlobClient();
            CloudBlobContainer container = null;
            String source = "" + path;
            container = serviceClient.getContainerReference("" + containername);
            container.createIfNotExists();
            String temp = "" + directory;
            filListe = listf(source);
            for (File file : filListe) {
                if (file.isDirectory() == true && file.getParentFile().getName().equalsIgnoreCase(temp) != true) {
                    temp = (temp + "\\" + file.getName());
                }
                if (file.isDirectory() != true) {
                    CloudBlockBlob blob = container.getBlockBlobReference("" + file.getCanonicalPath().replace("" + pathPartRemover, ""));
                    File sourceFile = new File("" + file.getAbsolutePath());
        blob.upload(new FileInputStream(sourceFile), sourceFile.length()); 
                }
            }

        } catch (FileNotFoundException fileNotFoundException) {
            respons = respons + "FileNotFoundException encountered: " + fileNotFoundException.getMessage();
        } catch (StorageException storageException) {
            respons = respons + "StorageException encountered: " + storageException.getMessage();
        } catch (IOException e) {
            respons = respons + "IOexception encountered: " + e.getMessage();
        } catch (URISyntaxException e) {
            respons = respons + "URIexception encountered: " + e.getMessage();
        } catch (InvalidKeyException ex) {
            respons = respons + "InvalidKeyException encountered: " + ex.getMessage();
        }

        return respons;
    }
    return "No connection";
}

public static synchronized List<File> listf(String directoryName) {
    File directory = new File(directoryName);
    List<File> resultList = new ArrayList<>();


    File[] fList = directory.listFiles();
    resultList.addAll(Arrays.asList(fList));
    for (File file : fList) {
        if (file.isFile()) {
        } else if (file.isDirectory()) {
            resultList.addAll(listf(file.getAbsolutePath()));
        }
    }
    return resultList;
}
公共静态最终字符串存储连接字符串=
“DefaultEndpointsProtocol=http;”
+“AccountName=;”
+“AccountKey=”;
已选择公共静态同步字符串上载(字符串容器名称、字符串路径、字符串目录、字符串路径PartRemover){
List-filListe=new-ArrayList();
if(storageConnectionString.isEmpty()!=true){
字符串response=“”;
试一试{
CloudStorageAccount=CloudStorageAccount.parse(storageConnectionString);
CloudBlobClient serviceClient=account.createCloudBlobClient();
CloudBlobContainer容器=null;
字符串source=“”+路径;
container=serviceClient.getContainerReference(“+containername”);
container.createIfNotExists();
字符串temp=”“+目录;
filListe=listf(来源);
用于(文件:filListe){
if(file.isDirectory()==true&&file.getParentFile().getName().equalsIgnoreCase(temp)!=true){
temp=(temp+“\\”+file.getName());
}
if(file.isDirectory()!=true){
CloudBlockBlob blob=container.getBlockBlobReference(“+file.getCanonicalPath().replace(“+pathPartRemover,”));
File sourceFile=新文件(“+File.getAbsolutePath());
upload(新文件输入流(sourceFile),sourceFile.length());
}
}
}捕获(FileNotFoundException FileNotFoundException){
respons=respons+“遇到FileNotFoundException:”+FileNotFoundException.getMessage();
}捕获(存储异常存储异常){
respons=respons+“遇到StorageException:”+StorageException.getMessage();
}捕获(IOE异常){
respons=respons+“遇到IOexception:”+e.getMessage();
}捕获(URISyntaxException e){
response=response+“遇到异常:”+e.getMessage();
}捕获(InvalidKeyException ex){
respons=respons+“遇到InvalidKeyException:”+ex.getMessage();
}
返回响应;
}
返回“无连接”;
}
公共静态同步列表listf(字符串目录名){
文件目录=新文件(目录名);
List resultList=new ArrayList();
File[]fList=directory.listFiles();
addAll(Arrays.asList(fList));
for(文件:fList){
if(file.isFile()){
}else if(file.isDirectory()){
addAll(listf(file.getAbsolutePath());
}
}
返回结果列表;
}