Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to download use file amazon s3 bucket.But I dont know.How to write this download copy file in c# method and provide connection?

What I have tried:

public void File()
{
// I dont write this field.
}
Posted
Updated 14-Sep-20 9:03am

Amazon and AWS has invested a huge amount of effort in their documentation and everything is pretty much clear, Get an Object Using the AWS SDK for .NET - Amazon Simple Storage Service[^].

Only thing you need to have is the .NET SDK for AWS, and exploring the documentation of the SDK will help you in order to go ahead and perform the CRUD operations on the data you have.
 
Share this answer
 
// Get an amazon S3 client
    try
    {
        AmazonS3Config config = new AmazonS3Config();
        config.RegionEndpoint ="Your region" 
        m_s3Client = new AmazonS3Client("Your access key",
                                         "Your secret key",
                                          config);
         m_connected = true;
    }
    catch (Exception ex)
    {
        // Handle exception
        m_connected = false;
    }

/// Use it to get the file
    if (m_connected)
    try
    {
        GetObjectRequest request= 
        new GetObjectRequest
           { 
              BucketName = bucketName,
              Key = Key    // Key is of the form "folder/folder/filename"
                        // or if the file is directly in bucket just "filename" 
            };
        using (GetObjectResponse response = m_s3Client.GetObject(request))
        using (Stream responseStream = response.ResponseStream)
        using (StreamReader reader = new StreamReader(responseStream))
        {
            string contentType = response.Headers["Content-Type"];
            string responseBody = reader.ReadToEnd(); 
            // Now you process the response body.
            if (File.Exists(SelectedToDownload.FileName))
                File.Delete(SelectedToDownload.FileName);

             File.WriteAllText(SelectedToDownload.FileName, responseBody);
             string readText = File.ReadAllText(SelectedToDownload.FileName);
             
               
            }
        catch (AmazonS3Exception e)
        {
            // Handle Exceptions
        }
 
Share this answer
 

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