Import File from Azure File Share to Cloud BC

hubi777hubi777 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;

Comments

  • hubi777hubi777 Member Posts: 8
    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;

Sign In or Register to comment.