Delphi ActiveX
Delphi ActiveX
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 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;
bdRequestBody: TChilkatBinData;
resp: TChilkatHttpResponse;
sbResponseBody: TChilkatStringBuilder;
jResp: TChilkatJsonObject;
respStatusCode: Integer;
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);
// 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 := TChilkatBinData.Create(Self);
success := bdRequestBody.LoadFile('JSON_FILE_PATH');
if (success <> 1) then
begin
Memo1.Lines.Add('Failed to load JSON_FILE_PATH');
Exit;
end;
// Adds the "Authorization: Bearer OAUTH2_TOKEN" header.
http.AuthToken := 'OAUTH2_TOKEN';
resp := TChilkatHttpResponse.Create(Self);
success := http.HttpBd('PATCH','https://storage.googleapis.com/storage/v1/b/BUCKET_NAME/o/OBJECT_NAME',bdRequestBody.ControlInterface,'application/json',resp.ControlInterface);
if (success = 0) then
begin
Memo1.Lines.Add(http.LastErrorText);
Exit;
end;
sbResponseBody := TChilkatStringBuilder.Create(Self);
resp.GetBodySb(sbResponseBody.ControlInterface);
jResp := TChilkatJsonObject.Create(Self);
jResp.LoadSb(sbResponseBody.ControlInterface);
jResp.EmitCompact := 0;
Memo1.Lines.Add('Response Body:');
Memo1.Lines.Add(jResp.Emit());
respStatusCode := resp.StatusCode;
Memo1.Lines.Add('Response Status Code = ' + IntToStr(respStatusCode));
if (respStatusCode >= 400) then
begin
Memo1.Lines.Add('Response Header:');
Memo1.Lines.Add(resp.Header);
Memo1.Lines.Add('Failed.');
Exit;
end;
end;