S3: Generate a Presigned Download URL
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.
Generates a SigV4 URL valid for 15 minutes, using HTTPS and the bucket's actual signing region. A presigned URL is a bearer credential: protect it from shared logs and untrusted recipients. It remains usable multiple times until expiration or revocation; it is not a one-time URL. Temporary credentials may expire sooner, and bucket policies can further restrict use. SigV4 expiration cannot exceed seven days. Generation is local and does not verify the bucket, object, permissions, or credentials with S3.
The signer needs s3:GetObject. The URL is saved to qa_output/s3-download-url.txt; create and restrict that directory before running. Sharing the URL grants its bearer temporary download access without making the object public.
Chilkat PHP ActiveX Downloads
<?php
$success = 0;
$stringCompare = new COM("Chilkat.StringBuilder");
// 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 = new COM("Chilkat.JsonObject");
$credentials->Strict = 1;
$success = $credentials->LoadFile('qa_data/aws/s3.json');
if ($success != 1) {
print 'Could not load private S3 credentials.' . "\n";
exit;
}
$stringCompare->SetString($credentials->stringOf('access_key'));
$stringMatch1 = $stringCompare->ContentsEqual('',1);
if (($credentials->JsonTypeOf('access_key') != 1) or ($stringMatch1 == 1)) {
print 'Missing or invalid access_key.' . "\n";
exit;
}
$stringCompare->SetString($credentials->stringOf('secret_key'));
$stringMatch2 = $stringCompare->ContentsEqual('',1);
if (($credentials->JsonTypeOf('secret_key') != 1) or ($stringMatch2 == 1)) {
print 'Missing or invalid secret_key.' . "\n";
exit;
}
$http = new COM("Chilkat.Http");
$http->AwsAccessKey = $credentials->stringOf('access_key');
$http->AwsSecretKey = $credentials->stringOf('secret_key');
if ($credentials->HasMember('session_token') == 1) {
$stringCompare->SetString($credentials->stringOf('session_token'));
$stringMatch3 = $stringCompare->ContentsEqual('',1);
if (($credentials->JsonTypeOf('session_token') != 1) or ($stringMatch3 == 1)) {
print 'session_token must be a nonempty string when supplied.' . "\n";
exit;
}
$http->AwsSessionToken = $credentials->stringOf('session_token');
}
// Current Chilkat uses SigV4 by default. Do not select deprecated SigV2.
// Set the actual bucket region, not the caller's location.
$region = 'us-west-2';
$http->AwsRegion = $region;
$endpoint = new COM("Chilkat.StringBuilder");
$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 = 'your-bucket-name';
// A leading slash is part of a key. Do not add one to imitate a file path.
$objectName = 'images/seahorse.jpg';
// Start with the original key. Encode once and retain '/' path separators.
// This also protects a literal "%2F" from being interpreted as a slash.
$keyPath = new COM("Chilkat.StringBuilder");
$keyPath->Append($http->urlEncode($objectName));
$slashCount = $keyPath->ReplaceNoCase('%2f','/');
$encodedKey = $keyPath->getAsString();
$signedUrl = $http->s3_GenPresignedUrl('GET',1,$bucketName,$encodedKey,900,'s3');
if ($http->LastMethodSuccess != 1) {
print 'Could not generate presigned download URL.' . "\n";
exit;
}
// Save the bearer URL in a private directory, not a shared application log.
$files = new COM("Chilkat.FileAccess");
$urlPath = 'qa_output/s3-download-url.txt';
if ($files->FileExists($urlPath) == 1) {
print 'Choose an unused private URL output path.' . "\n";
exit;
}
$urlText = new COM("Chilkat.StringBuilder");
$urlText->Append($signedUrl);
$success = $urlText->WriteFile($urlPath,'utf-8',0);
if ($success != 1) {
print 'Could not save the presigned URL.' . "\n";
exit;
}
print 'Presigned download URL saved to the private output file.' . "\n";
?>