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.
Continue to Security Credentials.
Click (+) Acess Keys> create New Access Key button > download the central file or accept note of your id and key somewhere.
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.
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.
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'.
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.
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.
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.
Now, add a fileupload control and a push control to your webform. The code for the webform is shown below.
- using Organization;
- using System.IO;
- namespace awsTestUpload
- {
- public partial form WebForm1 : System.Spider web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- protected void upload(cord path,string file)
- {
- }
- protected void Button2_Click(object sender, EventArgs e)
- {
- Stream st = FileUpload1.PostedFile.InputStream;
- string name = Path.GetFileName(FileUpload1.FileName);
- string myBucketName ="test73" ;
- string s3DirectoryName ="" ;
- string s3FileName = @name;
- bool a;
- AmazonUploader myUploader =new AmazonUploader();
- a = myUploader.sendMyFileToS3(st, myBucketName, s3DirectoryName, s3FileName);
- if (a == true )
- {
- Response.Write("successfully uploaded" );
- }
- else
- Response.Write("Mistake" );
- }
- }
- }
Now, right click on your project proper noun in Solution Explorer and add together a new class. I accept named information technology AmazonUploader.cs.
The lawmaking for AmazonUploader.cs will be similar beneath.
- using Amazon.S3;
- using Amazon.S3.Transfer;
- namespace awsTestUpload
- {
- public class AmazonUploader
- {
- public bool sendMyFileToS3(System.IO.Stream localFilePath, string bucketName, string subDirectoryInBucket, string fileNameInS3)
- {
- IAmazonS3 client =new AmazonS3Client(RegionEndpoint.APSouth1);
- TransferUtility utility =new TransferUtility(client);
- TransferUtilityUploadRequest request =new TransferUtilityUploadRequest();
- if (subDirectoryInBucket == "" || subDirectoryInBucket == null )
- {
- asking.BucketName = bucketName;
- }
- else
- {
- request.BucketName = bucketName + @"/" + subDirectoryInBucket;
- }
- request.Key = fileNameInS3;
- request.InputStream = localFilePath;
- utility.Upload(asking);
- render truthful ;
- }
- }
- }
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.
- <appSettings>
- <add key="AWSProfileName" value= "Contour Name" />
- <add key="AWSAccessKey" value= " Your Secret Primal Id " />
- <add together key="AWSSecretKey" value= "Your Secret Key " />
- <!--AWSProfileName is used to reference an account that has been registeredwith the SDK.
- If using AWS Toolkitfor Visual Studio so this value is the same value shown in the AWS Explorer.
- It is also possible to register an account using the <solution-dir>/packages/AWSSDK-X.10.X.X/tools/account-management.ps1 PowerShell script
- that is bundledwith the nuget bundle under the tools folder.
- <add primal="AWSProfileName" value= "" />
- -->
- </appSettings>
If y'all want to upload large files, add the following settings too in webconfig file all under <configuration> tag.
- <system.spider web>
- <compilation debug="true" targetFramework= "4.half dozen" />
- <httpRuntime targetFramework="4.6" maxRequestLength= "1048576" executionTimeout= "3600" />
- <pages>
- <controls>
- <add together tagPrefix="ajaxToolkit" assembly= "AjaxControlToolkit" namespace= "AjaxControlToolkit" />
- </controls>
- </pages></organization.web>
- <organisation.webServer>
- <security>
- <requestFiltering>
- <requestLimits maxAllowedContentLength="1073741824" />
- </requestFiltering>
- </security>
- </system.webServer>
That'due south it. Now, run the project.
Output
Uploaded file in S3
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