Amazon S3: List Unfinished Multipart Uploads
See more S3 - REST Examples
List incomplete uploads under a prefix to identify abandoned uploads and associated storage charges. Requires s3:ListBucketMultipartUploads. This example does not abort anything. For a deliberately selected upload, save its bucket, region, key, and upload_id as described by the existing abort example; stop active part writers before cleanup. A lifecycle rule can provide ongoing cleanup.
Follow both pagination markers, including after an empty page. With encoding-type=url, decode keys and NextKeyMarker once, but never decode the opaque version/upload ID. Repeated marker pairs and the 1000-page bound report incomplete enumeration. Pages are not an atomic snapshot, and no deprecated DisplayName field is required.
For general purpose S3 buckets, using regional HTTPS endpoints and AWS Signature Version 4. Directory buckets (S3 Express), access points, Outposts, and other AWS partitions need their own endpoint/authentication rules. Use a bucket name without dots for these virtual-hosted TLS examples; see the path-style example for dotted names.
Provide current IAM credentials in a private qa_data/aws/s3.json file with string fields access_key, secret_key, and session_token for temporary credentials. Prefer short-lived role credentials. This file is explicitly loaded; these examples do not discover profiles or refresh credentials. Keep it outside public content and source control. The session token is included in the signed request. TLS certificate verification is enabled on the Socket used by Rest.
Transport success and HTTP success are different. Keep full diagnostics private; signed requests and presigned URLs can contain credentials or grant access. For retryable reads use bounded backoff with jitter. Before retrying a write with an uncertain outcome, check the resource state. Keep keys, upload IDs, version IDs, ETags, and large sizes as strings.
AWS ListMultipartUploads reference
Chilkat Delphi DLL Downloads
var
success: Boolean;
textCompare: HCkStringBuilder;
credentials: HCkJsonObject;
sameText1: Boolean;
sameText2: Boolean;
region: PWideChar;
bucketName: PWideChar;
endpoint: HCkStringBuilder;
aws: HCkAuthAws;
rest: HCkRest;
sameText3: Boolean;
connection: HCkSocket;
xml: HCkXml;
seenPages: HCkHashtable;
markerPair: HCkJsonObject;
more: Boolean;
page: Integer;
responseBody: PWideChar;
sameText4: Boolean;
sameText5: Boolean;
u: Integer;
sameText6: Boolean;
keyu: HCkStringBuilder;
keyuPluses: Integer;
sameText7: Boolean;
sameText8: Boolean;
sameText9: Boolean;
sameText10: Boolean;
sameText11: Boolean;
sameText12: Boolean;
sameText13: Boolean;
sameText14: Boolean;
nextKey: HCkStringBuilder;
nextKeyPluses: Integer;
pair: PWideChar;
begin
success := False;
// StringBuilder compares text by value in every generated language.
textCompare := CkStringBuilder_Create();
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success := False;
// Private configuration, not a file in public web content or source control.
// Load current IAM credentials authorized for the S3 operation.
credentials := CkJsonObject_Create();
CkJsonObject_putStrict(credentials,True);
success := CkJsonObject_LoadFile(credentials,'qa_data/aws/s3.json');
if (success <> True) then
begin
Memo1.Lines.Add('Could not load private AWS credentials.');
Exit;
end;
CkStringBuilder_Clear(textCompare);
CkStringBuilder_SetString(textCompare,CkJsonObject__stringOf(credentials,'access_key'));
sameText1 := CkStringBuilder_ContentsEqual(textCompare,'',True);
if ((CkJsonObject_JsonTypeOf(credentials,'access_key') <> 1) or (sameText1 = True)) then
begin
Memo1.Lines.Add('Missing or invalid access_key.');
Exit;
end;
CkStringBuilder_Clear(textCompare);
CkStringBuilder_SetString(textCompare,CkJsonObject__stringOf(credentials,'secret_key'));
sameText2 := CkStringBuilder_ContentsEqual(textCompare,'',True);
if ((CkJsonObject_JsonTypeOf(credentials,'secret_key') <> 1) or (sameText2 = True)) then
begin
Memo1.Lines.Add('Missing or invalid secret_key.');
Exit;
end;
// General purpose bucket in the commercial AWS partition.
region := 'us-west-2';
bucketName := 'amzn-s3-demo-bucket';
endpoint := CkStringBuilder_Create();
success := CkStringBuilder_Append(endpoint,bucketName);
if (success <> True) then
begin
Memo1.Lines.Add('Could not build text.');
Exit;
end;
success := CkStringBuilder_Append(endpoint,'.');
if (success <> True) then
begin
Memo1.Lines.Add('Could not build text.');
Exit;
end;
success := CkStringBuilder_Append(endpoint,'s3.');
if (success <> True) then
begin
Memo1.Lines.Add('Could not build text.');
Exit;
end;
success := CkStringBuilder_Append(endpoint,region);
if (success <> True) then
begin
Memo1.Lines.Add('Could not build text.');
Exit;
end;
success := CkStringBuilder_Append(endpoint,'.amazonaws.com');
if (success <> True) then
begin
Memo1.Lines.Add('Could not build text.');
Exit;
end;
aws := CkAuthAws_Create();
CkAuthAws_putAccessKey(aws,CkJsonObject__stringOf(credentials,'access_key'));
CkAuthAws_putSecretKey(aws,CkJsonObject__stringOf(credentials,'secret_key'));
CkAuthAws_putRegion(aws,region);
CkAuthAws_putServiceName(aws,'s3');
CkAuthAws_putSignatureVersion(aws,4);
rest := CkRest_Create();
CkRest_putConnectTimeoutMs(rest,20000);
CkRest_putIdleTimeoutMs(rest,60000);
success := CkRest_SetAuthAws(rest,aws);
if (success <> True) then
begin
Memo1.Lines.Add('Could not configure AWS signing.');
Exit;
end;
// Temporary credentials require this token in every signed query.
// Keep request URLs private. Do not clear this parameter between requests.
if (CkJsonObject_HasMember(credentials,'session_token') = True) then
begin
if (CkJsonObject_JsonTypeOf(credentials,'session_token') <> 1) then
begin
Memo1.Lines.Add('session_token must be a string.');
Exit;
end;
CkStringBuilder_Clear(textCompare);
CkStringBuilder_SetString(textCompare,CkJsonObject__stringOf(credentials,'session_token'));
sameText3 := CkStringBuilder_ContentsEqual(textCompare,'',True);
if (sameText3 <> True) then
begin
success := CkRest_AddQueryParam(rest,'X-Amz-Security-Token',CkJsonObject__stringOf(credentials,'session_token'));
if (success <> True) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
end;
end;
// Connect to the SAME hostname used for TLS verification and request signing.
connection := CkSocket_Create();
CkSocket_putRequireSslCertVerify(connection,True);
CkSocket_putMaxReadIdleMs(connection,60000);
CkSocket_putMaxSendIdleMs(connection,60000);
success := CkSocket_Connect(connection,CkStringBuilder__getAsString(endpoint),443,True,20000);
if (success <> True) then
begin
Memo1.Lines.Add('TLS connection failed; inspect diagnostics privately.');
Exit;
end;
success := CkRest_UseConnection(rest,connection,True);
if (success <> True) then
begin
Memo1.Lines.Add('Could not attach the TLS connection.');
Exit;
end;
xml := CkXml_Create();
CkXml_putStrict(xml,True);
success := CkRest_AddQueryParam(rest,'uploads','');
if (success <> True) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
success := CkRest_AddQueryParam(rest,'max-uploads','1000');
if (success <> True) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
success := CkRest_AddQueryParam(rest,'prefix','documents/');
if (success <> True) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
success := CkRest_AddQueryParam(rest,'encoding-type','url');
if (success <> True) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
seenPages := CkHashtable_Create();
markerPair := CkJsonObject_Create();
more := True;
page := 0;
while (more = True) do
begin
if (page = 1000) then
begin
Memo1.Lines.Add('Page limit reached; enumeration is incomplete.');
Exit;
end;
responseBody := CkRest__fullRequestNoBody(rest,'GET','/');
if (CkRest_getLastMethodSuccess(rest) <> True) then
begin
Memo1.Lines.Add('Request did not complete; inspect diagnostics privately.');
Exit;
end;
if (CkRest_getResponseStatusCode(rest) <> 200) then
begin
Memo1.Lines.Add('S3 HTTP status: ' + IntToStr(CkRest_getResponseStatusCode(rest)));
success := CkXml_LoadXml(xml,responseBody);
CkStringBuilder_Clear(textCompare);
CkStringBuilder_SetString(textCompare,CkXml__tag(xml));
sameText4 := CkStringBuilder_ContentsEqual(textCompare,'Error',True);
if ((success = True) and (sameText4 = True)) then
begin
Memo1.Lines.Add('S3 error code: ' + CkXml__getChildContent(xml,'Code'));
end;
// Keep signed headers, credentials, URLs, and full diagnostics private.
// A timeout may leave a write's outcome uncertain. Check before retrying.
Exit;
end;
success := CkXml_LoadXml(xml,responseBody);
if (success <> True) then
begin
Memo1.Lines.Add('Expected well-formed XML.');
Exit;
end;
CkStringBuilder_Clear(textCompare);
CkStringBuilder_SetString(textCompare,CkXml__tag(xml));
sameText5 := CkStringBuilder_ContentsEqual(textCompare,'ListMultipartUploadsResult',True);
if (sameText5 <> True) then
begin
Memo1.Lines.Add('Unexpected S3 response element; this is not a successful result.');
Exit;
end;
u := 0;
while (u < CkXml_NumChildrenHavingTag(xml,'Upload')) do
begin
CkXml_putI(xml,u);
CkStringBuilder_Clear(textCompare);
CkStringBuilder_SetString(textCompare,CkXml__getChildContent(xml,'Upload[i]|Key'));
sameText6 := CkStringBuilder_ContentsEqual(textCompare,'',True);
if ((CkXml_HasChildWithTag(xml,'Upload[i]|Key') <> True) or (sameText6 = True)) then
begin
Memo1.Lines.Add('Missing response field: Upload[i]|Key');
Exit;
end;
keyu := CkStringBuilder_Create();
success := CkStringBuilder_Append(keyu,CkXml__getChildContent(xml,'Upload[i]|Key'));
if (success <> True) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
keyuPluses := CkStringBuilder_Replace(keyu,'+','%2B');
success := CkStringBuilder_Decode(keyu,'url','utf-8');
if (success <> True) then
begin
Memo1.Lines.Add('Could not decode a returned key.');
Exit;
end;
Memo1.Lines.Add('Upload key: ' + CkStringBuilder__getAsString(keyu));
CkStringBuilder_Clear(textCompare);
CkStringBuilder_SetString(textCompare,CkXml__getChildContent(xml,'Upload[i]|UploadId'));
sameText7 := CkStringBuilder_ContentsEqual(textCompare,'',True);
if ((CkXml_HasChildWithTag(xml,'Upload[i]|UploadId') <> True) or (sameText7 = True)) then
begin
Memo1.Lines.Add('Missing response field: Upload[i]|UploadId');
Exit;
end;
Memo1.Lines.Add('UploadId: ' + CkXml__getChildContent(xml,'Upload[i]|UploadId'));
CkStringBuilder_Clear(textCompare);
CkStringBuilder_SetString(textCompare,CkXml__getChildContent(xml,'Upload[i]|Initiated'));
sameText8 := CkStringBuilder_ContentsEqual(textCompare,'',True);
if ((CkXml_HasChildWithTag(xml,'Upload[i]|Initiated') <> True) or (sameText8 = True)) then
begin
Memo1.Lines.Add('Missing response field: Upload[i]|Initiated');
Exit;
end;
Memo1.Lines.Add('Initiated: ' + CkXml__getChildContent(xml,'Upload[i]|Initiated'));
u := u + 1;
end;
CkStringBuilder_Clear(textCompare);
CkStringBuilder_SetString(textCompare,CkXml__getChildContent(xml,'IsTruncated'));
sameText9 := CkStringBuilder_ContentsEqual(textCompare,'',True);
if ((CkXml_HasChildWithTag(xml,'IsTruncated') <> True) or (sameText9 = True)) then
begin
Memo1.Lines.Add('Missing response field: IsTruncated');
Exit;
end;
CkStringBuilder_Clear(textCompare);
CkStringBuilder_SetString(textCompare,CkXml__getChildContent(xml,'IsTruncated'));
sameText10 := CkStringBuilder_ContentsEqual(textCompare,'true',True);
CkStringBuilder_Clear(textCompare);
CkStringBuilder_SetString(textCompare,CkXml__getChildContent(xml,'IsTruncated'));
sameText11 := CkStringBuilder_ContentsEqual(textCompare,'false',True);
if ((sameText10 <> True) and (sameText11 <> True)) then
begin
Memo1.Lines.Add('Invalid IsTruncated value.');
Exit;
end;
more := False;
CkStringBuilder_Clear(textCompare);
CkStringBuilder_SetString(textCompare,CkXml__getChildContent(xml,'IsTruncated'));
sameText12 := CkStringBuilder_ContentsEqual(textCompare,'true',True);
if (sameText12 = True) then
begin
CkStringBuilder_Clear(textCompare);
CkStringBuilder_SetString(textCompare,CkXml__getChildContent(xml,'NextKeyMarker'));
sameText13 := CkStringBuilder_ContentsEqual(textCompare,'',True);
if ((CkXml_HasChildWithTag(xml,'NextKeyMarker') <> True) or (sameText13 = True)) then
begin
Memo1.Lines.Add('Missing response field: NextKeyMarker');
Exit;
end;
CkStringBuilder_Clear(textCompare);
CkStringBuilder_SetString(textCompare,CkXml__getChildContent(xml,'NextUploadIdMarker'));
sameText14 := CkStringBuilder_ContentsEqual(textCompare,'',True);
if ((CkXml_HasChildWithTag(xml,'NextUploadIdMarker') <> True) or (sameText14 = True)) then
begin
Memo1.Lines.Add('Missing response field: NextUploadIdMarker');
Exit;
end;
nextKey := CkStringBuilder_Create();
success := CkStringBuilder_Append(nextKey,CkXml__getChildContent(xml,'NextKeyMarker'));
if (success <> True) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
nextKeyPluses := CkStringBuilder_Replace(nextKey,'+','%2B');
success := CkStringBuilder_Decode(nextKey,'url','utf-8');
if (success <> True) then
begin
Memo1.Lines.Add('Could not decode a returned key.');
Exit;
end;
success := CkJsonObject_UpdateString(markerPair,'key',CkStringBuilder__getAsString(nextKey));
if (success <> True) then
begin
Memo1.Lines.Add('Could not build saved state.');
Exit;
end;
success := CkJsonObject_UpdateString(markerPair,'id',CkXml__getChildContent(xml,'NextUploadIdMarker'));
if (success <> True) then
begin
Memo1.Lines.Add('Could not build saved state.');
Exit;
end;
pair := CkJsonObject__emit(markerPair);
if (CkHashtable_Contains(seenPages,pair) = True) then
begin
Memo1.Lines.Add('Repeated marker pair; enumeration is incomplete.');
Exit;
end;
success := CkHashtable_AddInt(seenPages,pair,1);
if (success <> True) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
if (page > 0) then
begin
success := CkRest_RemoveQueryParam(rest,'key-marker');
if (success <> True) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
success := CkRest_RemoveQueryParam(rest,'upload-id-marker');
if (success <> True) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
end;
// AddQueryParam encodes the decoded key and the opaque, unmodified ID once.
success := CkRest_AddQueryParam(rest,'key-marker',CkStringBuilder__getAsString(nextKey));
if (success <> True) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
success := CkRest_AddQueryParam(rest,'upload-id-marker',CkXml__getChildContent(xml,'NextUploadIdMarker'));
if (success <> True) then
begin
Memo1.Lines.Add('Could not prepare the request.');
Exit;
end;
more := True;
end;
page := page + 1;
end;
Memo1.Lines.Add('Enumeration complete.');
CkStringBuilder_Dispose(textCompare);
CkJsonObject_Dispose(credentials);
CkStringBuilder_Dispose(endpoint);
CkAuthAws_Dispose(aws);
CkRest_Dispose(rest);
CkSocket_Dispose(connection);
CkXml_Dispose(xml);
CkHashtable_Dispose(seenPages);
CkJsonObject_Dispose(markerPair);
CkStringBuilder_Dispose(keyu);
CkStringBuilder_Dispose(nextKey);