Amazon S3.NET Core how to upload a file

I would like to download a file from Amazon S3 as part of a .NET Core project. Are there any links on how to create and use an AmazonS3 client? All I can find in the AmazonS3 documentation for .Net Core is this ( http://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/net-dg-config-netcore.html ) which is not very helpful.

+10


source to share


5 answers


I used IFormFile, like this:

(You need to install AWSSDK.S3

)



public async Task UploadFileToS3(IFormFile file)
{
    using (var client = new AmazonS3Client("yourAwsAccessKeyId", "yourAwsSecretAccessKey", RegionEndpoint.USEast1))
    {
        using (var newMemoryStream = new MemoryStream())
        {
            file.CopyTo(newMemoryStream);

            var uploadRequest = new TransferUtilityUploadRequest
            {
                InputStream = newMemoryStream,
                Key = file.FileName,
                BucketName = "yourBucketName",
                CannedACL = S3CannedACL.PublicRead
            };

            var fileTransferUtility = new TransferUtility(client);
            await fileTransferUtility.UploadAsync(uploadRequest);
        }
    }
}

      

+22


source


For a simple download of files in a .netcore project, I followed this link .

Having finished the simple file upload procedure, I followed the documentation on this and this links which were very helpful. The next two links were also helpful for a quick start.

  1. https://github.com/awslabs/aws-sdk-net-samples/blob/master/ConsoleSamples/AmazonS3Sample/AmazonS3Sample/S3Sample.cs

  2. http://www.c-sharpcorner.com/article/fileupload-to-aws-s3-using-asp-net/

These were my final pieces of code in the controller for uploading files (I missed the browsing part which is explained in detail in the link above).

[HttpPost("UploadFiles")]
public IActionResult UploadFiles(List<IFormFile> files)
{
     long size = files.Sum(f => f.Length);

     foreach (var formFile in files)
     {
           if (formFile.Length > 0)
           {
                var filename = ContentDispositionHeaderValue
                        .Parse(formFile.ContentDisposition)
                        .FileName
                        .TrimStart().ToString();
                filename = _hostingEnvironment.WebRootPath + $@"\uploads" + $@"\{formFile.FileName}";
                size += formFile.Length;
                using (var fs = System.IO.File.Create(filename))
                {
                     formFile.CopyTo(fs);
                     fs.Flush();
                }//these code snippets saves the uploaded files to the project directory

                 uploadToS3(filename);//this is the method to upload saved file to S3

            }
      }

      return RedirectToAction("Index", "Library");
}

      



This is the way to upload files to Amazon S3:

        private IHostingEnvironment _hostingEnvironment;
        private AmazonS3Client _s3Client = new AmazonS3Client(RegionEndpoint.EUWest2);
        private string _bucketName = "mis-pdf-library";//this is my Amazon Bucket name
        private static string _bucketSubdirectory = String.Empty;

        public UploadController(IHostingEnvironment environment)
        {
            _hostingEnvironment = environment;
        }


        public void uploadToS3(string filePath)
        {
            try
            {
                TransferUtility fileTransferUtility = new
                    TransferUtility(new AmazonS3Client(Amazon.RegionEndpoint.EUWest2));

                string bucketName;


                if (_bucketSubdirectory == "" || _bucketSubdirectory == null)
                {
                    bucketName = _bucketName; //no subdirectory just bucket name  
                }
                else
                {   // subdirectory and bucket name  
                    bucketName = _bucketName + @"/" + _bucketSubdirectory;
                }


                // 1. Upload a file, file name is used as the object key name.
                fileTransferUtility.Upload(filePath, bucketName);
                Console.WriteLine("Upload 1 completed");


            }
            catch (AmazonS3Exception s3Exception)
            {
                Console.WriteLine(s3Exception.Message,
                                  s3Exception.InnerException);
            }
        }

      

That was all for uploading files to the Amazon S3 bucket. I've been working on .netcore 2.0 and also remember to add the required dependencies to use the Amazon API. These were:

  1. AWSSDK.Core
  2. AWSSDK.Extensions.NETCore.Setup
  3. AWSSDK.S3

Hope this helps.

+14


source


In the AWS SDK docs, Core Core support was added at the end of 2016.

https://aws.amazon.com/sdk-for-net/

So the instructions for uploading files to S3 should be identical to those for .Net.

The " getting started" tutorial for the AWS SDK for .Net is literally the case where you describe connecting and uploading a file to S3 - and is included as a sample project ready to run if you have installed the "AWS Toolkit for Visual Studio" (which should be installed with SDK.Net AWS).

So, all you have to do is open visual studio, find a sample S3 project, or you can take a look here:

            // simple object put
            PutObjectRequest request = new PutObjectRequest()
            {
                ContentBody = "this is a test",
                BucketName = bucketName,
                Key = keyName
            };

            PutObjectResponse response = client.PutObject(request);

      

It is assumed that you have created an instance of Amazon.S3.AmazonS3Client after enabling the namespace and configured it with your own credentials.

+1


source


First you need to install in Package Manager Console

:

Install-package AWSSDK.Extensions.NETCORE.Setup

Install-package AWSSDK.S3

      

Then you need to have a credential file in the directory:

C:\Users\username\.aws\credentials

      

The credential file should be in the following format:

[default]
aws_access_key_id=[Write your access key in here]
aws_secret_access_key=[Write your secret access key in here]
region=[Write your region here]

      

I have uploaded an example of basic CRUD in ASP.NET CORE for S3 buckets to github .

0


source


We ran into a problem while implementing a high level API in the .net core. When clients had low bandwidth around 3Mbps, Amazon S3 would throw an error (the XML file you provided was not well-formed). To solve this problem, we had to implement the implementation using a low-level API.

https://docs.aws.amazon.com/en_us/AmazonS3/latest/dev/LLuploadFileDotNet.html

// Create list to store upload part responses.
List<UploadPartResponse> uploadResponses = new List<UploadPartResponse>();

// Setup information required to initiate the multipart upload.
InitiateMultipartUploadRequest initiateRequest = new InitiateMultipartUploadRequest{
    BucketName = bucketName,
    Key = pathbucket
};

//Add metadata to file 
string newDate = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss");                    

// Initiate the upload.
InitiateMultipartUploadResponse initResponse = await s3Client.InitiateMultipartUploadAsync(initiateRequest);      
int uploadmb = 5;

// Upload parts.
long contentLength = new FileInfo(zippath).Length;
long partSize = uploadmb * (long)Math.Pow(2, 20); // 5 MB         

try 
{
    long filePosition = 0;
    for (int i = 1; filePosition < contentLength; i++) {
        UploadPartRequest uploadRequest = new UploadPartRequest{
            BucketName = bucketName,
            Key = pathbucket,
            UploadId = initResponse.UploadId,
            PartNumber = i,
            PartSize = partSize,
            FilePosition = filePosition,
            FilePath = zippath
        };

        // Track upload progress.
        uploadRequest.StreamTransferProgress += new EventHandler<StreamTransferProgressArgs>(UploadPartProgressEventCallback);
        // Upload a part and add the response to our list.
        uploadResponses.Add(await s3Client.UploadPartAsync(uploadRequest));
        filePosition += partSize;
    }

    // Setup to complete the upload.
    CompleteMultipartUploadRequest completeRequest = new CompleteMultipartUploadRequest {
        BucketName = bucketName,
        Key = pathbucket,
        UploadId = initResponse.UploadId
    };
    completeRequest.AddPartETags(uploadResponses);

    // Complete the upload.
    CompleteMultipartUploadResponse completeUploadResponse = await s3Client.CompleteMultipartUploadAsync(completeRequest);
} 
catch (Exception exception) 
{
    Console.WriteLine("An AmazonS3Exception was thrown: { 0}", exception.Message);

    // Abort the upload.
    AbortMultipartUploadRequest abortMPURequest = new AbortMultipartUploadRequest {
        BucketName = bucketName,
        Key = keyName,
        UploadId = initResponse.UploadId
    };
}

      

0


source







All Articles