如何在没有SDK的情况下调用Windows Azure Storage RestFul服务?

时间:2017-02-08 15:10:03

标签: rest api azure sdk storage

我尝试使用Windows Azure,就像Salesforce.com的存储一样。

我讨论了文档,我只能看到从SDK(Java,.Net,JS等)示例调用azure rest api的调用。

我需要将Salesforce与Windows Azure存储集成,但Azure不具备适用于Salesforce.com的SDK

从Salesforce.com允许调用休息服务,但调用Azure Rest服务的过程需要一个或多个图书馆。

Exameple:

Azure存储服务的身份验证需要:

  • 标题:日期标题和授权标题

授权标题需要两个元素

  • SharedKey
  • 帐户名称
  • 授权=" [SharedKey | SharedKeyLite]:"

SharedKey和帐户名称提供转换:

  • HMAC-SHA256转换
  • 通过UTF-8编码

对于此转换,文档引用SDK Librarys,换句话说,在Salesforce.com中不存在Java Class或.Net Class类型帮助程序。

请,我需要一个例子来调用没有sdk的身份验证服务

抱歉我的英语不好。

访问:https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/authentication-for-the-azure-storage-services

2 个答案:

答案 0 :(得分:0)

  

我需要一个示例来调用不带sdk的身份验证服务

我们可以生成签名字符串,并为执行Azure存储服务的请求指定Authorization标头,而无需安装SDK。这是一个列出容器的简单工作示例,您可以参考我的generateAuthorizationHeader函数和Authentication for the Azure Storage Services来构造签名字符串。

string StorageAccount = "mystorageaccount";
string StorageKey = "my storage key";

string requestMethod = "GET";
string mxdate = "";
string storageServiceVersion = "2014-02-14";

protected void btnlist_Click(object sender, EventArgs e)
{
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(string.Format(CultureInfo.InvariantCulture,
    "https://{0}.blob.core.windows.net/?comp=list",
    StorageAccount
    ));

    req.Method = requestMethod;

    //specify request header
    string AuthorizationHeader = generateAuthorizationHeader();
    req.Headers.Add("Authorization", AuthorizationHeader);
    req.Headers.Add("x-ms-date", mxdate);
    req.Headers.Add("x-ms-version", storageServiceVersion);

    using (HttpWebResponse response = (HttpWebResponse)req.GetResponse())
    {
    var stream = response.GetResponseStream();

    StreamReader reader = new StreamReader(stream);
    string content = reader.ReadToEnd();

    StringReader theReader = new StringReader(content);
    DataSet theDataSet = new DataSet();
    theDataSet.ReadXml(theReader);

    DataTable dt = theDataSet.Tables[2];

    }
}

public string generateAuthorizationHeader()
{
    mxdate = DateTime.UtcNow.ToString("R");

    string canonicalizedHeaders = string.Format(
        "x-ms-date:{0}\nx-ms-version:{1}",
        mxdate,
        storageServiceVersion);

    string canonicalizedResource = string.Format("/{0}/\ncomp:list", StorageAccount);

    string stringToSign = string.Format(
    "{0}\n\n\n\n\n\n\n\n\n\n\n\n{1}\n{2}",
    requestMethod,
    canonicalizedHeaders,
    canonicalizedResource);

    HMACSHA256 hmac = new HMACSHA256(Convert.FromBase64String(StorageKey));

    string signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));

    String authorization = String.Format("{0} {1}:{2}",
    "SharedKey",
    StorageAccount,
    signature
    );

    return authorization;
}

此外,请参阅Azure Storage Services REST API Reference以了解有关通过REST API对Azure存储服务进行编程访问的更多信息。

答案 1 :(得分:0)

我找到了解决这个问题的方法。

你应该使用Shared Sing,在这里解释一下:

  1. 进入Portal Azure
  2. 打开帐户存储
  3. 在一般信息中,点击“分享唱歌访问权”
  4. 启用所需的所有权限(仅在我的情况下启用“文件”)
  5. 启用所需的所有资源权限(在我的情况下,启用“服务,容器和对象”)
  6. 定义和开始日期和结束日期(这是共享密钥有效的时间空间)
  7. 定义协议类型(在我的情况下使用HTTPS)
  8. Clic on“Generate SAS”按钮
  9. 在此过程之后,您将获得如下标记: ?SV = 2016年5月31日&安培; SS = F&安培; SRT = SCO&安培; SP = rwdlc&安培; SE = 2017-11-28T04:29:49Z&安培; ST = 2017-02-18T20:29:49Z&安培; SPR = HTTPS&安培; SIG的= rt7Loxo1MHGJqp0F6ryLhYAmOdRreyiYT418ybDN2OI%3D

    1. 你必须像Autentication一样使用这个令牌
    2. 示例呼叫代码列出内容:

          public with sharing class CallAzureRestDemo {
      
      public string token = '&sv=2016-05-31&ss=f&srt=sco&sp=rwdlc&se=2017-02-19T04:00:44Z&st=2017-02-18T20:00:44Z&spr=https&sig=GTWGQc5GOAvQ0BIMxMbwUpgag5AmUVjrfZc56nHkhjI%3D';
      //public Integer batchSize;
      
      
      public CallAzureRestDemo(){}    
      
      
      public void getlistcontent(String endpoint)
      {        
          // Create HTTP GET request
          HttpRequest req = new HttpRequest();
          req.setMethod('GET');
          req.setEndpoint(endpoint+token);
      
          Http http = new Http();
          HTTPResponse res;
      
          System.debug(LoggingLevel.INFO, '@@RESPONSE: '+res);
      
          // only do this if not running in a test method
          if(!Test.isRunningTest())
          {
              System.debug(LoggingLevel.INFO, 'Sending the message to Azure');
              res = http.send(req);                
              System.debug(LoggingLevel.INFO, 'http.send result status: ' + res.getStatus());
          }
          else
          {
              System.debug(LoggingLevel.INFO, 'Running in a test so not sending the message to Azure');
          }
      }    
      

      }

      示例TestMethod:

      @isTest
      

      私人课程Test_CallAzureRestDemo {

      static testMethod void myUnitTest() {
      
          CallAzureRestDemo oRest = new CallAzureRestDemo();          
      
          try{
              //Call the method and set endpoint
              oRest.getlistcontent('https://accountstoragecomex.file.core.windows.net/?comp=list');
          }catch(Exception e){
              System.debug('@@'+e);
          }
        }
      

      }

      回应示例:

      20:15:47.64(79388244)| CALLOUT_REQUEST | [100] | System.HttpRequest [Endpoint = h t t p s://accountstoragecomex.file。 core.windows.net/?comp=list&sv=2016-05-31&ss=f&srt=sco&sp=rwdlc&se=2017-02-19T04:00:44Z&st=2017-02-18T20 :00:44Z& spr = h t t p s& sig = GTWGQc5GOAvQ0BIMxMbwUpgag5AmUVjrfZc56nHkhjI%3D,Method = GET] 20:15:47.64(395755012)| CALLOUT_RESPONSE | [100] | System.HttpResponse [Status = OK,StatusCode = 200]

      示例呼叫服务“文件 - 获取列表共享”

      Call To List Content

      再一次,抱歉我的英语不好。