Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

Google Cloud Storage: Update Object Metadata

See more Google Cloud Storage Examples

Demonstrates how to update (edit) the metadata associated with an object in a Google Cloud Storage bucket.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
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.BinData,
  Chilkat.HttpResponse,
  Chilkat.StringBuilder,
  Chilkat.JsonObject;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  http: THttp;
  bdRequestBody: TBinData;
  resp: THttpResponse;
  sbResponseBody: TStringBuilder;
  jResp: TJsonObject;
  respStatusCode: Integer;

begin
  success := False;

  //  This example assumes the Chilkat API to have been previously unlocked.
  //  See Global Unlock Sample for sample code.

  http := THttp.Create;

  //  Implements the following CURL command:

  //  curl -X PATCH --data-binary @JSON_FILE_NAME \
  //    -H "Authorization: Bearer OAUTH2_TOKEN" \
  //    -H "Content-Type: application/json" \
  //    "https://storage.googleapis.com/storage/v1/b/BUCKET_NAME/o/OBJECT_NAME"

  //  Use the following online tool to generate HTTP code from a CURL command
  //  Convert a cURL Command to HTTP Source Code

  bdRequestBody := TBinData.Create;
  success := bdRequestBody.LoadFile('JSON_FILE_PATH');
  if (success <> True) then
    begin
      WriteLn('Failed to load JSON_FILE_PATH');
      Exit;
    end;

  //  Adds the "Authorization: Bearer OAUTH2_TOKEN" header.
  http.AuthToken := 'OAUTH2_TOKEN';

  resp := THttpResponse.Create;
  success := http.HttpBd('PATCH','https://storage.googleapis.com/storage/v1/b/BUCKET_NAME/o/OBJECT_NAME',bdRequestBody,'application/json',resp);
  if (success = False) then
    begin
      WriteLn(http.LastErrorText);
      Exit;
    end;

  sbResponseBody := TStringBuilder.Create;
  resp.GetBodySb(sbResponseBody);

  jResp := TJsonObject.Create;
  jResp.LoadSb(sbResponseBody);
  jResp.EmitCompact := False;

  WriteLn('Response Body:');
  WriteLn(jResp.Emit());

  respStatusCode := resp.StatusCode;
  WriteLn('Response Status Code = ' + respStatusCode);
  if (respStatusCode >= 400) then
    begin
      WriteLn('Response Header:');
      WriteLn(resp.Header);
      WriteLn('Failed.');
      Exit;
    end;


  http.Free;
  bdRequestBody.Free;
  resp.Free;
  sbResponseBody.Free;
  jResp.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.