我可以从Azure存储中获取具有休息响应的文件吗?

时间:2014-10-11 21:03:22

标签: android rest https azure-storage

我有一个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);
        }
    }
}

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

1 个答案:

答案 0 :(得分:0)

由于您直接使用REST API,我强烈建议您阅读相应的API文档:Get Blob。它看起来不像您正在进行共享密钥身份验证,因此您有两个选择:

  1. 您的容器需要允许匿名访问。
  2. 您需要使用共享访问签名。
  3. 有关身份验证的详情,请参阅Authentication for the Azure Storage ServicesDelegating Access with a Shared Access Signature