在Salesforce APEX中实施Azure Put Blob

问题描述 投票:0回答:1

我正在尝试在Salesforce APEX中实施Azure Put Blob,但出现以下错误。我已经阅读了几乎所有的Microsoft文档,并尝试了许多方法,但最终还是停留在同一点。

我想请人看看。

错误

<?xml version="1.0" encoding="utf-8"?>
<Error>
    <Code>AuthenticationFailed</Code>
    <Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
RequestId:e41a64e6-301e-0047-5cbd-fa2206000000
Time:2020-03-15T11:36:39.3690495Z</Message>
    <AuthenticationErrorDetail>The MAC signature found in the HTTP request '7XBxAbAYZNkrx6mWol+RAYieDNZm9/JN6/4IQ+ygxhk=' is not the same as any computed signature. Server used following string to sign: 'PUT


1002








x-ms-blob-type:BlockBlob
x-ms-date:Sun, 15 Mar 2020 11:31:59 GMT
x-ms-version:2015-02-21
/zaindevtesting/zaindevblob/test.txt
blockid:YmxvY2stMQ==
comp:block'.</AuthenticationErrorDetail>
</Error>

下面是示例代码,关键信息是针对我的测试环境的,因此请注意,您可以使用相同的密钥来执行示例。

public static void putAzureBlob(Blob fileBody)
    {

        String blobName = '/zaindevtesting/zaindevblob/test.txt';
        String urlQueue = 'https://zaindevtesting.blob.core.windows.net/zaindevblob/test.txt';

        string storageKey = 'xxxx==';

       Datetime dt = Datetime.now();
    string formattedDate = dt.formatGMT('EEE, dd MMM yyyy HH:mm:ss') + ' GMT';
    System.debug('formattedDate--->'+formattedDate);
    System.debug('fileLength--->'+ fileLength);
    System.debug('fileLength--->'+ fileType);
    string sts = 'PUT\n\n\n'+fileLength+'\n\n'+fileType+'\n\n\n\n\n\nx-ms-blob-type:BlockBlob\nx-ms-date:' + formattedDate + '\nx-ms-version:2015-02-21\n' + blobName;

    String signature = '';
    Blob unicodeKey = EncodingUtil.base64Decode(storageKey);
    Blob data = Crypto.generateMac('HMACSHA256', Blob.valueOf(sts), unicodeKey);
    signature = EncodingUtil.base64Encode(data);

    string authHeader = 'SharedKey zaindevtesting:' + signature;
    System.debug('authHeader--->'+authHeader);
    HttpRequest req = new HttpRequest();
    req.setMethod('PUT');
   req.setHeader('x-ms-blob-type', 'BlockBlob');
    req.setHeader('x-ms-date', formattedDate);
    req.setHeader('x-ms-version', '2015-02-21');
    req.setHeader('Authorization', authHeader);

    req.setEndpoint(urlQueue);
    req.setBodyAsBlob(fileBody);

    Http http = new Http();

    try
    {
    HTTPResponse res = http.send(req);
    // in the response body you have your blob
    System.debug('Status--->'+res.getStatus());
    // Let's save it as attachment

    System.debug('Body---->'+res.getBody());

    }catch(Exception exp)
    {
        System.debug('Exception --->'+exp);
    }
    }

第二种方法编辑用于生成签名的代码后,我现在得到此错误

<?xml version="1.0" encoding="utf-8"?>
<Error>
<Code>InvalidAuthenticationInfo</Code>
<Message>Authentication information is not given in the correct format. Check the value of Authorization header.
RequestId:c869405b-501e-0013-6328-fbc88c000000
Time:2020-03-16T00:16:06.8896380Z</Message>
</Error>

以下是我的代码

public static void putAzureBlob(Blob fileBody, Integer fileLength, String fileType)
    {

        String blobName = '/zaindevtesting/zaindevblob/test.txt';
        String urlQueue = 'https://zaindevtesting.blob.core.windows.net/zaindevblob/test.txt';

        string storageKey = 'XXXXXXXXXXXX==';

        Datetime dt = Datetime.now();
        string formattedDate = dt.formatGMT('EEE, dd MMM yyyy HH:mm:ss') + ' GMT';
        System.debug('formattedDate--->'+formattedDate);
        System.debug('fileLength--->'+ fileLength);
        System.debug('fileLength--->'+ fileType);
        string sts = 'PUT\n\n\n'+fileLength+'\n\n'+fileType+'\n\n\n\n\n\nx-ms-blob-type:BlockBlob\nx-ms-date:' + formattedDate + '\nx-ms-version:2015-02-21\n' + blobName;

        String signature = '';
        Blob unicodeKey = EncodingUtil.base64Decode(storageKey);
        Blob data = Crypto.generateMac('HMACSHA256', Blob.valueOf(sts), unicodeKey);
        signature = EncodingUtil.base64Encode(data);
        System.debug('signature--->1'+ signature);

        signature = EncodingUtil.urlEncode(signature, 'UTF-8');

         System.debug('signature--->2'+ signature);

        string authHeader = 'SharedKey zaindevtesting:' + signature;

        System.debug('authHeader--->'+authHeader);
        HttpRequest req = new HttpRequest();

       req.setMethod('PUT');
       req.setHeader('x-ms-blob-type', 'BlockBlob');
       req.setHeader('x-ms-date', formattedDate);
       req.setHeader('x-ms-version', '2015-02-21');
       req.setHeader('Authorization', authHeader);
       req.setHeader('Content-Length', String.valueof(fileLength));


        req.setEndpoint(urlQueue);
        req.setBodyAsBlob(fileBody);

        Http http = new Http();

        try
        {
        HTTPResponse res = http.send(req);
        // in the response body you have your blob
        System.debug('Status--->'+res.getStatus());
        // Let's save it as attachment

        System.debug('Body---->'+res.getBody());

        }catch(Exception exp)
        {
            System.debug('Exception --->'+exp);
        }
}

第三种方法这是我用于签名的三种不同方式

 string sts = 'PUT\n\n\n'+fileLength+'\n\n'+fileType+'\n\n\n\n\n\n\nx-ms-blob-type:BlockBlob\nx-ms-date:' + formattedDate + '\nx-ms-version:2015-02-21\n' + blobName;

 //string sts = 'PUT\n\n\n'+fileLength+'\n\n'+fileType+'\n\n\n\n\n\n\nx-ms-date:' + formattedDate + '\nx-ms-version:2015-02-21\n' + blobName;

 // string sts = 'PUT\n\n\n\n\n\n\n\n\n\n\n\nx-ms-date:' + formattedDate + '\nx-ms-version:2015-02-21\n' + blobName+'\nrestype:container\ntimeout:30';

我仍然收到此错误,有一件事,我注意到根据调试我的内容长度为996,但错误显示1002吗?

<?xml version="1.0" encoding="UTF-8"?>
<Error>
   <Code>AuthenticationFailed</Code>
   <Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
RequestId:f7ccfaf6-a01e-0060-113d-fbb84f000000
Time:2020-03-16T02:52:05.0980775Z</Message>
   <AuthenticationErrorDetail>The MAC signature found in the HTTP request 'R+k2bivFW2AH3UriO8m4z8RxPJRDC8uujRc6FCZMEs8=' is not the same as any computed signature. Server used following string to sign: 'PUT


1002

text/plain;charset=UTF-8






x-ms-blob-type:BlockBlob
x-ms-date:Mon, 16 Mar 2020 02:52:04 GMT
x-ms-version:2015-02-21
/zaindevtesting/zaindevblob/test.txt'.</AuthenticationErrorDetail>
</Error>
azure salesforce blob apex
1个回答
0
投票

我在您的代码中看到的一个问题是,您的签名字符串(sts)中没有包含content-lengthcontent-type

string sts = 'PUT\n\n\n\n\n\n\n\n\n\n\nx-ms-blob-type:BlockBlob\nx-ms-date:' + formattedDate + '\nx-ms-version:2015-02-21\n' + blobName+'\nblockid:YmxvY2stMQ==\ncomp:block';

此外,考虑到您执行的是Put Blob操作而不是Put Blob操作,因此不需要在Put Block中包括Put Block

应该是:

blockid:YmxvY2stMQ==\ncomp:block

有关更多详细信息,请参见:sts

© www.soinside.com 2019 - 2024. All rights reserved.