Upload or download large files to and from Amazon S3 using an AWS SDK - Amazon Simple Storage Service

 

global using System.Text;

global using Amazon.S3;

global using Amazon.S3.Model;

global using Amazon.S3.Transfer;

global using TransferUtilityBasics;


// This Amazon S3 client uses the default user credentials

// defined for this computer.

using Microsoft.Extensions.Configuration;


IAmazonS3 client = new AmazonS3Client();

var transferUtil = new TransferUtility(client);

IConfiguration _configuration;


_configuration = new ConfigurationBuilder()

    .SetBasePath(Directory.GetCurrentDirectory())

    .AddJsonFile("settings.json") // Load test settings from JSON file.

    .AddJsonFile("settings.local.json",

        true) // Optionally load local settings.

    .Build();


// Edit the values in settings.json to use an S3 bucket and files that

// exist on your AWS account and on the local computer where you

// run this scenario.

var bucketName = _configuration["BucketName"];

var localPath = $"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\\TransferFolder";


DisplayInstructions();


PressEnter();


Console.WriteLine();


// Upload a single file to an S3 bucket.

DisplayTitle("Upload a single file");


var fileToUpload = _configuration["FileToUpload"];

Console.WriteLine($"Uploading {fileToUpload} to the S3 bucket, {bucketName}.");


var success = await TransferMethods.UploadSingleFileAsync(transferUtil, bucketName, fileToUpload, localPath);

if (success)

{

    Console.WriteLine($"Successfully uploaded the file, {fileToUpload} to {bucketName}.");

}


PressEnter();


// Upload a local directory to an S3 bucket.

DisplayTitle("Upload all files from a local directory");

Console.WriteLine("Upload all the files in a local folder to an S3 bucket.");

const string keyPrefix = "UploadFolder";

var uploadPath = $"{localPath}\\UploadFolder";


Console.WriteLine($"Uploading the files in {uploadPath} to {bucketName}");

DisplayTitle($"{uploadPath} files");

DisplayLocalFiles(uploadPath);

Console.WriteLine();


PressEnter();


success = await TransferMethods.UploadFullDirectoryAsync(transferUtil, bucketName, keyPrefix, uploadPath);

if (success)

{

    Console.WriteLine($"Successfully uploaded the files in {uploadPath} to {bucketName}.");

    Console.WriteLine($"{bucketName} currently contains the following files:");

    await DisplayBucketFiles(client, bucketName, keyPrefix);

    Console.WriteLine();

}


PressEnter();


// Download a single file from an S3 bucket.

DisplayTitle("Download a single file");

Console.WriteLine("Now we will download a single file from an S3 bucket.");


var keyName = _configuration["FileToDownload"];


Console.WriteLine($"Downloading {keyName} from {bucketName}.");


success = await TransferMethods.DownloadSingleFileAsync(transferUtil, bucketName, keyName, localPath);

if (success)

{

    Console.WriteLine("$Successfully downloaded the file, {keyName} from {bucketName}.");

}


PressEnter();


// Download the contents of a directory from an S3 bucket.

DisplayTitle("Download the contents of an S3 bucket");

var s3Path = _configuration["S3Path"];

var downloadPath = $"{localPath}\\{s3Path}";


Console.WriteLine($"Downloading the contents of {bucketName}\\{s3Path}");

Console.WriteLine($"{bucketName}\\{s3Path} contains the following files:");

await DisplayBucketFiles(client, bucketName, s3Path);

Console.WriteLine();


success = await TransferMethods.DownloadS3DirectoryAsync(transferUtil, bucketName, s3Path, downloadPath);

if (success)

{

    Console.WriteLine($"Downloaded the files in {bucketName} to {downloadPath}.");

    Console.WriteLine($"{downloadPath} now contains the following files:");

    DisplayLocalFiles(downloadPath);

}


Console.WriteLine("\nThe TransferUtility Basics application has completed.");

PressEnter();


// Displays the title for a section of the scenario.

static void DisplayTitle(string titleText)

{

    var sepBar = new string('-', Console.WindowWidth);


    Console.WriteLine(sepBar);

    Console.WriteLine(CenterText(titleText));

    Console.WriteLine(sepBar);

}


// Displays a description of the actions to be performed by the scenario.

static void DisplayInstructions()

{

    var sepBar = new string('-', Console.WindowWidth);


    DisplayTitle("Amazon S3 Transfer Utility Basics");

    Console.WriteLine("This program shows how to use the Amazon S3 Transfer Utility.");

    Console.WriteLine("It performs the following actions:");

    Console.WriteLine("\t1. Upload a single object to an S3 bucket.");

    Console.WriteLine("\t2. Upload an entire directory from the local computer to an\n\t  S3 bucket.");

    Console.WriteLine("\t3. Download a single object from an S3 bucket.");

    Console.WriteLine("\t4. Download the objects in an S3 bucket to a local directory.");

    Console.WriteLine($"\n{sepBar}");

}


// Pauses the scenario.

static void PressEnter()

{

    Console.WriteLine("Press <Enter> to continue.");

    _ = Console.ReadLine();

    Console.WriteLine("\n");

}


// Returns the string textToCenter, padded on the left with spaces

// that center the text on the console display.

static string CenterText(string textToCenter)

{

    var centeredText = new StringBuilder();

    var screenWidth = Console.WindowWidth;

    centeredText.Append(new string(' ', (int)(screenWidth - textToCenter.Length) / 2));

    centeredText.Append(textToCenter);

    return centeredText.ToString();

}


// Displays a list of file names included in the specified path.

static void DisplayLocalFiles(string localPath)

{

    var fileList = Directory.GetFiles(localPath);

    if (fileList.Length > 0)

    {

        foreach (var fileName in fileList)

        {

            Console.WriteLine(fileName);

        }

    }

}


// Displays a list of the files in the specified S3 bucket and prefix.

static async Task DisplayBucketFiles(IAmazonS3 client, string bucketName, string s3Path)

{

    ListObjectsV2Request request = new()

    {

        BucketName = bucketName,

        Prefix = s3Path,

        MaxKeys = 5,

    };


    var response = new ListObjectsV2Response();


    do

    {

        response = await client.ListObjectsV2Async(request);


        response.S3Objects

            .ForEach(obj => Console.WriteLine($"{obj.Key}"));


        // If the response is truncated, set the request ContinuationToken

        // from the NextContinuationToken property of the response.

        request.ContinuationToken = response.NextContinuationToken;

    } while (response.IsTruncated);

}

Upload or download large files to and from Amazon S3 using an AWS SDK - Amazon Simple Storage Service

DOT NET ADDA

interested in solving the problems based on technologies like Amazon AWS ,Google Cloud, Azure and Dot related technology like asp.net, C#, asp.net core API, swagger, react js,Jquery ,javascripts, bootstrap, css,html, ms sql,IIS,WPF ,WCF,Firebase,RDLC Report etc..

إرسال تعليق (0)
أحدث أقدم