Delphi DLL
Delphi DLL
S3 Rename File
See more Amazon S3 (new) Examples
Demonstrates how to rename a file on Amazon S3.Renaming an object (file) in AWS S3 using the AWS S3 API involves copying the object to a new location with the desired name and then deleting the original object.
Chilkat Delphi DLL Downloads
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, AuthAws, Rest, StringBuilder;
...
procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
rest: HCkRest;
bTls: Boolean;
port: Integer;
bAutoReconnect: Boolean;
authAws: HCkAuthAws;
sbResponse: HCkStringBuilder;
statusCode: Integer;
begin
success := False;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
rest := CkRest_Create();
// Connect to the Amazon AWS REST server in the desired region.
bTls := True;
port := 443;
bAutoReconnect := True;
success := CkRest_Connect(rest,'s3-us-west-2.amazonaws.com',port,bTls,bAutoReconnect);
// Provide AWS credentials.
authAws := CkAuthAws_Create();
CkAuthAws_putAccessKey(authAws,'AWS_ACCESS_KEY');
CkAuthAws_putSecretKey(authAws,'AWS_SECRET_KEY');
CkAuthAws_putServiceName(authAws,'s3');
// This particular bucket is in the Oregon region, as opposed to the US Standard,
// therefore the Region must be set appropriately.
// Also note that we connected to "s3-us-west-2.amazonaws.com".
CkAuthAws_putRegion(authAws,'us-west-2');
CkRest_SetAuthAws(rest,authAws);
// Set the bucket name via the HOST header.
// In this case, the bucket name is "chilkat.qa".
// Note that the Host header should use "bucketName.s3.amazonaws.com", not "bucketName.s3-us-west-2.amazonaws.com"
CkRest_putHost(rest,'chilkat.qa.s3.amazonaws.com');
// Copy /images/sea_creatures/starfish.jpg to /images/sea_creatures/starfish3.jpg
// Notice here that the x-amz-copy-source includes the bucket in the source path,
// but the target path passed to FullRequestNoBody does NOT contain the bucket (because the bucket
// was already specified in the Host header).
CkRest_AddHeader(rest,'x-amz-copy-source','/chilkat.qa/images/sea_creatures/starfish.jpg');
sbResponse := CkStringBuilder_Create();
success := CkRest_FullRequestNoBodySb(rest,'PUT','/images/sea_creatures/starfish3.jpg',sbResponse);
if (success = False) then
begin
Memo1.Lines.Add(CkRest__lastErrorText(rest));
Exit;
end;
// When successful, the S3 Storage service will respond with a 200 or 204 response code,
// with an XML body.
if (CkRest_getResponseStatusCode(rest) <> 200) then
begin
// Examine the request/response to see what happened.
Memo1.Lines.Add('response status code = ' + IntToStr(CkRest_getResponseStatusCode(rest)));
Memo1.Lines.Add('response status text = ' + CkRest__responseStatusText(rest));
Memo1.Lines.Add('response header: ' + CkRest__responseHeader(rest));
Memo1.Lines.Add('response body: ' + CkStringBuilder__getAsString(sbResponse));
Memo1.Lines.Add('---');
Memo1.Lines.Add('LastRequestStartLine: ' + CkRest__lastRequestStartLine(rest));
Memo1.Lines.Add('LastRequestHeader: ' + CkRest__lastRequestHeader(rest));
end;
Memo1.Lines.Add(CkStringBuilder__getAsString(sbResponse));
Memo1.Lines.Add('Copy Successful.');
// --------------------------------------
// Now we can delete the original file..
// --------------------------------------
// We no longer want to send the x-amz-copy-source header. Remove it.
CkRest_RemoveHeader(rest,'x-amz-copy-source');
success := CkRest_FullRequestNoBodySb(rest,'DELETE','/images/sea_creatures/starfish.jpg',sbResponse);
if (success = False) then
begin
Memo1.Lines.Add(CkRest__lastErrorText(rest));
Exit;
end;
// If successfully deleted, the response status code is 204 and the response body text will be empty.
statusCode := CkRest_getResponseStatusCode(rest);
Memo1.Lines.Add('Response status code = ' + IntToStr(statusCode));
Memo1.Lines.Add('Response text = ');
Memo1.Lines.Add(CkStringBuilder__getAsString(sbResponse));
CkRest_Dispose(rest);
CkAuthAws_Dispose(authAws);
CkStringBuilder_Dispose(sbResponse);
end;