简体   繁体   English

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

[英]How to connect with Azure cloud (blob storage) using dasein API

Can anyone help me with example or sample? 有人可以帮我提供示例或示例吗? I need to get & put files on the blob storage 我需要将文件放在Blob存储中

I have managed to code following, 我设法编写了以下代码,

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

On executing above code I am getting NPE as below 在执行上面的代码时,我得到如下的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

This Works for me: 这对我有用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 ManagedIdentityCredential 连接到 Azure Blob 存储 - How to connect to Azure Blob Storage using ManagedIdentityCredential 如何使用 Java API 禁用 azure 存储帐户的 blob 版本控制? - How to disable the blob versioning for an azure storage account using Java API? Azure:使用存储API查找存储是通用存储还是Blob存储 - Azure: find if storage is general purpose or blob storage using storage API 如何通过 Java/Spring-Boot 使用服务主体连接到 Azure Blob 存储 - How to connect to Azure Blob Storage using Service Principal with Java/Spring-Boot Azure REST API使用java获取存储Blob大小详细信息 - Azure REST API to get Storage Blob Size Details using java 无法使用本地 hadoop 与 azure blob 存储连接 - Unable to connect with azure blob storage with local hadoop 如何使用 Java 在 Google Cloud Storage 中的 Blob 上设置 TTL? - How to set TTL on a Blob in Google Cloud Storage using Java? 使用rest api将块Blob列表放在Azure存储上时,指定的XML在语法上不是有效语法错误 - XML specified is not syntactically valid error while putting block blob list on azure storage using rest api 使用 Java 将文件上传到 Azure Blob 存储 - Upload a file to Azure Blob storage using Java 如何找到天蓝色的云团细节 - How find the azure cloud blob details
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM