问题描述
我正在尝试使用Business Central Client中的Rest API通过SharedKey将blob存储到我的Azure存储中。 香港专业教育学院已经将其作为c#代码示例,但无法在AL中重现。这是我收到的错误:
/ NavHttpClient请求失败/
到目前为止我的代码
procedure PutBlob(StorageAccount: Text; ContainerName: Text; StorageKey: Text; BlobName: Text) worked: Boolean
var
TmpBlob: Codeunit "Temp Blob"; // Going to be a parameter
InStr: InStream;
WebResponse: HttpResponseMessage;
WebRequest: HttpRequestMessage;
begin
Method := 'PUT';
UtcNow := Format(CurrentDateTime,9); //Going to use Type Helper CU
RequestUri := StrSubstNo('http://%1.blob.core.windows.net/%2/%3',StorageAccount,ContainerName,BlobName);
WebRequest.SetRequestUri(RequestUri);
WebRequest.Method(Method);
TestText := 'Hello World'; // For Example Content
TmpBlob.CreateInStream(InStr);
InStr.Read(TestText);
WebContent.WriteFrom(InStr);
WebContent.GetHeaders(WebHeaders);
WebHeaders.Clear();
WebHeaders.Add('Content-Length','11'); // Static for testing - Going to be dynamically
WebHeaders.Add('Content-Type','text/plain; charset=UTF-8'); // Static for testing - Going to be dynamically
WebRequest.Content := WebContent;
WebRequest.GetHeaders(WebHeaders);
WebHeaders.Add('Authorization',AuthorizationHeader(Method,UtcNow,StorageKey,BlobName));
WebHeaders.Add('x-ms-blob-type','BlockBlob');
WebHeaders.Add('x-ms-date',UtcNow);
WebHeaders.Add('x-ms-version','2014-02-14');
worked := true;
worked := Client.Send(WebRequest,WebResponse);
if not WebResponse.IsSuccessstatusCode() then begin
worked := false;
error((StrSubstNo('/%1/%2/',FailedToGetBlobErr,WebResponse.ReasonPhrase())));
end;
end;
授权功能
local procedure AuthorizationHeader(Method: Text; UtcNow: text; StorageAccount: Text; StorageKey: text; ContainerName: text; BlobName: text) AuthorizationHeader: Text
var
Hmac: Codeunit "Cryptography Management";
HeaderResource: text;
UrlRessource: Text;
StringToSign: Text;
Signature: Text;
HashAlgorithmType: Option HMACMD5,HMACSHA1,HMACSHA256,HMACSHA384,HMACSHA512;
begin
HeaderResource := StrSubstNo('x-ms-blob-type:BlockBlob\nx-ms-date:%1\nx-ms-version:2014-02-14',UtcNow);
UrlRessource := StrSubstNo('/%1/%2/%3',BlobName);
StringToSign := StrSubstNo('%1\n\n\n%2\n\n%3\n\n\n\n\n\n\n%4\n%5',Method,'11','text/plain; charset=UTF-8',HeaderResource,UrlRessource);
Signature := Hmac.GenerateBase64KeyedHashAsBase64String(StringToSign,HashAlgorithmType::HMACSHA256);
AuthorizationHeader := StrSubstNo('SharedKey %1:%2',Signature);
end;
全局变量:
var
TestText: Text;
WebHeaders: HttpHeaders;
Method: Text;
UtcNow: Text;
RequestUri: Text;
WebContent: HttpContent;
Client: HttpClient;
FailedToGetBlobErr: Label 'Failed to upload to blob: ';
我已经尝试解决此问题了几个小时,但无法修复。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)