Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
We are trying to upload file in AWS s3 and after successful upload, we want to get the URL of that file in C#.

What I have tried:

We tried multiple ways, but it is not working.
Posted
Updated 24-Oct-23 0:57am
v2

1 solution

You can upload a file to an AWS S3 bucket and get the URL using the AWS SDK for .NET, which is available as the AWSSDK.S3 NuGet package.
First, install the AWSSDK.S3 package using NuGet. You can do this by running the following command in the Package Manager Console:
Install-Package AWSSDK.S3
C#
using Amazon;
using Amazon.S3;
using Amazon.S3.Transfer;

public class S3FileUploader
{
    private const string bucketName = "your-unique-bucket-name"; // Provide your unique bucket name
    private const string keyName = "your-unique-object-key"; // Provide your unique object key
    private const string filePath = "path-to-your-file"; // Provide the path to your file

    public static string UploadFile()
    {
        var region = RegionEndpoint.USEast1; // Provide the appropriate AWS region

        using (var client = new AmazonS3Client(region))
        {
            var fileTransferUtility = new TransferUtility(client);

            // Option 1: Upload a file
            fileTransferUtility.Upload(filePath, bucketName, keyName);

            // Option 2: Upload a file with the specified object key name
            fileTransferUtility.Upload(filePath, bucketName, keyName);

            // Get the URL of the uploaded file
            var url = $"https://{bucketName}.s3.amazonaws.com/{keyName}";

            return url;
        }
    }
}
Make sure to replace the placeholder values with your actual bucket name, object key, file path, and the appropriate AWS region. This code will upload the file to the specified S3 bucket and provide you with the URL of the uploaded file.
 
Share this answer
 
Comments
Member 15418280 26-Oct-23 5:18am    
How can we upload multiple files from that folder location

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900