S3: Create a Bucket in a Region Using Http
See more S3 - HTTP Examples
Uses Chilkat.Http with Amazon S3 general purpose buckets and Signature Version 4. These examples do not cover directory buckets/S3 Express, access-point ARNs, Outposts, or other specialized endpoints. They target the commercial AWS partition; other partitions need their documented endpoint suffix.
Load private qa_data/aws/s3.json containing nonempty string fields access_key and secret_key, plus session_token for temporary credentials. Prefer short-lived role credentials. These samples do not discover AWS profiles or refresh credentials. Set the bucket's actual region and replace sample bucket names and object keys. TLS certificate verification is explicitly enabled. Keep detailed HTTP diagnostics private because they can contain credentials or presigned URLs.
Creates a general purpose bucket through Chilkat.Http. Uses a short-lived SigV4 PUT URL and HttpStr to send the CreateBucketConfiguration XML to the matching regional endpoint. The same region drives signing, endpoint, and LocationConstraint. For us-east-1 it sends an empty body instead. The simpler S3_CreateBucket method sends an empty body and cannot supply a regional configuration.
Choose an unused bucket name; requires s3:CreateBucket. Presigned URLs are bearer credentials and must stay private. No public ACL or ownership override is sent. New-bucket security defaults apply. An uncertain response may mean the bucket was created; inspect its state before retrying. In us-east-1, recreating an owned bucket can reset its ACLs.
Chilkat Delphi ActiveX Downloads
var
success: Integer;
stringCompare: TChilkatStringBuilder;
credentials: TChilkatJsonObject;
stringMatch1: Integer;
stringMatch2: Integer;
http: TChilkatHttp;
stringMatch3: Integer;
region: WideString;
endpoint: TChilkatStringBuilder;
bucketName: WideString;
config: TChilkatXml;
body: WideString;
stringMatch4: Integer;
signedUrl: WideString;
response: TChilkatHttpResponse;
begin
success := 0;
stringCompare := TChilkatStringBuilder.Create(Self);
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Load private credentials; never put access keys or session tokens in source.
credentials := TChilkatJsonObject.Create(Self);
credentials.Strict := 1;
success := credentials.LoadFile('qa_data/aws/s3.json');
if (success <> 1) then
begin
Memo1.Lines.Add('Could not load private S3 credentials.');
Exit;
end;
stringCompare.SetString(credentials.StringOf('access_key'));
stringMatch1 := stringCompare.ContentsEqual('',1);
if ((credentials.JsonTypeOf('access_key') <> 1) or (stringMatch1 = 1)) then
begin
Memo1.Lines.Add('Missing or invalid access_key.');
Exit;
end;
stringCompare.SetString(credentials.StringOf('secret_key'));
stringMatch2 := stringCompare.ContentsEqual('',1);
if ((credentials.JsonTypeOf('secret_key') <> 1) or (stringMatch2 = 1)) then
begin
Memo1.Lines.Add('Missing or invalid secret_key.');
Exit;
end;
http := TChilkatHttp.Create(Self);
http.AwsAccessKey := credentials.StringOf('access_key');
http.AwsSecretKey := credentials.StringOf('secret_key');
if (credentials.HasMember('session_token') = 1) then
begin
stringCompare.SetString(credentials.StringOf('session_token'));
stringMatch3 := stringCompare.ContentsEqual('',1);
if ((credentials.JsonTypeOf('session_token') <> 1) or (stringMatch3 = 1)) then
begin
Memo1.Lines.Add('session_token must be a nonempty string when supplied.');
Exit;
end;
http.AwsSessionToken := credentials.StringOf('session_token');
end;
// Current Chilkat uses SigV4 by default. Do not select deprecated SigV2.
// Set the actual bucket region, not the caller's location.
region := 'eu-west-2';
http.AwsRegion := region;
endpoint := TChilkatStringBuilder.Create(Self);
endpoint.Append('s3.');
endpoint.Append(region);
endpoint.Append('.amazonaws.com');
http.AwsEndpoint := endpoint.GetAsString();
http.RequireSslCertVerify := 1;
http.FollowRedirects := 0;
// A region redirect is not success. Correct the endpoint and signing region.
bucketName := 'replace-with-your-unique-bucket-name';
config := TChilkatXml.Create(Self);
config.Tag := 'CreateBucketConfiguration';
config.AddAttribute('xmlns','http://s3.amazonaws.com/doc/2006-03-01/');
config.UpdateChildContent('LocationConstraint',region);
body := '';
stringCompare.SetString(region);
stringMatch4 := stringCompare.ContentsEqual('us-east-1',1);
if (stringMatch4 <> 1) then
begin
body := config.GetXml();
end;
// A short-lived signed PUT URL lets Http send the required XML body.
// The signing region and regional endpoint are the SAME as LocationConstraint.
signedUrl := http.S3_GenPresignedUrl('PUT',1,bucketName,'',300,'s3');
if (http.LastMethodSuccess <> 1) then
begin
Memo1.Lines.Add('Could not sign bucket creation request.');
Exit;
end;
response := TChilkatHttpResponse.Create(Self);
success := http.HttpStr('PUT',signedUrl,body,'utf-8','application/xml',response.ControlInterface);
if (success <> 1) then
begin
Memo1.Lines.Add('Request failed (transport or S3 error). HTTP status: ' + IntToStr(http.LastStatus));
// Inspect LastErrorText privately. Do not log credentials or signed URLs.
Exit;
end;
if (response.StatusCode <> 200) then
begin
Memo1.Lines.Add('Unexpected HTTP status: ' + IntToStr(response.StatusCode));
Exit;
end;
Memo1.Lines.Add('Regional bucket creation request succeeded.');