Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Initiate Resumable Upload Session
See more Google Cloud Storage Examples
Initiate a Google Cloud Storage resumable upload session..Chilkat Pascal (Lazarus/Delphi) Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.Http,
Chilkat.HttpResponse,
Chilkat.JsonObject;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
http: THttp;
jsonToken: TJsonObject;
jsonMetaData: TJsonObject;
url: string;
resp: THttpResponse;
statusCode: Integer;
sessionUrl: string;
begin
success := False;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
http := THttp.Create;
jsonToken := TJsonObject.Create;
success := jsonToken.LoadFile('qa_data/tokens/googleCloudStorage.json');
if (success = False) then
begin
WriteLn(jsonToken.LastErrorText);
Exit;
end;
jsonMetaData := TJsonObject.Create;
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 := THttpResponse.Create;
success := http.HttpJson('POST',url,jsonMetaData,'application/json',resp);
if (success = False) then
begin
WriteLn(http.LastErrorText);
Exit;
end;
statusCode := resp.StatusCode;
WriteLn('response status code = ' + statusCode);
sessionUrl := '';
if (statusCode <> 200) then
begin
WriteLn(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');
WriteLn('Session URL = ' + sessionUrl);
end;
http.Free;
jsonToken.Free;
jsonMetaData.Free;
resp.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.