AWS Developer Blog

Amazon S3 Transfer Utility for Windows Store and Windows Phone

by Milind Gokarn | on | in .NET* | Permalink | Comments |  Share

We recently made the Amazon S3 Transfer Utility API in AWS SDK for .NET available for the Windows Store and Windows Phone platforms. TransferUtility is an API that runs on top of the low-level Amazon S3 API and provides utility methods for uploading and downloading files and directories. It includes support for automatic switching to multipart upload for large files, multi-threaded uploads, cancellation of in-progress operations and notifications for transfer progress. The set of TransferUtility API available for Windows Store and Windows Phone platforms includes all the methods available for .NET 3.5 and .NET 4.5 platforms except for the upload/download directory functionality. Another point to note is that these platforms support only the asynchronous APIs.

The code snippets in the following sections show how to upload and download a file using TransferUtility. Notice that we use the IStorageFile type available on Windows Store and Windows Phone platforms. You can use File Pickers available on these platforms to get an instance of IStorageFile. This article provides information on working with File Pickers for the Windows Store platform.

Upload using Transfer Utility

The following code snippet shows the TransferUtility.UploadAsync method being used to upload a file. We use an instance of the TransferUtilityConfig class to change the default values for ConcurrentServiceRequests and MinSizeBeforePartUpload. We have changed the part size to 10 MB for multipart upload using the TransferUtilityUploadRequest.PartSize property. You can also see that we subscribe to TransferUtilityUploadRequest.UploadProgressEvent to receive upload progress notification events.

private const int MB_SIZE = (int)Math.Pow(2, 20);

public async Task UploadFile(IStorageFile storageFile, string bucket, string key, AWSCredentials credentials, CancellationToken cancellationToken)
{
    var s3Client = new AmazonS3Client(credentials,RegionEndpoint.USWest2);
    var transferUtilityConfig = new TransferUtilityConfig
    {
        // Use 5 concurrent requests.
        ConcurrentServiceRequests = 5,

        // Use multipart upload for file size greater 20 MB.
        MinSizeBeforePartUpload = 20 * MB_SIZE,
    };
    using (var transferUtility = new TransferUtility(s3Client, transferUtilityConfig))
    {
        var uploadRequest = new TransferUtilityUploadRequest
        {
            BucketName = bucket,
            Key = key,
            StorageFile = storageFile,

            // Set size of each part for multipart upload to 10 MB
            PartSize = 10 * MB_SIZE
        };
        uploadRequest.UploadProgressEvent += OnUploadProgressEvent;
        await transferUtility.UploadAsync(uploadRequest, cancellationToken);
    }
}

void OnUploadProgressEvent(object sender, UploadProgressArgs e)
{
    // Process progress update events.
}

Download using Transfer Utility

Following is a snippet to download an object from S3 using the TransferUtility.DownloadAsync method. We use the TransferUtilityDownloadRequest.WriteObjectProgressEvent event to suscribe to notifications about the download progress.

public async Task DownloadFile(IStorageFile storageFile, string bucket, string key, AWSCredentials credentials, CancellationToken cancellationToken)
{
    var s3Client = new AmazonS3Client(credentials, RegionEndpoint.USWest2);
    using (var transferUtility = new TransferUtility(s3Client))
    {
        var downloadRequest = new TransferUtilityDownloadRequest
        {
            BucketName = bucket,
            Key = key,
            StorageFile = storageFile
        };
        downloadRequest.WriteObjectProgressEvent += OnWriteObjectProgressEvent;
        await transferUtility.DownloadAsync(downloadRequest, cancellationToken);
    }
}

void OnWriteObjectProgressEvent(object sender, WriteObjectProgressArgs e)
{
    // Process progress update events.
}

In this post, we saw how to use the Amazon S3 Transfer Utility API to upload and download files on the Windows Store and Windows Phone 8 platforms. Try it out, and let us know what you think.