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
- Create a S3 bucket.
- Upload an image file in the S3 bucket.
- Create a project and write code to get the URL.
- Output.
Create A S3 Bucket
Upload An Image File In The 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
Click to Enlarge
Click to Enlarge