|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: Open document from AWS S3 in ASP.NET Core Document editor | Syncfusion |
| 4 | +description: Learn about how to Open document from AWS S3 in ASP.NET Core Document editor control of Syncfusion Essential JS 2 and more details. |
| 5 | +platform: document-processing |
| 6 | +control: Open document from AWS S3 |
| 7 | +documentation: ug |
| 8 | +domainurl: ##DomainURL## |
| 9 | +--- |
| 10 | + |
| 11 | +# Open document from AWS S3 |
| 12 | + |
| 13 | +To load a document from AWS S3 in a Document Editor, you can follow the steps below |
| 14 | + |
| 15 | + |
| 16 | +**Step 1:** Create a Simple Document Editor Sample in ASP.NET Core |
| 17 | + |
| 18 | +Start by following the steps provided in this [link](../../document-editor/getting-started-core) to create a simple Document Editor sample in ASP.NET Core. This will give you a basic setup of the Document Editor component. |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | +**Step 2:** Modify the `DocumentEditorController.cs` File in the Web Service Project |
| 23 | + |
| 24 | +* Open the `DocumentEditorController.cs` file in your web service project. |
| 25 | + |
| 26 | +* Import the required namespaces at the top of the file: |
| 27 | + |
| 28 | +```csharp |
| 29 | +using System.IO; |
| 30 | +using Amazon; |
| 31 | +using Amazon.S3; |
| 32 | +using Amazon.S3.Model; |
| 33 | +``` |
| 34 | + |
| 35 | +* Add the following private fields and constructor parameters to the `DocumentEditorController` class, In the constructor, assign the values from the configuration to the corresponding fields |
| 36 | + |
| 37 | +```csharp |
| 38 | +private IConfiguration _configuration; |
| 39 | +public readonly string _accessKey; |
| 40 | +public readonly string _secretKey; |
| 41 | +public readonly string _bucketName; |
| 42 | + |
| 43 | +public DocumentEditorController(IWebHostEnvironment hostingEnvironment, IMemoryCache cache, IConfiguration configuration) |
| 44 | +{ |
| 45 | + _hostingEnvironment = hostingEnvironment; |
| 46 | + _cache = cache; |
| 47 | + _configuration = configuration; |
| 48 | + _accessKey = _configuration.GetValue<string>("AccessKey"); |
| 49 | + _secretKey = _configuration.GetValue<string>("SecretKey"); |
| 50 | + _bucketName = _configuration.GetValue<string>("BucketName"); |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +* Create the `LoadFromS3()` method to load the document from AWS S3. |
| 55 | + |
| 56 | +```csharp |
| 57 | + |
| 58 | +[AcceptVerbs("Post")] |
| 59 | +[HttpPost] |
| 60 | +[EnableCors("AllowAllOrigins")] |
| 61 | +[Route("LoadFromS3")] |
| 62 | +//Post action for Loading the documents |
| 63 | +
|
| 64 | +public async Task<string> LoadFromS3([FromBody] Dictionary<string, string> onObject) |
| 65 | +{ |
| 66 | + MemoryStream stream = new MemoryStream(); |
| 67 | + |
| 68 | + if (jsonObject == null && !jsonObject.ContainsKey("documentName")) |
| 69 | + { |
| 70 | + return null; |
| 71 | + } |
| 72 | + RegionEndpoint bucketRegion = RegionEndpoint.USEast1; |
| 73 | + |
| 74 | + // Configure the AWS SDK with your access credentials and other settings |
| 75 | + var s3Client = new AmazonS3Client(_accessKey, _secretKey, bucketRegion); |
| 76 | + |
| 77 | + string documentName = jsonObject["documentName"]; |
| 78 | + |
| 79 | + // Specify the document name or retrieve it from a different source |
| 80 | + var response = await s3Client.GetObjectAsync(_bucketName, documentName); |
| 81 | + |
| 82 | + Stream responseStream = response.ResponseStream; |
| 83 | + responseStream.CopyTo(stream); |
| 84 | + stream.Seek(0, SeekOrigin.Begin); |
| 85 | + WordDocument document = WordDocument.Load(stream, FormatType.Docx); |
| 86 | + string json = Newtonsoft.Json.JsonConvert.SerializeObject(document); |
| 87 | + document.Dispose(); |
| 88 | + stream.Close(); |
| 89 | + return json; |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +* Open the `appsettings.json` file in your web service project, Add the following lines below the existing `"AllowedHosts"` configuration |
| 94 | + |
| 95 | +```json |
| 96 | +{ |
| 97 | + "Logging": { |
| 98 | + "LogLevel": { |
| 99 | + "Default": "Information", |
| 100 | + "Microsoft.AspNetCore": "Warning" |
| 101 | + } |
| 102 | + }, |
| 103 | + "AllowedHosts": "*", |
| 104 | + "AccessKey": "Your Access Key from AWS S3", |
| 105 | + "SecretKey": "Your Secret Key from AWS S3", |
| 106 | + "BucketName": "Your Bucket name from AWS S3" |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +N> Replace **Your Access Key from AWS S3**, **Your Secret Key from AWS S3**, and **Your Bucket name from AWS S3** with your actual AWS access key, secret key and bucket name |
| 111 | + |
| 112 | +**Step 3:** Modify the Index.cshtml File in the Document Editor sample |
| 113 | + |
| 114 | +In the client-side, the document is returned from the web service is opening using `open` method. |
| 115 | + |
| 116 | +{% tabs %} |
| 117 | +{% highlight cshtml tabtitle="CSHTML" %} |
| 118 | +{% include code-snippet/document-editor/asp-net-core/document-editor-container/open-aws-s3/tagHelper %} |
| 119 | +{% endhighlight %} |
| 120 | +{% highlight c# tabtitle="Document-editor.cs" %} |
| 121 | +{% include code-snippet/document-editor/asp-net-core/document-editor-container/open-aws-s3/document-editor.cs %} |
| 122 | +{% endhighlight %} |
| 123 | +{% endtabs %} |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | +N> The **AWSSDK.S3** NuGet package must be installed in your application to use the previous code example. |
0 commit comments