且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用Azure云(Blob存储)连接使用此在API

更新时间:2023-02-08 23:27:41

这对我的作品:

 公共静态最后弦乐storageConnectionString =
    DefaultEndpointsProtocol = HTTP;
    +帐户名= lt;您的帐户名取代;
    +AccountKey =<您的钥匙>中;公共静态同步字符串upLoadSelected(字符串容器名称,路径字符串,字符串目录字符串pathPartRemover){
    清单<文件> filListe =新的ArrayList<>();
    如果(storageConnectionString.isEmpty()!= TRUE){
        串respons =;
        尝试{            CloudStorageAccount帐户= CloudStorageAccount.parse(storageConnectionString);
            CloudBlobClient serviceClient = account.createCloudBlobClient();
            CloudBlobContainer容器= NULL;
            字符串源=+路径;
            容器= serviceClient.getContainerReference(+容器名称);
            container.createIfNotExists();
            字符串TEMP =+目录;
            filListe = LISTF(源);
            对于(文件文件:filListe){
                如果(file.isDirectory()==真&放大器;&安培;!file.getParentFile()的getName()equalsIgnoreCase(TEMP)=真){
                    临时=(温度+\\\\+ file.getName());
                }
                如果(file.isDirectory()!= TRUE){
                    CloudBlockBlob BLOB = container.getBlockBlobReference(+ file.getCanonicalPath()代替(+ pathPartRemover,));
                    文件的资源文件=新的文件(+ file.getAbsolutePath());
        blob.upload(新的FileInputStream(的SourceFile),sourceFile.length());
                }
            }        }赶上(FileNotFoundException异常FileNotFoundException异常){
            respons = respons +FileNotFoundException异常遭遇:+ fileNotFoundException.getMessage();
        }赶上(StorageException storageException){
            respons = respons +StorageException遭遇:+ storageException.getMessage();
        }赶上(IOException异常五){
            respons = respons +遇到IOException异常:+ e.getMessage();
        }赶上(的URISyntaxException E){
            respons = respons +URIexception遭遇:+ e.getMessage();
        }赶上(InvalidKeyException将前){
            respons = respons +InvalidKeyException将遭遇:+ ex.getMessage();
        }        返回respons;
    }
    返回无连接;
}公共静态同步列表<文件> LISTF(字符串目录名){
    文件目录=新的文件(目录名);
    清单<文件> resultList =新的ArrayList<>();
    文件[] = FLIST directory.listFiles();
    resultList.addAll(Arrays.asList(FLIST));
    对于(文件文件:FLIST){
        如果(file.isFile()){
        }否则如果(file.isDirectory()){
            resultList.addAll(LISTF(file.getAbsolutePath()));
        }
    }
    返回resultList;
}

Can anyone help me with example or sample? I need to get & put files on the blob storage

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

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