使用java的azure blob的StorageException

时间:2015-04-12 09:58:46

标签: java azure azure-storage-blobs

当我尝试为我的存储创建容器时,我得到StorageException。 我创建了azure帐户。 我为blob创建了azure存储空间 我打算简单测试(下) 我在本地机器上制作了这个代码并得到了例外。有什么问题?

public class Test {
public static final String storageConnectionString =
        "DefaultEndpointsProtocol=https;" +
                "AccountName=my_account;" +
                "AccountKey=my_account_key";


public static void main(String[] args) throws StorageException, InvalidKeyException, URISyntaxException {



    pushControll();

}

public static void pushControll() throws URISyntaxException, StorageException, InvalidKeyException {

        CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);

        CloudBlobClient blobClient = storageAccount.createCloudBlobClient();


        CloudBlobContainer container = blobClient.getContainerReference("observer");

        container.create();




    }
}

我得到了StorageException - >:

Exception in thread "main" com.microsoft.azure.storage.StorageException: Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
at com.microsoft.azure.storage.StorageException.translateException(StorageException.java:89)
at com.microsoft.azure.storage.core.StorageRequest.materializeException(StorageRequest.java:307)
at com.microsoft.azure.storage.core.ExecutionEngine.executeWithRetry(ExecutionEngine.java:182)
at com.microsoft.azure.storage.blob.CloudBlobContainer.create(CloudBlobContainer.java:279)
at com.microsoft.azure.storage.blob.CloudBlobContainer.create(CloudBlobContainer.java:252)
at ru.marketirs.model.Test.pushControll(Test.java:40)
at ru.marketirs.model.Test.main(Test.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

使用退出代码1完成处理

我做错了什么?

2 个答案:

答案 0 :(得分:1)

您的代码对我来说没问题。请检查以下两件事:1)确保帐户名称/密钥正确无误2)检查计算机上的时钟并查看它是否运行缓慢。这两件事可能会导致你得到的错误。

答案 1 :(得分:1)

要通过代理,请使用如下所示,它按预期工作,并且已经过测试。

public class AzureUpload {

    // Define the connection-string with your values
    /*public static final String storageConnectionString =
        "DefaultEndpointsProtocol=http;" +
        "AccountName=your_storage_account;" +
        "AccountKey=your_storage_account_key";*/
    public static final String storageConnectionString =
            "DefaultEndpointsProtocol=http;" +
            "AccountName=test2rdrhgf62;" +
            "AccountKey=1gy3lpE7Du1j5ljKiupjhgjghjcbfgTGhbntjnRfr9Yi6GUQqVMQqGxd7/YThisv/OVVLfIOv9kQ==";

    // Define the path to a local file.
    static final String filePath = "D:\\Project\\Supporting Files\\Jar's\\Azure\\azure-storage-1.2.0.jar";
    static final String file_Path = "D:\\Project\\Healthcare\\Azcopy_To_Azure\\data";

    public static void main(String[] args) {
        try
        {
            // Retrieve storage account from connection-string.
            //String storageConnectionString = RoleEnvironment.getConfigurationSettings().get("StorageConnectionString");
            //Proxy httpProxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress("132.186.192.234",8080));
            System.setProperty("http.proxyHost", "102.122.15.234");
            System.setProperty("http.proxyPort", "80");
            System.setProperty("https.proxyUser", "ad001\\empid001");
            System.setProperty("https.proxyPassword", "pass!1");
            // Retrieve storage account from connection-string.
            CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);

            // Create the blob client.
            CloudBlobClient blobClient = storageAccount.createCloudBlobClient();


            // Get a reference to a container.
            // The container name must be lower case
            CloudBlobContainer container = blobClient.getContainerReference("rpmsdatafromhospital");

            // Create the container if it does not exist.
            container.createIfNotExists();

            // Create a permissions object.
            BlobContainerPermissions containerPermissions = new BlobContainerPermissions();

            // Include public access in the permissions object.
            containerPermissions.setPublicAccess(BlobContainerPublicAccessType.CONTAINER);

            // Set the permissions on the container.
            container.uploadPermissions(containerPermissions);

            // Create or overwrite the new file to blob with contents from a local file.
            /*CloudBlockBlob blob = container.getBlockBlobReference("azure-storage-1.2.0.jar");
            File source = new File(filePath);
            blob.upload(new FileInputStream(source), source.length());*/

            String envFilePath = System.getenv("AZURE_FILE_PATH");

            //upload list of files/directory to blob storage
            File folder = new File(envFilePath);
            File[] listOfFiles = folder.listFiles();

            for (int i = 0; i < listOfFiles.length; i++) {
              if (listOfFiles[i].isFile()) {
                System.out.println("File " + listOfFiles[i].getName());

                CloudBlockBlob blob = container.getBlockBlobReference(listOfFiles[i].getName());
                File source = new File(envFilePath+"\\"+listOfFiles[i].getName());
                blob.upload(new FileInputStream(source), source.length());
                System.out.println("File " + listOfFiles[i].getName()+ " upload successful");

              }
              //directory upload
              /*else if (listOfFiles[i].isDirectory()) {
                System.out.println("Directory " + listOfFiles[i].getName());

                CloudBlockBlob blob = container.getBlockBlobReference(listOfFiles[i].getName());
                File source = new File(file_Path+"\\"+listOfFiles[i].getName());
                blob.upload(new FileInputStream(source), source.length());
              }*/
            }

        }catch (Exception e)
        {
            // Output the stack trace.
            e.printStackTrace();
        }
    }
}

.Net或C#然后请将以下代码添加到“App.config”

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>

    <system.net> 
     <defaultProxy enabled="true" useDefaultCredentials="true"> 
       <proxy usesystemdefault="true" /> 
     </defaultProxy>
    </system.net>

</configuration>