Home > other >  How to get response of AWS for upload link in ASP.NET Core?
How to get response of AWS for upload link in ASP.NET Core?

Time:09-27

I want to implement uploading images from Client-Side directly to an AWS S3 bucket.

I found a good tutorial in YouTube that show how to do it. Tutorial Link

But it's just for JavaScript.

I writing my program using ASP.NET core & I don't have any idea how to get the link from the AWS on the Back-End.

If you have done something similar, It would be great to share it with us.

CodePudding user response:

Please, remember about limitations:

https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/S3/TGetPreSignedUrlRequest.html

This final example creates a URL that allows a third party to put an object, then uses it to upload sample content. The URL is set to expire in 10 days.

GetPreSignedURL sample 5

// Create a client
AmazonS3Client client = new AmazonS3Client();

// Create a CopyObject request
GetPreSignedUrlRequest request = new GetPreSignedUrlRequest
{
    BucketName = "SampleBucket",
    Key = "Item1",
    Verb = HttpVerb.PUT,
    Expires = DateTime.UtcNow.AddDays(10)
};

// Get path for request
string path = client.GetPreSignedURL(request);

// Prepare data
byte[] data = UTF8Encoding.UTF8.GetBytes("Sample text.");

// Configure request
HttpWebRequest httpRequest = WebRequest.Create(path) as HttpWebRequest;
httpRequest.Method = "PUT";
httpRequest.ContentLength = data.Length;

// Write data to stream
Stream requestStream = httpRequest.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Close();

// Issue request
HttpWebResponse response = httpRequest.GetResponse() as HttpWebResponse;
            
  • Related