Amazon Glacier: Upload Archive from a File
See more Amazon Glacier Examples
For existing Amazon Glacier vault customers. The standalone vault service is closed to new customers. These examples use its REST API version 2012-06-01, not the S3 bucket APIs used by S3 Glacier storage classes. See AWS service guidance.
Computes both SHA-256 hashes, uploads immutable archive bytes, checks HTTP 201 and the returned tree hash, and prints the archive ID for durable storage in your catalog. A description is optional, must be printable ASCII, and is limited to 1,024 characters; it is not a lookup key. Single-request uploads support up to 4 GiB; use multipart upload for larger archives. This variant hashes and streams a file without buffering the full upload; the file must remain unchanged.
Uses AWS Signature Version 4 with service glacier. Set the region once; the example builds the commercial AWS endpoint. Other partitions require the appropriate endpoint suffix. /-/ selects the signing account; change the vault name and IDs to your own resources. Load private qa_data/aws/glacier.json with string fields access_key, secret_key, and session_token for temporary credentials. Prefer short-lived role credentials. This sample does not discover profiles or refresh credentials. TLS certificate verification is enabled explicitly.
Checks transport completion, the operation-specific HTTP status, and parsed data separately. Inspect full diagnostics privately. Renew expired credentials; use bounded backoff with jitter for retryable reads. Uploads and job initiation can succeed before a connection fails, so do not blindly resubmit them. Configure IAM permissions for only the required operations and vault resources.
Chilkat Delphi DLL Downloads
var
success: Boolean;
credentials: HCkJsonObject;
region: PWideChar;
endpoint: HCkStringBuilder;
aws: HCkAuthAws;
rest: HCkRest;
connection: HCkSocket;
body: HCkJsonObject;
result: HCkJsonObject;
error: HCkJsonObject;
requestBody: HCkStringBuilder;
responseBody: HCkStringBuilder;
filePath: PWideChar;
crypt: HCkCrypt2;
files: HCkFileAccess;
fileSize: PWideChar;
treeHash: PWideChar;
linearHash: PWideChar;
fileStream: HCkStream;
response: PWideChar;
archiveId: PWideChar;
returnedHash: PWideChar;
begin
// 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 Glacier operation.
credentials := CkJsonObject_Create();
CkJsonObject_putStrict(credentials,True);
success := CkJsonObject_LoadFile(credentials,'qa_data/aws/glacier.json');
if (success <> True) then
begin
Memo1.Lines.Add('Could not load private AWS credentials.');
Exit;
end;
if ((CkJsonObject_JsonTypeOf(credentials,'access_key') <> 1) or (CkJsonObject__stringOf(credentials,'access_key') = '')) then
begin
Memo1.Lines.Add('Missing or invalid access_key.');
Exit;
end;
if ((CkJsonObject_JsonTypeOf(credentials,'secret_key') <> 1) or (CkJsonObject__stringOf(credentials,'secret_key') = '')) then
begin
Memo1.Lines.Add('Missing or invalid secret_key.');
Exit;
end;
// Set this once to the region containing the vault.
region := 'us-west-2';
endpoint := CkStringBuilder_Create();
CkStringBuilder_Append(endpoint,'glacier.');
CkStringBuilder_Append(endpoint,region);
CkStringBuilder_Append(endpoint,'.amazonaws.com');
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,'glacier');
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 IAM/STS credentials require their session token on every signed request.
// Include the token in the signed query. Keep request URLs and diagnostics private.
// Leave session_token absent or empty only for credentials that do not have one.
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;
if (CkJsonObject__stringOf(credentials,'session_token') <> '') 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 set the session-token parameter.');
Exit;
end;
end;
end;
// Supply a Socket to explicitly enable TLS certificate verification.
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;
success := CkRest_AddHeader(rest,'x-amz-glacier-version','2012-06-01');
if (success <> True) then
begin
Memo1.Lines.Add('Could not set x-amz-glacier-version.');
Exit;
end;
// A hyphen selects the account belonging to the signing credentials.
// Use the original, URL-safe vault name and IDs returned by Glacier.
success := CkRest_AddPathParam(rest,'{vault}','chilkat');
if (success <> True) then
begin
Memo1.Lines.Add('Could not set vault name.');
Exit;
end;
body := CkJsonObject_Create();
result := CkJsonObject_Create();
CkJsonObject_putStrict(result,True);
error := CkJsonObject_Create();
CkJsonObject_putStrict(error,True);
requestBody := CkStringBuilder_Create();
responseBody := CkStringBuilder_Create();
filePath := 'qa_data/zips/somethingBig.zip';
// Keep this file unchanged during BOTH hashing passes and the upload.
crypt := CkCrypt2_Create();
CkCrypt2_putHashAlgorithm(crypt,'sha256-tree-hash');
CkCrypt2_putEncodingMode(crypt,'hexlower');
files := CkFileAccess_Create();
fileSize := CkFileAccess__fileSizeStr(files,filePath);
if (CkFileAccess_getLastMethodSuccess(files) <> True) then
begin
Memo1.Lines.Add('Could not read archive file size.');
Exit;
end;
if (fileSize = '0') then
begin
CkCrypt2_putHashAlgorithm(crypt,'sha256');
end;
treeHash := CkCrypt2__hashFileENC(crypt,filePath);
if (CkCrypt2_getLastMethodSuccess(crypt) <> True) then
begin
Memo1.Lines.Add('Could not compute tree hash.');
Exit;
end;
success := CkRest_AddHeader(rest,'x-amz-sha256-tree-hash',treeHash);
if (success <> True) then
begin
Memo1.Lines.Add('Could not set x-amz-sha256-tree-hash.');
Exit;
end;
CkCrypt2_putHashAlgorithm(crypt,'sha256');
linearHash := CkCrypt2__hashFileENC(crypt,filePath);
if (CkCrypt2_getLastMethodSuccess(crypt) <> True) then
begin
Memo1.Lines.Add('Could not compute payload hash.');
Exit;
end;
CkAuthAws_putPrecomputedSha256(aws,linearHash);
// Set the precomputed hash before attaching the signing configuration again.
success := CkRest_SetAuthAws(rest,aws);
if (success <> True) then
begin
Memo1.Lines.Add('Could not update AWS signing.');
Exit;
end;
success := CkRest_AddHeader(rest,'x-amz-content-sha256',linearHash);
if (success <> True) then
begin
Memo1.Lines.Add('Could not set x-amz-content-sha256.');
Exit;
end;
success := CkRest_AddHeader(rest,'x-amz-archive-description','Example archive');
if (success <> True) then
begin
Memo1.Lines.Add('Could not set x-amz-archive-description.');
Exit;
end;
fileStream := CkStream_Create();
CkStream_putSourceFile(fileStream,filePath);
CkRest_putStreamNonChunked(rest,True);
CkRest_putIdleTimeoutMs(rest,120000);
CkSocket_putMaxReadIdleMs(connection,120000);
CkSocket_putMaxSendIdleMs(connection,120000);
response := CkRest__fullRequestStream(rest,'POST','/-/vaults/{vault}/archives',fileStream);
if (CkRest_getLastMethodSuccess(rest) <> True) then
begin
Memo1.Lines.Add('Upload did not complete; check for an accepted archive before retrying.');
Exit;
end;
CkStringBuilder_Append(responseBody,response);
if (CkRest_getResponseStatusCode(rest) <> 201) then
begin
Memo1.Lines.Add('Glacier HTTP status: ' + IntToStr(CkRest_getResponseStatusCode(rest)));
success := CkJsonObject_LoadSb(error,responseBody);
if (success = True) then
begin
if (CkJsonObject_JsonTypeOf(error,'code') = 1) then
begin
Memo1.Lines.Add('AWS error code: ' + CkJsonObject__stringOf(error,'code'));
end;
end;
Memo1.Lines.Add('AWS request ID: ' + CkRest__responseHdrByName(rest,'x-amzn-RequestId'));
// Inspect full errors privately. An HTTP error is not a successful operation.
Exit;
end;
archiveId := CkRest__responseHdrByName(rest,'x-amz-archive-id');
if ((CkRest_getLastMethodSuccess(rest) <> True) or (archiveId = '')) then
begin
Memo1.Lines.Add('Missing archive ID; investigate the accepted upload before retrying.');
Exit;
end;
// Save this ID even if subsequent checksum verification fails.
Memo1.Lines.Add('Archive ID: ' + archiveId);
Memo1.Lines.Add('Location: ' + CkRest__responseHdrByName(rest,'Location'));
returnedHash := CkRest__responseHdrByName(rest,'x-amz-sha256-tree-hash');
if (returnedHash <> treeHash) then
begin
Memo1.Lines.Add('Upload response checksum mismatch; preserve the archive ID and investigate.');
Exit;
end;
Memo1.Lines.Add('Archive upload verified. Persist its ID and checksum in your archive catalog.');
CkJsonObject_Dispose(credentials);
CkStringBuilder_Dispose(endpoint);
CkAuthAws_Dispose(aws);
CkRest_Dispose(rest);
CkSocket_Dispose(connection);
CkJsonObject_Dispose(body);
CkJsonObject_Dispose(result);
CkJsonObject_Dispose(error);
CkStringBuilder_Dispose(requestBody);
CkStringBuilder_Dispose(responseBody);
CkCrypt2_Dispose(crypt);
CkFileAccess_Dispose(files);
CkStream_Dispose(fileStream);