【问题标题】:How to connect with Azure cloud (blob storage) using dasein API如何使用 dasein API 连接 Azure 云(blob 存储)
【发布时间】:2014-04-25 16:16:25
【问题描述】:

谁能帮助我提供示例或示例?我需要获取文件并将其放入 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

【问题讨论】:

    标签: java azure azure-blob-storage


    【解决方案1】:

    这对我有用:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-08-03
      • 1970-01-01
      • 2021-11-23
      • 2018-06-30
      • 2020-05-28
      • 2020-07-04
      • 2015-05-29
      相关资源
      最近更新 更多