Skip to main content

Introducing AWS S3 Support in AWS SDK for Delphi

aws-s3

AWS Simple Storage Service (S3) is one of the most popular services provided by Amazon Web Services. It offers scalable, secure, and reliable cloud storage for all kinds of data, ranging from media files to backups. AWS S3 is designed to store data in "buckets," which are containers that hold objects (files) along with metadata. S3 provides essential features like versioning, encryption, and access control, making it an excellent choice for developers who need a versatile and easy-to-use storage solution.

Support for AWS S3 has been introduced in the AWS SDK for Delphi. This new addition brings the powerful capabilities of AWS S3 directly to Delphi developers, allowing you to interact with your cloud storage from your Delphi applications seamlessly. Whether you need to upload files, list objects, or manage buckets, it's all now possible with the familiar Delphi syntax you know and love.

Key Features of AWS S3 in AWS SDK for Delphi

  • Bucket Management: Easily create, delete, and list S3 buckets.
  • Object Operations: Upload, download, delete, and list objects in your S3 buckets.
  • Access Control: Manage permissions to control who can access your data.
  • Advanced Configurations: Leverage options like server-side encryption and metadata tagging for your objects.

Uploading a File to S3

This section demonstrates how to upload a file to AWS S3 using the AWS SDK for Delphi:

uses
AWS.S3;

procedure UploadFileToS3;
var
S3Client: IAmazonS3;
Request: IPutObjectRequest;
BucketName, Content, ObjectKey: string;
begin
S3Client := TAmazonS3Client.Create;
BucketName := 'my-bucket';
ObjectKey := 'TestObject';
Content := 'Sample Content';

Request := TPutObjectRequest.Create;
Request.BucketName := BucketName;
Request.ContentBody := Content;
Request.Key := ObjectKey;

try
S3Client.PutObject(Request);
Writeln('File uploaded successfully!');
except
on E: Exception do
Writeln('Error uploading file: ', E.Message);
end;
end;

Getting an Object from S3

This section explains how you can retrieve an object from an S3 bucket using the AWS SDK for Delphi:

procedure GetObjectFromS3;
var
S3Client: IAmazonS3;
GetObjectRequest: IGetObjectRequest;
GetObjectResponse: IGetObjectResponse;
BucketName, ObjectKey: string;
Reader: TStreamReader;
begin
S3Client := TAmazonS3Client.Create;
BucketName := 'my-bucket';
ObjectKey := 'test.txt';

GetObjectRequest := TGetObjectRequest.Create;
GetObjectRequest.BucketName := BucketName;
GetObjectRequest.Key := ObjectKey;

try
GetObjectResponse := S3Client.GetObject(GetObjectRequest);
Reader := TStreamReader.Create(GetObjectResponse.ResponseStream);
try
Writeln('Object Content: ', Reader.ReadToEnd);
finally
Reader.Free;
end;
except
on E: Exception do
Writeln('Error getting object: ', E.Message);
end;
end;

Generating a Pre-Signed URL

This section demonstrates how to generate a pre-signed URL for an object stored in an S3 bucket, allowing temporary access to the object:

procedure GeneratePreSignedUrl;
var
S3Client: IAmazonS3;
Request: IGetPreSignedUrlRequest;
BucketName, ObjectKey: string;
Expires: TDateTime;
Url: string;
begin
S3Client := TAmazonS3Client.Create;
BucketName := 'my-bucket';
ObjectKey := 'test.txt';
Expires := Now + 1; // URL expires in 1 day

Request := TGetPreSignedUrlRequest.Create;
Request.BucketName := BucketName;
Request.Key := ObjectKey;
Request.Expires := Expires;

try
Url := S3Client.GetPreSignedURL(Request);
Writeln('Generated Pre-Signed URL: ', Url);
except
on E: Exception do
Writeln('Error generating Pre-Signed URL: ', E.Message);
end;
end;

In this example, we create an instance of TS3Client and use it to upload a local file to an S3 bucket. The AWS credentials and region are automatically retrieved, similar to how it is done in the AWS SDK for .NET, from the .aws configuration file or environment variables.

Example: Deleting a Bucket with All Its Objects

Here’s an example of how to delete an S3 bucket along with all its objects, including different versions of objects. This can be useful when you need to completely remove a bucket and ensure all of its contents are deleted first.

class procedure TS3TestUtils.DeleteS3BucketWithObjects(S3Client: IAmazonS3; const BucketName: string;
const DeleteOptions: TS3DeleteBucketWithObjectsOptions);
begin
var listVersionsRequest: IListVersionsRequest := TListVersionsRequest.Create;
listVersionsRequest.BucketName := BucketName;
var listVersionsResponse: IListVersionsResponse;
repeat
// List all the versions of all the objects in the bucket.
listVersionsResponse := S3Client.ListVersions(listVersionsRequest);

if listVersionsResponse.Versions.Count = 0 then
Break;

var keyVersionList := TObjectList<TKeyVersion>.Create;
try
for var index := 0 to listVersionsResponse.Versions.Count - 1 do
begin
var keyVersion := TKeyVersion.Create;
keyVersionList.Add(keyVersion);
keyVersion.Key := listVersionsResponse.Versions[index].Key;
keyVersion.VersionId := listVersionsResponse.Versions[index].VersionId;
end;
try
// Delete the current set of objects.
var deleteObjectsRequest: IDeleteObjectsRequest := TDeleteObjectsRequest.Create;
deleteObjectsRequest.BucketName := BucketName;
deleteObjectsRequest.Objects := keyVersionList;
deleteObjectsRequest.KeepObjects := True;
deleteObjectsRequest.Quiet := DeleteOptions.QuietMode;
var deleteObjectsResponse := S3Client.DeleteObjects(deleteObjectsRequest);

except
on DeleteObjectsException: EDeleteObjectsException do
begin
if DeleteOptions.ContinueOnError then
begin
// Continue the delete operation if an error was encountered.
end
else
// Re-throw the exception if an error was encountered.
raise;
end;
end;
finally
keyVersionList.Free;
end;

// Set the markers to get next set of objects from the bucket.
listVersionsRequest.KeyMarker := listVersionsResponse.NextKeyMarker;
listVersionsRequest.VersionIdMarker := listVersionsResponse.NextVersionIdMarker;
until listVersionsResponse.IsTruncated;

var maxRetries := 10;
for var retries := 1 to maxRetries do
begin
try
// Bucket is empty, delete the bucket.
S3Client.DeleteBucket(BucketName);
Break;
except
on E: EAmazonS3Exception do
if (E.StatusCode <> 409) or (retries = maxRetries) then
raise
else
TDefaultRetryPolicy.DoWaitBeforeRetry(retries, 5000);
end;
end;
end;

This example shows how to list and delete all object versions in a bucket before deleting the bucket itself, ensuring that all resources are properly removed.

Conclusion

The addition of AWS S3 support to AWS SDK for Delphi opens up a range of possibilities for Delphi developers who want to leverage cloud storage in their applications. From managing buckets to working with objects, you can now build more versatile and cloud-connected solutions using Delphi.

We hope you're as excited as we are about this new feature. Feel free to explore the capabilities and start integrating AWS S3 into your Delphi applications today!

If you have any questions or need help getting started, let's discuss about this article in our forum!.