// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 using System; using System.IO; using System.Threading.Tasks; using Amazon.Runtime; using Amazon.S3; using Amazon.S3.Transfer; namespace S3Demo.Storage { /// /// This example shows how to use the TransferUtility api for Amazon Simple /// Storage Service (Amazon S3) to upload a single file to an Amazon S3 /// bucket. The example was created using the AWS SDK for .NET version 3.7 /// and .NET Core 5.0. /// public class UploadFileMPUHighLevelAPI { public static async Task Uploaderfile(string bucketName, string keyName, string path, BasicAWSCredentials credentials, AmazonS3Config conf) { //string bucketName = "doc-example-bucket"; //string keyName = "sample_pic.png"; //string path = "filepath/directory/"; string filePath = $"{path}{keyName}"; // If the AWS Region defined for your default user is different // from the Region where your Amazon S3 bucket is located, // pass the Region name to the S3 client object's constructor. // For example: RegionEndpoint.USWest2 or RegionEndpoint.USEast2. IAmazonS3 client = new AmazonS3Client(credentials, conf); await UploadFileAsync(client, bucketName, keyName, filePath); } /// /// This method uploads a file to an S3 bucket using a TransferUtility /// object. /// /// The initialized S3 client object used to /// perform the multi-part upload. /// The name of the bucket to which to upload /// the file. /// The file name to be used in the /// destination S3 bucket. /// The path, including the file name of the /// file to be uploaded to the S3 bucket. private static async Task UploadFileAsync( IAmazonS3 client, string bucketName, string keyName, string filePath) { try { var fileTransferUtility = new TransferUtility(client); // Upload a file. The file name is used as the object key name. await fileTransferUtility.UploadAsync(filePath, bucketName); Console.WriteLine("Upload 1 completed"); // Specify object key name explicitly. await fileTransferUtility.UploadAsync(filePath, bucketName, keyName); Console.WriteLine("Upload 2 completed"); // Upload data from a System.IO.Stream object. using (var fileToUpload = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { await fileTransferUtility.UploadAsync( fileToUpload, bucketName, keyName); } Console.WriteLine("Upload 3 completed"); // Option 4. Specify advanced settings. var fileTransferUtilityRequest = new TransferUtilityUploadRequest { BucketName = bucketName, FilePath = filePath, StorageClass = S3StorageClass.StandardInfrequentAccess, PartSize = 6291456, // 6 MB. Key = keyName, CannedACL = S3CannedACL.PublicRead, }; fileTransferUtilityRequest.Metadata.Add("param1", "Value1"); fileTransferUtilityRequest.Metadata.Add("param2", "Value2"); await fileTransferUtility.UploadAsync(fileTransferUtilityRequest); Console.WriteLine("Upload 4 completed"); } catch (AmazonS3Exception ex) { Console.WriteLine($"Error: {ex.Message}"); } } } }