Uploadstring Method Uploads the Credentials

Introduction

In this article, I will testify you lot how to upload a file (image/video/..) to Amazon S3 Bucket through an ASP.Internet web application. For this, showtime, you demand to have an AWS account in Amazon Web Services. You can create an AWS complimentary tier account that's valid for 12 months. Visit this link to know more about a free tier business relationship.

After signing into the AWS business relationship, the next matter you need is to create security credentials for your account which volition create a secret admission cardinal Id and secret access cardinal. The below images show how to do this.

Account name> My Security Credentials.

ASP.NET

Continue to Security Credentials.

ASP.NET

Click (+) Acess Keys> create New Access Key button > download the central file or accept note of your id and key somewhere.

ASP.NET

Next, create an S3 Bucket. Click the "Services" tab and select S3 under the Storage division. This will take yous to Amazon S3 page. On clicking the "Create" button, the following pop upwards will come up. Give a bucket name and select a region. Click "Side by side" and permit the public to read it.

ASP.NET

Now, we need to create a bucket policy that allows us to create rules for managing admission to our buckets and files.

Select your bucket > Permission tab > Bucket Policy > Policy Generator.

ASP.NET

This will accept you to a new page. Fill in the details as per your requirement. I accept selected two actions - putobject and getobject. And in ARN, give your bucket name such as  'test73'.

ASP.NET

Clicking "Add together Argument" volition enable a new push 'Policy Generator'. Click that button, copy, and paste the generated policy dorsum in your bucket policy.

ASP.NET

Click "Salve". At present, you are done with all Amazon settings. The next step is to create an ASP.NET Application.

Open Visual Studio, and get to File > New > website > ASP.NET Empty spider web Site. Give a project name and click OK.

ASP.NET

Add a new webform (empty) to your projection. Now, we need to add two references - AWSSDK.Core and AWSSDK.S3 to our project. Open NuGet Package Manager in VS and search awssdk.

Download these two references to your project.

ASP.NET

Now, add a fileupload control and a push control to your webform. The code for the webform is shown below.

  1. using Organization;
  2. using System.IO;
  3. namespace awsTestUpload
  4. {
  5. public  partial form  WebForm1 : System.Spider web.UI.Page
  6.     {
  7. protected void  Page_Load(object sender, EventArgs e)
  8.         {
  9.         }
  10. protected void  upload(cord path,string file)
  11.         {
  12.         }
  13. protected void  Button2_Click(object sender, EventArgs e)
  14.         {
  15.          Stream st = FileUpload1.PostedFile.InputStream;
  16.             string name = Path.GetFileName(FileUpload1.FileName);
  17.             string myBucketName ="test73" ;
  18.             string s3DirectoryName ="" ;
  19.             string s3FileName = @name;
  20.             bool a;
  21.             AmazonUploader myUploader =new  AmazonUploader();
  22.             a = myUploader.sendMyFileToS3(st, myBucketName, s3DirectoryName, s3FileName);
  23. if  (a == true )
  24.             {
  25.                 Response.Write("successfully uploaded" );
  26.             }
  27. else
  28.                 Response.Write("Mistake" );
  29.         }
  30.     }
  31. }

Now, right click on your project proper noun in Solution Explorer and add together a new class. I accept named information technology AmazonUploader.cs.

ASP.NET

The lawmaking for AmazonUploader.cs will be similar beneath.

  1. using Amazon.S3;
  2. using Amazon.S3.Transfer;
  3. namespace awsTestUpload
  4. {
  5. public class  AmazonUploader
  6.     {
  7. public  bool sendMyFileToS3(System.IO.Stream localFilePath, string bucketName, string subDirectoryInBucket, string fileNameInS3)
  8.         {
  9.             IAmazonS3 client =new  AmazonS3Client(RegionEndpoint.APSouth1);
  10.             TransferUtility utility =new  TransferUtility(client);
  11.             TransferUtilityUploadRequest request =new  TransferUtilityUploadRequest();
  12. if  (subDirectoryInBucket == ""  || subDirectoryInBucket == null )
  13.             {
  14.                 asking.BucketName = bucketName;
  15.             }
  16. else
  17.             {
  18.                 request.BucketName = bucketName + @"/"  + subDirectoryInBucket;
  19.             }
  20.             request.Key = fileNameInS3;
  21.             request.InputStream = localFilePath;
  22.             utility.Upload(asking);
  23. render truthful ;
  24.         }
  25.     }
  26. }

Now, y'all have to set up your AWS Secret Key Id and Underground Key in your webconfig file. Place the following code nether <configurations> tag in webconfig file.

  1. <appSettings>
  2.     <add key="AWSProfileName"  value= "Contour Name"  />
  3.     <add key="AWSAccessKey"  value= " Your Secret Primal Id "  />
  4.     <add together key="AWSSecretKey"  value= "Your Secret Key "  />
  5.     <!--AWSProfileName is used to reference an account that has been registeredwith  the SDK.
  6. If using AWS Toolkitfor  Visual Studio so this  value is the same value shown in  the AWS Explorer.
  7. It is also possible to register an account using the <solution-dir>/packages/AWSSDK-X.10.X.X/tools/account-management.ps1 PowerShell script
  8. that is bundledwith  the nuget bundle  under the tools folder.
  9.         <add primal="AWSProfileName"  value= ""  />
  10. -->
  11.   </appSettings>

If y'all want to upload large files, add the following settings too in webconfig file all under <configuration> tag.

  1. <system.spider web>
  2.  <compilation debug="true"  targetFramework= "4.half dozen"  />
  3.   <httpRuntime targetFramework="4.6"  maxRequestLength= "1048576"  executionTimeout= "3600"  />
  4. <pages>
  5.     <controls>
  6.       <add together tagPrefix="ajaxToolkit"  assembly= "AjaxControlToolkit"  namespace= "AjaxControlToolkit"  />
  7.     </controls>
  8.   </pages></organization.web>
  9.   <organisation.webServer>
  10.   <security>
  11.     <requestFiltering>
  12.       <requestLimits maxAllowedContentLength="1073741824"  />
  13.     </requestFiltering>
  14.   </security>
  15. </system.webServer>

That'due south it. Now, run the project.

Output

ASP.NET

Uploaded file in S3

ASP.NET

smithcought.blogspot.com

Source: https://www.c-sharpcorner.com/article/fileupload-to-aws-s3-using-asp-net/

0 Response to "Uploadstring Method Uploads the Credentials"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel