· 7 years ago · Sep 02, 2018, 09:16 PM
1using System;
2using Amazon;
3using Amazon.S3;
4using Amazon.S3.Model;
5using System.IO;
6using System.Threading.Tasks;
7using System.Text;
8using System.Runtime.InteropServices;
9using Excel = Microsoft.Office.Interop.Excel; //microsoft Excel 14 object in references-> COM tab sfsa
10
11namespace BucketDemo
12{
13 class Program
14 {
15 private const string sourceBucket = "bucketname";
16 private const string objectKey = "excelFile.xlsx";
17
18 private const string accessKey = "AJNFSABFDSBHF";
19 private const string secretKey = "AJNFSABFDSBHF";
20 // Specify your bucket region (an example region is shown).
21 private static readonly RegionEndpoint bucketRegion = RegionEndpoint.USEast2;
22
23
24 public static void Main()
25 {
26
27
28
29 ReadObjectDataAsync().Wait();
30 // ReadExcelFile();
31 Console.WriteLine("finished");
32
33
34
35 }
36
37 static async Task ReadObjectDataAsync()
38 {
39 IAmazonS3 client = new AmazonS3Client(accessKey, secretKey, bucketRegion);
40 string responseBody = "";
41 try
42 {
43 GetObjectRequest request = new GetObjectRequest
44 {
45 BucketName = sourceBucket,
46 Key = objectKey
47 };
48 using (GetObjectResponse response = await client.GetObjectAsync(request))
49 using (Stream responseStream = response.ResponseStream)
50 using (StreamReader reader = new StreamReader(responseStream))
51 {
52 string title = response.Metadata["x-amz-meta-title"]; // Assume you have "title" as medata added to the object.
53 string contentType = response.Headers["Content-Type"];
54 Console.WriteLine("Object metadata, Title: {0}", title);
55 Console.WriteLine("Content type: {0}", contentType);
56
57 responseBody = reader.ReadToEnd(); // Now you process the response body.
58 }
59 }
60 catch (AmazonS3Exception e)
61 {
62 Console.WriteLine("Error encountered ***. Message:'{0}' when writing an object", e.Message);
63 }
64 catch (Exception e)
65 {
66 Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", e.Message);
67 }
68 }
69
70}**strong text**