Delphi ActiveX
Delphi ActiveX
Initiate Resumable Upload Session
See more Google Cloud Storage Examples
Initiate a Google Cloud Storage resumable upload session..Chilkat Delphi ActiveX Downloads
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Chilkat_TLB;
...
procedure TForm1.Button1Click(Sender: TObject);
var
success: Integer;
http: TChilkatHttp;
jsonToken: TChilkatJsonObject;
jsonMetaData: TChilkatJsonObject;
url: WideString;
resp: TChilkatHttpResponse;
statusCode: Integer;
sessionUrl: WideString;
begin
success := 0;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
http := TChilkatHttp.Create(Self);
jsonToken := TChilkatJsonObject.Create(Self);
success := jsonToken.LoadFile('qa_data/tokens/googleCloudStorage.json');
if (success = 0) then
begin
Memo1.Lines.Add(jsonToken.LastErrorText);
Exit;
end;
jsonMetaData := TChilkatJsonObject.Create(Self);
jsonMetaData.UpdateString('contentType','image/jpeg');
// Adds the "Authorization: Bearer <access_token>" header..
http.AuthToken := jsonToken.StringOf('access_token');
http.SetUrlVar('bucket_name','chilkat-bucket-b');
http.SetUrlVar('object_name','penguins2.jpg');
url := 'https://storage.googleapis.com/upload/storage/v1/b/{$bucket_name}/o?uploadType=resumable&name={$object_name}';
resp := TChilkatHttpResponse.Create(Self);
success := http.HttpJson('POST',url,jsonMetaData.ControlInterface,'application/json',resp.ControlInterface);
if (success = 0) then
begin
Memo1.Lines.Add(http.LastErrorText);
Exit;
end;
statusCode := resp.StatusCode;
Memo1.Lines.Add('response status code = ' + IntToStr(statusCode));
sessionUrl := '';
if (statusCode <> 200) then
begin
Memo1.Lines.Add(resp.BodyStr);
end
else
begin
// The session URL will be used to upload the file in chunks, in subsequent HTTP POSTs...
sessionUrl := resp.GetHeaderField('Location');
Memo1.Lines.Add('Session URL = ' + sessionUrl);
end;
end;