Import File from Azure File Share to Cloud BC

hubi777
Member Posts: 8
Hi,
i am using BC20 and try to download a file from Azure File Share.
I can create files, upload data (TempBlob) to Azure and get file information (example below), but how can i download data into my TempBlob?
Regards
procedure ReadFile(FileName: Text; TempBLOB: codeunit "Temp Blob");
var
CryptographyManagement: Codeunit "Cryptography Management";
requestMethod: Text;
HttpRequestMessage: HttpRequestMessage;
HttpHeaders: HttpHeaders;
HttpResponseMessage: HttpResponseMessage;
canonicalizedResource: Text;
canonicalizedHeaders: Text;
urlPath: Text;
HttpClient: HttpClient;
authorizationHeader: Text;
stringToSign: Text;
msVersion: Text;
cha: Text[1];
keyss: Text;
dateInRfc1123Format: Text;
uri: Text;
ResponseText: Text;
begin
cha[1] := 10;
msVersion := '2015-02-21';
keyss := GetAccountAccessKey();
dateInRfc1123Format := GetUTCDateTimeText();
requestMethod := 'GET';
urlPath := GetAccountSharedRessource() + '/data/' + FileName;
CanonicalizedResource := ConvertStr(StrSubstNo('/%1/%2', GetAccountName(), urlPath), '?=&', cha + ':' + cha);
canonicalizedHeaders := 'x-ms-date:' + dateInRfc1123Format + Format(cha) +
'x-ms-version:' + msVersion;
stringToSign := (requestMethod + Format(cha) +
Format(cha) +
Format(cha) +
Format(cha) +
Format(cha) +
Format(cha) +
Format(cha) +
Format(cha) +
Format(cha) +
Format(cha) +
Format(cha) +
Format(cha) +
canonicalizedHeaders + Format(cha) +
canonicalizedResource);
authorizationHeader := 'SharedKey ' + GetAccountName() + ':' + CryptographyManagement.GenerateBase64KeyedHashAsBase64String(stringToSign, keyss, 2);
uri := StrSubstNo('https://%1.file.core.windows.net/%2', GetAccountName(), urlPath);
HttpRequestMessage.SetRequestUri(uri);
HttpRequestMessage.Method := requestMethod;
HttpHeaders.Clear();
HttpRequestMessage.GetHeaders(HttpHeaders);
HttpHeaders.Add('Authorization', authorizationHeader);
HttpHeaders.Add('x-ms-date', dateInRfc1123Format);
HttpHeaders.Add('x-ms-version', msVersion);
HttpClient.Send(HttpRequestMessage, HttpResponseMessage);
if not HttpResponseMessage.IsSuccessStatusCode then
Error(HttpResponseMessage.ReasonPhrase)
else begin
Message(HttpResponseMessage.ReasonPhrase);
HttpResponseMessage.Content.ReadAs(ResponseText);
message('%1', ResponseText);
end;
end;
i am using BC20 and try to download a file from Azure File Share.
I can create files, upload data (TempBlob) to Azure and get file information (example below), but how can i download data into my TempBlob?
Regards
procedure ReadFile(FileName: Text; TempBLOB: codeunit "Temp Blob");
var
CryptographyManagement: Codeunit "Cryptography Management";
requestMethod: Text;
HttpRequestMessage: HttpRequestMessage;
HttpHeaders: HttpHeaders;
HttpResponseMessage: HttpResponseMessage;
canonicalizedResource: Text;
canonicalizedHeaders: Text;
urlPath: Text;
HttpClient: HttpClient;
authorizationHeader: Text;
stringToSign: Text;
msVersion: Text;
cha: Text[1];
keyss: Text;
dateInRfc1123Format: Text;
uri: Text;
ResponseText: Text;
begin
cha[1] := 10;
msVersion := '2015-02-21';
keyss := GetAccountAccessKey();
dateInRfc1123Format := GetUTCDateTimeText();
requestMethod := 'GET';
urlPath := GetAccountSharedRessource() + '/data/' + FileName;
CanonicalizedResource := ConvertStr(StrSubstNo('/%1/%2', GetAccountName(), urlPath), '?=&', cha + ':' + cha);
canonicalizedHeaders := 'x-ms-date:' + dateInRfc1123Format + Format(cha) +
'x-ms-version:' + msVersion;
stringToSign := (requestMethod + Format(cha) +
Format(cha) +
Format(cha) +
Format(cha) +
Format(cha) +
Format(cha) +
Format(cha) +
Format(cha) +
Format(cha) +
Format(cha) +
Format(cha) +
Format(cha) +
canonicalizedHeaders + Format(cha) +
canonicalizedResource);
authorizationHeader := 'SharedKey ' + GetAccountName() + ':' + CryptographyManagement.GenerateBase64KeyedHashAsBase64String(stringToSign, keyss, 2);
uri := StrSubstNo('https://%1.file.core.windows.net/%2', GetAccountName(), urlPath);
HttpRequestMessage.SetRequestUri(uri);
HttpRequestMessage.Method := requestMethod;
HttpHeaders.Clear();
HttpRequestMessage.GetHeaders(HttpHeaders);
HttpHeaders.Add('Authorization', authorizationHeader);
HttpHeaders.Add('x-ms-date', dateInRfc1123Format);
HttpHeaders.Add('x-ms-version', msVersion);
HttpClient.Send(HttpRequestMessage, HttpResponseMessage);
if not HttpResponseMessage.IsSuccessStatusCode then
Error(HttpResponseMessage.ReasonPhrase)
else begin
Message(HttpResponseMessage.ReasonPhrase);
HttpResponseMessage.Content.ReadAs(ResponseText);
message('%1', ResponseText);
end;
end;
0
Comments
-
Hello @hubi777,
Take a look to this:
https://github.com/microsoft/ALAppExtensions/tree/main/Modules/System/Azure Blob Services API
Regards0 -
Hi,
i found a solution to upload files but Azure only allows streams up to 4MB. If the stream is larger, it must be split into chunks.
How can is split the InStream into multiple chunks?
Regards
procedure WriteFile(FileName: Text; TempBLOB: codeunit "Temp Blob");
var
CryptographyManagement: Codeunit "Cryptography Management";
requestMethod: Text;
HttpRequestMessage: HttpRequestMessage;
HttpHeaders: HttpHeaders;
HttpResponseMessage: HttpResponseMessage;
canonicalizedResource: Text;
canonicalizedHeaders: Text;
urlPath: Text;
HttpClient: HttpClient;
HttpContent: HttpContent;
ContentHeaders: HttpHeaders;
authorizationHeader: Text;
stringToSign: Text;
msVersion: Text;
cha: Text[1];
contentLength: Integer;
keyss: Text;
dateInRfc1123Format: Text;
uri: Text;
Range: Text;
InStream: InStream;
begin
if not TempBlob.HasValue() then
exit;
cha[1] := 10;
msVersion := '2015-02-21';
keyss := GetAccountAccessKey();
dateInRfc1123Format := GetUTCDateTimeText();
requestMethod := 'PUT';
urlPath := GetAccountSharedRessource() + '/data/' + FileName + '?comp=range';
CanonicalizedResource := ConvertStr(StrSubstNo('/%1/%2', GetAccountName(), urlPath), '?=', cha + ':');
TempBlob.CreateInStream(InStream, TextEncoding::UTF8);
HttpContent.WriteFrom(InStream);
ContentLength := TempBlob.Length();
ContentHeaders.Clear();
HttpContent.GetHeaders(ContentHeaders);
ContentHeaders.Add('Content-Length', Format(ContentLength, 0, 9));
ContentHeaders.Remove('Content-Type');
HttpRequestMessage.Content := HttpContent;
Range := 'bytes=0-' + Format(contentLength - 1, 0, 9);
canonicalizedHeaders := 'x-ms-date:' + dateInRfc1123Format + Format(cha) +
'x-ms-range:' + Range + Format(cha) +
'x-ms-version:' + msVersion + Format(cha) +
'x-ms-write:' + 'update';
stringToSign := (requestMethod + Format(cha) +
Format(cha) +
Format(cha) +
Format(contentLength, 0, 9) + Format(cha) +
Format(cha) +
Format(cha) +
Format(cha) +
Format(cha) +
Format(cha) +
Format(cha) +
Format(cha) +
Format(cha) +
canonicalizedHeaders + Format(cha) +
canonicalizedResource);
authorizationHeader := 'SharedKey ' + GetAccountName() + ':' + CryptographyManagement.GenerateBase64KeyedHashAsBase64String(stringToSign, keyss, 2);
uri := StrSubstNo('https://%1.file.core.windows.net/%2', GetAccountName(), urlPath);
HttpRequestMessage.SetRequestUri(uri);
HttpRequestMessage.Method := requestMethod;
HttpHeaders.Clear();
HttpRequestMessage.GetHeaders(HttpHeaders);
HttpHeaders.Add('Authorization', authorizationHeader);
HttpHeaders.Add('x-ms-date', dateInRfc1123Format);
HttpHeaders.Add('x-ms-range', Range);
HttpHeaders.Add('x-ms-version', msVersion);
HttpHeaders.Add('x-ms-write', 'update');
HttpClient.Send(HttpRequestMessage, HttpResponseMessage);
if not HttpResponseMessage.IsSuccessStatusCode then
Error(HttpResponseMessage.ReasonPhrase)
//else
// Message(HttpResponseMessage.ReasonPhrase);
end;
0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions