Articles → AWS SDK AND CSHARP → Generate The Presigned URL Of The S3 Bucket’S Object Using C#

Generate The Presigned URL Of The S3 Bucket’S Object Using C#






Steps




  1. Create a S3 bucket.
  2. Upload an image file in the S3 bucket.
  3. Create a project and write code to get the URL.
  4. Output.



Create A S3 Bucket





Upload An Image File In The S3 Bucket




Picture showing uploading an image file on S3 bucket

Click to Enlarge


Create A Project And Write Code To Get The URL




using Amazon;
using Amazon.S3;
using Amazon.S3.Model;
using System;

namespace dotnetS3
{
    class Program
    {

        public static void Main(string[] args)
        {

            string bucketName = "gyansangrah-bucket";

            string URL = GetPresignedURL(bucketName, "__counter_function_jmeter_five.jpg");

            Console.WriteLine(URL);
            Console.ReadLine();
        }

        static string GetPresignedURL(string bucketName, string filekey)
        {
            string url = "";
            if (!string.IsNullOrEmpty(filekey) && !string.IsNullOrWhiteSpace(filekey))
                using (IAmazonS3 client = new AmazonS3Client("<Access Id>", "<Secret Key>", RegionEndpoint.USEast2))
                {
                    GetPreSignedUrlRequest request = new GetPreSignedUrlRequest();
                    request.BucketName = bucketName;
                    request.Key = filekey.TrimEnd('/');
                    request.Verb = HttpVerb.GET;
                    request.Expires = DateTime.UtcNow.AddMinutes(1);
                    url = client.GetPreSignedURL(request);
                }
            return url;
        }

       

    }
}



Output


Picture showing generating the token for presigned URL

Click to Enlarge



Picture showing the image file accessed using the presigned URL

Click to Enlarge




Posted By  -  Karan Gupta
 
Posted On  -  Thursday, February 17, 2022

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250