Click here to Skip to main content
15,889,403 members
Please Sign up or sign in to vote.
1.22/5 (3 votes)
See more:
I has access key,secret key and bucketname.And I want to download the file on the server with amazon s3 using them.How do I download with these 3 information.

What I have tried:

I searched and I applied the code I found, but I get an error.Error is connection failure was thrown.

C#
private const string bucketName = "my bucket name";
        private const string keyName = " my secret key";
        // Specify your bucket region (an example region is shown).
        private static readonly RegionEndpoint bucketRegion = RegionEndpoint.USWest2;
        private static IAmazonS3 client;

        public static void Main()
        {
            client = new AmazonS3Client(bucketRegion);
            ReadObjectDataAsync().Wait();
        }

        static async Task ReadObjectDataAsync()
        {
            string responseBody = "";
            try
            {
                GetObjectRequest request = new GetObjectRequest
                {
                    BucketName = bucketName,
                    Key = keyName
                };
                using (GetObjectResponse response = await client.GetObjectAsync(request))
                using (Stream responseStream = response.ResponseStream)
                using (StreamReader reader = new StreamReader(responseStream))
                {
                    string title = response.Metadata["x-amz-meta-title"]; // Assume you have "title" as medata added to the object.
                    string contentType = response.Headers["Content-Type"];
                    Console.WriteLine("Object metadata, Title: {0}", title);
                    Console.WriteLine("Content type: {0}", contentType);

                    responseBody = reader.ReadToEnd(); // Now you process the response body.
                }
            }
            catch (AmazonS3Exception e)
            {
                Console.WriteLine("Error encountered ***. Message:'{0}' when writing an object", e.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", e.Message);   //this line error.
            }
Posted
Updated 1-Oct-19 13:36pm
v4
Comments
[no name] 23-Sep-19 20:21pm    
Do you not think it would be beneficial to post that code so we can see what you might be doing wrong? After all, we are not mind readers, nor do we have psychic abilities to guess what's causing your error...
Member 14169626 24-Sep-19 16:00pm    
I've been trying to do it with my research from the internet.
ZurdoDev 2-Oct-19 9:46am    
The exact error would be helpful.
Refer to Amazon's documentation. I imagine they must have some examples.
What line of code causes the error?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900