gpt4 book ai didi

azure - 在 Salesforce APEX 中实现 Azure Put Blob

转载 作者:行者123 更新时间:2023-12-03 01:32:50 26 4
gpt4 key购买 nike

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

我希望有人能看一下。

错误

<?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>

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

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>

最佳答案

我在您的代码中看到的一个问题是您没有在签名字符串 (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 Block操作时,您不需要在 sts 中包含 blockid:YmxvY2stMQ==\ncomp:block

应该是:

string sts = 'PUT\n\n\n' + content-length-here (745 based on the file you're trying to upload) + '\n\n' + content-type-here (text/plain based on the file you're trying to upload + '\n\n\n\n\n\nx-ms-blob-type:BlockBlob\nx-ms-date:' + formattedDate + '\nx-ms-version:2015-02-21\n' + blobName;

更多详情请参阅:https://learn.microsoft.com/en-us/rest/api/storageservices/authorize-with-shared-key .

关于azure - 在 Salesforce APEX 中实现 Azure Put Blob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60692538/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com