Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
156 views
in Technique[技术] by (71.8m points)

java - understanding Azure File Storage: UnknownHostException

Edit: I just realised that AccountName, below, refers to a storage account, which I have not yet created. I thought it was just the general azure "Account", that I have. Disappointing.

I am working through this Java/Azure File Storage example. I have been met with a problem, which I have been unable to find a resolution for:

In English: https://docs.microsoft.com/en-us/azure/storage/files/storage-java-how-to-use-file-storage?tabs=java

createFileShare exception: java.net.UnknownHostException: failed to resolve 'my-provided-accountname.file.core.windows.net' after 2 queries

I am unsure of what exactly to provide for AccountName and, to a lesser extent, AccountKey, which I think is correct.

My obfiscated code:

    public static final String connectStr =
        "DefaultEndpointsProtocol=https;" +
                "AccountName=my-provided-accountname;" +
                "AccountKey=87D2A2E999180C4A624E1A8153CEBD6";

public static void main(String[] args) {
    SpringApplication.run(AzureFileStorageApplication.class, args);
    ShareClient shareClient = new ShareClientBuilder()
            .connectionString(connectStr).shareName("testfilestorage")
            .buildClient();
    createFileShare(connectStr,"hello1A");
}
public static Boolean createFileShare(String connectStr, String shareName)
{
    try
    {
        ShareClient shareClient = new ShareClientBuilder()
                .connectionString(connectStr).shareName(shareName)
                .buildClient();

        shareClient.create();
        System.out.println(shareClient);
        return true;
    }
    catch (Exception e)
    {
        System.out.println("createFileShare exception: " + e.getMessage());
        return false;
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First you should create an Azure Storage Account https://docs.microsoft.com/en-us/azure/storage/common/storage-account-create?tabs=azure-portal

and paste it in the code. Example:

AccountName=myazurestorageaccount;

When Azure Storage Account is created, get AccountKey(as described in link below)

https://docs.microsoft.com/en-us/azure/storage/common/storage-account-keys-manage?tabs=azure-portal

and paste it in the code. Example:

AccountKey=87D2A2E999180C4A624E1A8153CEBD6;

Java ShareClientBuilder documentation describes instantiating a Share Client with connection string. https://docs.microsoft.com/en-us/java/api/com.azure.storage.file.share.shareclientbuilder?view=azure-java-stable

String connectionString = "DefaultEndpointsProtocol=https;AccountName={name};AccountKey={key};"
 + "EndpointSuffix={core.windows.net}";
ShareClient shareClient = new ShareClientBuilder()
 .connectionString(connectionString).shareName("myshare")
 .buildClient();

In you case you should append "EndpointSuffix=core.windows.net"; to your connectStr.

public static final String connectStr = 
    "DefaultEndpointsProtocol=https;" +
            "AccountName=my-provided-accountname;" +
            "AccountKey=87D2A2E999180C4A624E1A8153CEBD6;" + "EndpointSuffix=core.windows.net";

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...