Skip to content

Commit 3e9050e

Browse files
author
Konduru Keerthi Konduru Ravichandra Raju
committed
AWS-S3-Bucket
1 parent ea1579d commit 3e9050e

File tree

3 files changed

+58
-46
lines changed

3 files changed

+58
-46
lines changed

Read-and-Save-document/Open-and-save-Word-document/AWS-S3-Bucket/Open-Word-document/Open-Word-document/Controllers/HomeController.cs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using Amazon.Runtime;
21
using Amazon.S3;
32
using Microsoft.AspNetCore.Mvc;
43
using Open_Word_document.Models;
@@ -22,43 +21,43 @@ public IActionResult Index()
2221
}
2322
public async Task<IActionResult> EditDocument()
2423
{
25-
// Your AWS Storage Account bucket name
24+
//Your AWS Storage Account bucket name
2625
string bucketName = "your-bucket-name";
2726

28-
// Name of the Word file you want to load from AWS S3
27+
//Name of the Word file you want to load from AWS S3
2928
string key = "CreateWord.docx";
3029

3130
try
3231
{
33-
// Retrieve the document from AWS S3
32+
//Retrieve the document from AWS S3
3433
MemoryStream stream = await GetDocumentFromS3(bucketName, key);
3534

36-
// Set the position to the beginning of the MemoryStream
35+
//Set the position to the beginning of the MemoryStream
3736
stream.Position = 0;
3837

39-
// Create an instance of WordDocument
38+
//Create an instance of WordDocument
4039
using (WordDocument wordDocument = new WordDocument(stream, Syncfusion.DocIO.FormatType.Docx))
4140
{
42-
// Access the section in a Word document
41+
//Access the section in a Word document
4342
IWSection section = wordDocument.Sections[0];
4443

45-
// Add new paragraph to the section
44+
//Add new paragraph to the section
4645
IWParagraph paragraph = section.AddParagraph();
4746
paragraph.ParagraphFormat.FirstLineIndent = 36;
4847
paragraph.BreakCharacterFormat.FontSize = 12f;
4948

50-
// Add new text to the paragraph
49+
//Add new text to the paragraph
5150
IWTextRange textRange = paragraph.AppendText("In 2000, AdventureWorks Cycles bought a small manufacturing plant, Importadores Neptuno, located in Mexico. Importadores Neptuno manufactures several critical subcomponents for the AdventureWorks Cycles product line. These subcomponents are shipped to the Bothell location for final product assembly. In 2001, Importadores Neptuno, became the sole manufacturer and distributor of the touring bicycle product group.") as IWTextRange;
5251
textRange.CharacterFormat.FontSize = 12f;
5352

54-
// Saving the Word document to a MemoryStream
53+
//Saving the Word document to a MemoryStream
5554
MemoryStream outputStream = new MemoryStream();
5655
wordDocument.Save(outputStream, Syncfusion.DocIO.FormatType.Docx);
5756

58-
// Set the position as '0'.
57+
//Set the position as '0'.
5958
outputStream.Position = 0;
6059

61-
// Download the Word file in the browser
60+
//Download the Word file in the browser
6261
FileStreamResult fileStreamResult = new FileStreamResult(outputStream, "application/msword");
6362
fileStreamResult.FileDownloadName = "EditWord.docx";
6463
return fileStreamResult;
@@ -72,7 +71,7 @@ public async Task<IActionResult> EditDocument()
7271
}
7372
public async Task<MemoryStream> GetDocumentFromS3(string bucketName, string key)
7473
{
75-
// Configure AWS credentials and region
74+
//Configure AWS credentials and region
7675
var region = Amazon.RegionEndpoint.USEast1;
7776
var credentials = new Amazon.Runtime.BasicAWSCredentials("your-access-key", "your-secret-key");
7877
var config = new AmazonS3Config
@@ -84,17 +83,17 @@ public async Task<MemoryStream> GetDocumentFromS3(string bucketName, string key)
8483
{
8584
using (var client = new AmazonS3Client(credentials, config))
8685
{
87-
// Create a MemoryStream to copy the file content
86+
//Create a MemoryStream to copy the file content
8887
MemoryStream stream = new MemoryStream();
8988

90-
// Download the file from S3 into the MemoryStream
89+
//Download the file from S3 into the MemoryStream
9190
var response = await client.GetObjectAsync(new Amazon.S3.Model.GetObjectRequest
9291
{
9392
BucketName = bucketName,
9493
Key = key
9594
});
9695

97-
// Copy the response stream to the MemoryStream
96+
//Copy the response stream to the MemoryStream
9897
await response.ResponseStream.CopyToAsync(stream);
9998

10099
return stream;
@@ -107,7 +106,6 @@ public async Task<MemoryStream> GetDocumentFromS3(string bucketName, string key)
107106
throw; // or handle the exception as needed
108107
}
109108
}
110-
111109
public IActionResult Privacy()
112110
{
113111
return View();

Read-and-Save-document/Open-and-save-Word-document/AWS-S3-Bucket/Save-Word-document/Save-Word-document/Controllers/HomeController.cs

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,17 @@ public IActionResult Index()
2323
{
2424
return View();
2525
}
26-
public async Task<IActionResult> CreateDocument()
26+
public async Task<IActionResult> UploadDocument()
2727
{
28-
// Creating a new document.
28+
//Creating a new document
2929
WordDocument document = new WordDocument();
30-
//Adding a new section to the document.
30+
31+
//Adding a new section to the document
3132
WSection section = document.AddSection() as WSection;
33+
3234
//Set Margin of the section
3335
section.PageSetup.Margins.All = 72;
36+
3437
//Set page size of the section
3538
section.PageSetup.PageSize = new Syncfusion.Drawing.SizeF(612, 792);
3639

@@ -54,7 +57,8 @@ public async Task<IActionResult> CreateDocument()
5457
style.ParagraphFormat.OutlineLevel = OutlineLevel.Level1;
5558

5659
IWParagraph paragraph = section.HeadersFooters.Header.AddParagraph();
57-
// Gets the image stream.
60+
61+
//Gets the image stream
5862
FileStream imageStream = new FileStream("AdventureCycle.jpg", FileMode.Open, FileAccess.Read);
5963
IWPicture picture = paragraph.AppendPicture(imageStream);
6064
picture.TextWrappingStyle = TextWrappingStyle.InFrontOfText;
@@ -72,22 +76,22 @@ public async Task<IActionResult> CreateDocument()
7276
textRange.CharacterFormat.FontName = "Calibri";
7377
textRange.CharacterFormat.TextColor = Syncfusion.Drawing.Color.Red;
7478

75-
//Appends paragraph.
79+
//Appends paragraph
7680
paragraph = section.AddParagraph();
7781
paragraph.ApplyStyle("Heading 1");
7882
paragraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center;
7983
textRange = paragraph.AppendText("Adventure Works Cycles") as WTextRange;
8084
textRange.CharacterFormat.FontSize = 18f;
8185
textRange.CharacterFormat.FontName = "Calibri";
8286

83-
//Appends paragraph.
87+
//Appends paragraph
8488
paragraph = section.AddParagraph();
8589
paragraph.ParagraphFormat.FirstLineIndent = 36;
8690
paragraph.BreakCharacterFormat.FontSize = 12f;
8791
textRange = paragraph.AppendText("Adventure Works Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company. The company manufactures and sells metal and composite bicycles to North American, European and Asian commercial markets. While its base operation is in Bothell, Washington with 290 employees, several regional sales teams are located throughout their market base.") as WTextRange;
8892
textRange.CharacterFormat.FontSize = 12f;
8993

90-
//Appends paragraph.
94+
//Appends paragraph
9195
paragraph = section.AddParagraph();
9296
paragraph.ParagraphFormat.FirstLineIndent = 36;
9397
paragraph.BreakCharacterFormat.FontSize = 12f;
@@ -101,16 +105,18 @@ public async Task<IActionResult> CreateDocument()
101105
textRange.CharacterFormat.FontSize = 16f;
102106
textRange.CharacterFormat.FontName = "Calibri";
103107

104-
//Appends table.
108+
//Appends table
105109
IWTable table = section.AddTable();
106110
table.ResetCells(3, 2);
107111
table.TableFormat.Borders.BorderType = BorderStyle.None;
108112
table.TableFormat.IsAutoResized = true;
109-
//Appends paragraph.
113+
114+
//Appends paragraph
110115
paragraph = table[0, 0].AddParagraph();
111116
paragraph.ParagraphFormat.AfterSpacing = 0;
112117
paragraph.BreakCharacterFormat.FontSize = 12f;
113-
//Appends picture to the paragraph.
118+
119+
//Appends picture to the paragraph
114120
FileStream image1 = new FileStream("Mountain-200.jpg", FileMode.Open, FileAccess.Read);
115121
picture = paragraph.AppendPicture(image1);
116122
picture.TextWrappingStyle = TextWrappingStyle.TopAndBottom;
@@ -121,13 +127,14 @@ public async Task<IActionResult> CreateDocument()
121127
picture.WidthScale = 79;
122128
picture.HeightScale = 79;
123129

124-
//Appends paragraph.
130+
//Appends paragraph
125131
paragraph = table[0, 1].AddParagraph();
126132
paragraph.ApplyStyle("Heading 1");
127133
paragraph.ParagraphFormat.AfterSpacing = 0;
128134
paragraph.ParagraphFormat.LineSpacing = 12f;
129135
paragraph.AppendText("Mountain-200");
130-
//Appends paragraph.
136+
137+
//Appends paragraph
131138
paragraph = table[0, 1].AddParagraph();
132139
paragraph.ParagraphFormat.AfterSpacing = 0;
133140
paragraph.ParagraphFormat.LineSpacing = 12f;
@@ -145,19 +152,21 @@ public async Task<IActionResult> CreateDocument()
145152
textRange = paragraph.AppendText("Price: $2,294.99\r") as WTextRange;
146153
textRange.CharacterFormat.FontSize = 12f;
147154
textRange.CharacterFormat.FontName = "Times New Roman";
148-
//Appends paragraph.
155+
156+
//Appends paragraph
149157
paragraph = table[0, 1].AddParagraph();
150158
paragraph.ParagraphFormat.AfterSpacing = 0;
151159
paragraph.ParagraphFormat.LineSpacing = 12f;
152160
paragraph.BreakCharacterFormat.FontSize = 12f;
153161

154-
//Appends paragraph.
162+
//Appends paragraph
155163
paragraph = table[1, 0].AddParagraph();
156164
paragraph.ApplyStyle("Heading 1");
157165
paragraph.ParagraphFormat.AfterSpacing = 0;
158166
paragraph.ParagraphFormat.LineSpacing = 12f;
159167
paragraph.AppendText("Mountain-300 ");
160-
//Appends paragraph.
168+
169+
//Appends paragraph
161170
paragraph = table[1, 0].AddParagraph();
162171
paragraph.ParagraphFormat.AfterSpacing = 0;
163172
paragraph.ParagraphFormat.LineSpacing = 12f;
@@ -175,17 +184,19 @@ public async Task<IActionResult> CreateDocument()
175184
textRange = paragraph.AppendText("Price: $1,079.99\r") as WTextRange;
176185
textRange.CharacterFormat.FontSize = 12f;
177186
textRange.CharacterFormat.FontName = "Times New Roman";
178-
//Appends paragraph.
187+
188+
//Appends paragraph
179189
paragraph = table[1, 0].AddParagraph();
180190
paragraph.ParagraphFormat.AfterSpacing = 0;
181191
paragraph.ParagraphFormat.LineSpacing = 12f;
182192
paragraph.BreakCharacterFormat.FontSize = 12f;
183193

184-
//Appends paragraph.
194+
//Appends paragraph
185195
paragraph = table[1, 1].AddParagraph();
186196
paragraph.ApplyStyle("Heading 1");
187197
paragraph.ParagraphFormat.LineSpacing = 12f;
188-
//Appends picture to the paragraph.
198+
199+
//Appends picture to the paragraph
189200
FileStream image2 = new FileStream("Mountain-300.jpg", FileMode.Open, FileAccess.Read);
190201
picture = paragraph.AppendPicture(image2);
191202
picture.TextWrappingStyle = TextWrappingStyle.TopAndBottom;
@@ -196,11 +207,12 @@ public async Task<IActionResult> CreateDocument()
196207
picture.WidthScale = 75;
197208
picture.HeightScale = 75;
198209

199-
//Appends paragraph.
210+
//Appends paragraph
200211
paragraph = table[2, 0].AddParagraph();
201212
paragraph.ApplyStyle("Heading 1");
202213
paragraph.ParagraphFormat.LineSpacing = 12f;
203-
//Appends picture to the paragraph.
214+
215+
//Appends picture to the paragraph
204216
FileStream image3 = new FileStream("Road-550-W.jpg", FileMode.Open, FileAccess.Read);
205217
picture = paragraph.AppendPicture(image3);
206218
picture.TextWrappingStyle = TextWrappingStyle.TopAndBottom;
@@ -211,13 +223,14 @@ public async Task<IActionResult> CreateDocument()
211223
picture.WidthScale = 92;
212224
picture.HeightScale = 92;
213225

214-
//Appends paragraph.
226+
//Appends paragraph
215227
paragraph = table[2, 1].AddParagraph();
216228
paragraph.ApplyStyle("Heading 1");
217229
paragraph.ParagraphFormat.AfterSpacing = 0;
218230
paragraph.ParagraphFormat.LineSpacing = 12f;
219231
paragraph.AppendText("Road-150 ");
220-
//Appends paragraph.
232+
233+
//Appends paragraph
221234
paragraph = table[2, 1].AddParagraph();
222235
paragraph.ParagraphFormat.AfterSpacing = 0;
223236
paragraph.ParagraphFormat.LineSpacing = 12f;
@@ -235,28 +248,29 @@ public async Task<IActionResult> CreateDocument()
235248
textRange = paragraph.AppendText("Price: $3,578.27\r") as WTextRange;
236249
textRange.CharacterFormat.FontSize = 12f;
237250
textRange.CharacterFormat.FontName = "Times New Roman";
238-
//Appends paragraph.
251+
252+
//Appends paragraph
239253
section.AddParagraph();
240254

241255
//Saves the Word document to MemoryStream
242256
MemoryStream stream = new MemoryStream();
243257
document.Save(stream, FormatType.Docx);
244258
stream.Position = 0;
245-
259+
246260
//Your AWS Storage Account bucket name
247261
string bucketName = "your-bucket-name";
248262

249263
//Name of the Word file you want to upload
250264
string key = "CreateWord.docx";
251265

252-
// Upload the document to AWS S3
266+
//Upload the document to AWS S3
253267
await UploadDocumentToS3(bucketName, key, stream);
254-
268+
255269
return Ok("Word document uploaded to AWS S3 Storage.");
256270
}
257271
public async Task<MemoryStream> UploadDocumentToS3(string bucketName, string key, MemoryStream stream)
258272
{
259-
// Configure AWS credentials and region
273+
//Configure AWS credentials and region
260274
var region = Amazon.RegionEndpoint.USEast1;
261275
var credentials = new Amazon.Runtime.BasicAWSCredentials("your-access-key", "your-secret-key");
262276
var config = new AmazonS3Config
@@ -270,7 +284,7 @@ public async Task<MemoryStream> UploadDocumentToS3(string bucketName, string key
270284

271285
try
272286
{
273-
// Upload the stream to AWS S3
287+
//Upload the stream to AWS S3
274288
await fileTransferUtility.UploadAsync(stream, bucketName, key);
275289
Console.WriteLine("Upload completed successfully");
276290
}

Read-and-Save-document/Open-and-save-Word-document/AWS-S3-Bucket/Save-Word-document/Save-Word-document/Views/Home/Index.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
ViewData["Title"] = "Home Page";
33
}
44

5-
@{Html.BeginForm("CreateDocument", "Home", FormMethod.Get);
5+
@{Html.BeginForm("UploadDocument", "Home", FormMethod.Get);
66
{
77
<div>
88
<input type="submit" value="Upload Document" style="width:150px;height:27px" />

0 commit comments

Comments
 (0)