1. Home
  2. Data
  3. File Storage Services
  4. Storage – AWS Implementation

Storage – AWS Implementation

Configuration

AWS Implementation supports the Amazon S3 BLOB and Amazon S3 BOLB PRIVATE storage types. To setup a connection to this service you need a Client Access Key and a Client Secret Key from Amazon. You will also need to specify a Client Region to determine what servers to connect to.

Implementation

Save

public static void SaveBLOB(StorageInformation storageInformation)
{

    try
    {
        XElement storageXML = XElement.Parse(storageInformation.StorageDefinition);
        string clientAccessKey = storageXML.GetElement("clientAccessKey", "");
        string clientSecretKey = storageXML.GetElement("clientSecretKey", "");
        RegionEndpoint clientRegion = null;
        string URL = $"https://{storageInformation.WorkspaceGUID}.s3.amazonaws.com/";
        switch (storageXML.GetElement("clientRegion", ""))
        {
            case "AFSouth1":
                clientRegion = RegionEndpoint.AFSouth1;
                break;
            case "APEast1":
                clientRegion = RegionEndpoint.APEast1;
                break;
            case "APNortheast1":
                clientRegion = RegionEndpoint.APNortheast1;
                break;
            case "APNortheast2":
                clientRegion = RegionEndpoint.APNortheast2;
                break;
            case "APNortheast3":
                clientRegion = RegionEndpoint.APNortheast3;
                break;
            case "APSouth1":
                clientRegion = RegionEndpoint.APSouth1;
                break;
            case "APSoutheast1":
                clientRegion = RegionEndpoint.APSoutheast1;
                break;
            case "APSoutheast2":
                clientRegion = RegionEndpoint.APSoutheast2;
                break;
            case "CACentral1":
                clientRegion = RegionEndpoint.CACentral1;
                break;
            case "CNNorth1":
                clientRegion = RegionEndpoint.CNNorth1;
                break;
            case "CNNorthWest1":
                clientRegion = RegionEndpoint.CNNorthWest1;
                break;
            case "EUCentral1":
                clientRegion = RegionEndpoint.EUCentral1;
                break;
            case "EUNorth1":
                clientRegion = RegionEndpoint.EUNorth1;
                break;
            case "EUSouth1":
                clientRegion = RegionEndpoint.EUSouth1;
                break;
            case "EUWest1":
                clientRegion = RegionEndpoint.EUWest1;
                break;
            case "EUWest2":
                clientRegion = RegionEndpoint.EUWest2;
                break;
            case "EUWest3":
                clientRegion = RegionEndpoint.EUWest3;
                break;
            case "MESouth1":
                clientRegion = RegionEndpoint.MESouth1;
                break;
            case "SAEast1":
                clientRegion = RegionEndpoint.SAEast1;
                break;
            case "USEast1":
                clientRegion = RegionEndpoint.USEast1;
                break;
            case "USEast2":
                clientRegion = RegionEndpoint.USEast2;
                break;
            case "USGovCloudEast1":
                clientRegion = RegionEndpoint.USGovCloudEast1;
                break;
            case "USGovCloudWest1":
                clientRegion = RegionEndpoint.USGovCloudWest1;
                break;
            case "USWest1":
                clientRegion = RegionEndpoint.USWest1;
                break;
            case "USWest2":
                clientRegion = RegionEndpoint.USWest2;
                break;
        }

        // Create a connection to the AWS S3 storage account...
        AmazonS3Client s3Client = new(clientAccessKey, clientSecretKey, clientRegion);

        // Throw error if container does not exist...
        AmazonS3Util.DoesS3BucketExistV2Async(s3Client, storageInformation.WorkspaceGUID.ToString()).Wait();
        if (AmazonS3Util.DoesS3BucketExistV2Async(s3Client, storageInformation.WorkspaceGUID.ToString()).Result)
        {
            // Upload the file to AWS S3 storage container...
            storageInformation.MemoryStream.Position = 0;
            TransferUtility fileTransferUtility = new(s3Client);
            string fileType = storageInformation.IsFileCompressed ? "zip" : $"{storageInformation.FileType.ToLower()}";

            // Will overwrite file if already exists...
            if (storageInformation.IsFilePublic)
            {
                // Give the workspace container public access permissions so people can access the stored blobs...
                TransferUtilityUploadRequest fileTransferUtilityRequest = new()
                {
                    BucketName = storageInformation.WorkspaceGUID.ToString(),
                    Key = $"{storageInformation.FileName.ToLower()}.{fileType}",
                    InputStream = storageInformation.MemoryStream,
                    CannedACL = S3CannedACL.PublicRead
                };
                fileTransferUtility.UploadAsync(fileTransferUtilityRequest).Wait();
                storageInformation.FileURL = $"{URL}{storageInformation.FileName.ToLower()}.{fileType}";
            }
            else
            {
                fileTransferUtility.UploadAsync(storageInformation.MemoryStream, storageInformation.WorkspaceGUID.ToString(), $"{storageInformation.FileName.ToLower()}.{fileType}").Wait();
                storageInformation.FileURL = $"PRIVATE:{URL}{storageInformation.FileName.ToLower()}.{fileType}";
            }
        }
        else
        {
            storageInformation.ErrorMessage = $"Container {storageInformation.WorkspaceGUID} does not exist";
        }
    }
    catch (Exception)
    {
        storageInformation.ErrorMessage = "AWSS3Storage.SaveBlob - Unknown error";
    }
}

Read

public static void ReadBLOB(StorageInformation storageInformation)
{
    try
    {
        XElement storageXML = XElement.Parse(storageInformation.StorageDefinition);
        string clientAccessKey = storageXML.GetElement("clientAccessKey", "");
        string clientSecretKey = storageXML.GetElement("clientSecretKey", "");
        RegionEndpoint clientRegion = null;
        switch (storageXML.GetElement("clientRegion", ""))
        {
            case "AFSouth1":
                clientRegion = RegionEndpoint.AFSouth1;
                break;
            case "APEast1":
                clientRegion = RegionEndpoint.APEast1;
                break;
            case "APNortheast1":
                clientRegion = RegionEndpoint.APNortheast1;
                break;
            case "APNortheast2":
                clientRegion = RegionEndpoint.APNortheast2;
                break;
            case "APNortheast3":
                clientRegion = RegionEndpoint.APNortheast3;
                break;
            case "APSouth1":
                clientRegion = RegionEndpoint.APSouth1;
                break;
            case "APSoutheast1":
                clientRegion = RegionEndpoint.APSoutheast1;
                break;
            case "APSoutheast2":
                clientRegion = RegionEndpoint.APSoutheast2;
                break;
            case "CACentral1":
                clientRegion = RegionEndpoint.CACentral1;
                break;
            case "CNNorth1":
                clientRegion = RegionEndpoint.CNNorth1;
                break;
            case "CNNorthWest1":
                clientRegion = RegionEndpoint.CNNorthWest1;
                break;
            case "EUCentral1":
                clientRegion = RegionEndpoint.EUCentral1;
                break;
            case "EUNorth1":
                clientRegion = RegionEndpoint.EUNorth1;
                break;
            case "EUSouth1":
                clientRegion = RegionEndpoint.EUSouth1;
                break;
            case "EUWest1":
                clientRegion = RegionEndpoint.EUWest1;
                break;
            case "EUWest2":
                clientRegion = RegionEndpoint.EUWest2;
                break;
            case "EUWest3":
                clientRegion = RegionEndpoint.EUWest3;
                break;
            case "MESouth1":
                clientRegion = RegionEndpoint.MESouth1;
                break;
            case "SAEast1":
                clientRegion = RegionEndpoint.SAEast1;
                break;
            case "USEast1":
                clientRegion = RegionEndpoint.USEast1;
                break;
            case "USEast2":
                clientRegion = RegionEndpoint.USEast2;
                break;
            case "USGovCloudEast1":
                clientRegion = RegionEndpoint.USGovCloudEast1;
                break;
            case "USGovCloudWest1":
                clientRegion = RegionEndpoint.USGovCloudWest1;
                break;
            case "USWest1":
                clientRegion = RegionEndpoint.USWest1;
                break;
            case "USWest2":
                clientRegion = RegionEndpoint.USWest2;
                break;
        }

        // Create a connection to the AWS S3 storage account...
        AmazonS3Client s3Client = new(clientAccessKey, clientSecretKey, clientRegion);

        // Does the specified container already exist...
        AmazonS3Util.DoesS3BucketExistV2Async(s3Client, storageInformation.WorkspaceGUID.ToString()).Wait();
        if (AmazonS3Util.DoesS3BucketExistV2Async(s3Client, storageInformation.WorkspaceGUID.ToString()).Result)
        {
            // Read the file from the AWS S3 storage container...
            string fileType = storageInformation.IsFileCompressed ? "zip" : $"{storageInformation.FileType.ToLower()}";
            GetObjectRequest request = new()
            {
                BucketName = storageInformation.WorkspaceGUID.ToString(),
                Key = $"{storageInformation.FileName.ToLower()}.{fileType}"
            };
            try
            {
                using GetObjectResponse response = s3Client.GetObjectAsync(request).Result;
                using Stream responseStream = response.ResponseStream;
                using BinaryReader reader = new(responseStream);
                MemoryStream downloadedMemoryStream = new(reader.ReadBytes((int)responseStream.Length));
                downloadedMemoryStream.CopyTo(storageInformation.MemoryStream);
            }
            catch (Exception)
            {
                storageInformation.ErrorMessage = $"AWSS3Storage.ReadBlob - File {storageInformation.FileName.ToLower()}.{fileType} does not exist";
            }
        }
        else
        {
            storageInformation.ErrorMessage = $"Container {storageInformation.WorkspaceGUID} does not exist";
        }
    }
    catch (Exception)
    {
        storageInformation.ErrorMessage = "AWSS3Storage.ReadBlob - Unknown error";
    }
}
Updated on January 2, 2024

Was this article helpful?

Related Articles

Need Support?
Can’t find the answer you’re looking for? Don’t worry we’re here to help!
Contact Support

Leave a Comment