【问题标题】:Can I get file from Azure Storage with rest response?我可以从 Azure 存储中获取带有休息响应的文件吗?
【发布时间】:2014-12-06 19:03:13
【问题描述】:

我有一个 Android 2.1 应用程序。 我需要该应用程序从我的 Azure 帐户中的存储中下载文件。 我不能使用 Azure 提供的 API,因为我的 Android 版本太旧了。 因此,我尝试通过 Rest 服务调用下载该文件。 我不知道为什么我没有成功地做到这一点

那是我试图做的:

private String _url = "https://fake_azure_acount_name.blob.core.windows.net/";
private final String _thisClassName = "StorageProvider";
private final String _azurAcountName = "fake_azure_acount_name";

public String GetImage(String imagePath)
{
    String image = ExecuteGetRequest(imagePath, "Could not succeed get the requested image");
    return image;
}

private String ExecuteGetRequest(String getRequest, String errorMassageIfFailes)
{
    DefaultHttpClient httpclient = new DefaultHttpClient();

    try 
    {
        HttpClient httpClient = new DefaultHttpClient();
        HttpResponse httpResponse = httpClient.execute(new HttpGet(_url + getRequest));
        BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8"));
        String json = reader.readLine();
        JSONTokener tokener = new JSONTokener(json);
        JSONArray finalResult = new JSONArray(tokener);
        return json;
    }
    catch(Exception ex)
    {
        Log.e(_thisClassName, errorMassageIfFailes, ex);
        return null;
    }
    finally 
    {
        try
        {
          httpclient.getConnectionManager().shutdown();
        }
        catch(Exception ex)
        {
            Log.e(_thisClassName, "Failed to close http connection", ex);
        }
    }
}

这样做的正确方法是什么?

【问题讨论】:

    标签: android rest https azure-storage


    【解决方案1】:

    由于您直接使用 REST API,我强烈建议您阅读相应的 API 文档:Get Blob。您似乎没有进行共享密钥身份验证,因此您有两种选择:

    1. 您的容器需要允许匿名访问。
    2. 您需要使用共享访问签名。

    有关身份验证的更多信息,请参阅Authentication for the Azure Storage ServicesDelegating Access with a Shared Access Signature

    【讨论】:

    • Serdar,非常感谢您的回答。我试图通过博客。我不知道为什么,但我很复杂。你有在 Java 中构建该请求的示例吗?再次感谢
    猜你喜欢
    • 2021-05-22
    • 1970-01-01
    • 2021-04-19
    • 1970-01-01
    • 1970-01-01
    • 2012-05-12
    • 1970-01-01
    • 2018-11-05
    • 2013-12-04
    相关资源
    最近更新 更多