title: Download Objects from S3 Bucket using c#
body: <p>Im trying to download object from S3 bucket facing below issue 
  <strong>The Security token included in the request is Invalid .</strong>
  Please check and correct where is the mistake.</p>
  
  <p>Below is my code 
  1. Get Temporary credentails:</p>
  
  <pre><code>main()    
  {
      string path = "http://XXX.XXX.XXX./latest/meta-data/iam/security-credentials/EC2_WLMA_Permissions";
  
                  HttpWebRequest request = (HttpWebRequest)WebRequest.Create(path);
                  request.Method = "GET";
                  request.ContentType = "application/json";
                  HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                  string result = string.Empty;
                  using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                  {
                      result = reader.ReadToEnd();
                      dynamic metaData = JsonConvert.DeserializeObject(result);
                      _awsAccessKeyId = metaData.AccessKeyId;
                      _awsSecretAccessKey = metaData.SecretAccessKey;
                  }
  }
  </code></pre>
  
  <ol start="2">
  <li><p>Create SessionAWSCredentials  instance:</p>
  
  <pre><code>SessionAWSCredentials tempCredentials =
                   GetTemporaryCredentials(_awsAccessKeyId, _awsSecretAccessKey);
  </code></pre>
  
  <p>//GetTemporaryCredentials method:</p>
  
  <pre><code>   private static SessionAWSCredentials GetTemporaryCredentials(
                      string accessKeyId, string secretAccessKeyId)
      {             
  
          AmazonSecurityTokenServiceClient stsClient =
          new AmazonSecurityTokenServiceClient(accessKeyId,
                                                   secretAccessKeyId);
          Console.WriteLine(stsClient.ToString());
          GetSessionTokenRequest getSessionTokenRequest =
                                           new GetSessionTokenRequest();
  
          getSessionTokenRequest.DurationSeconds = 7200; // seconds
          GetSessionTokenResponse sessionTokenResponse =
                        stsClient.GetSessionToken(getSessionTokenRequest);
  
          Console.WriteLine(sessionTokenResponse.ToString());
          Credentials credentials = sessionTokenResponse.Credentials;
          Console.WriteLine(credentials.ToString());
  
          SessionAWSCredentials sessionCredentials =
              new SessionAWSCredentials(credentials.AccessKeyId,
                                        credentials.SecretAccessKey,
                                        credentials.SessionToken);
  
  
          return sessionCredentials;
      }
  </code></pre></li>
  <li><p>Get files from <code>S3</code> using AmazonS3Client:   </p>
  
  <pre><code>using (IAmazonS3 client = new AmazonS3Client(tempCredentials,RegionEndpoint.USEast1))                        
      {
                      GetObjectRequest request = new GetObjectRequest();
                      request.BucketName = "bucketName" + @"/" + "foldername";
                      request.Key = "Terms.docx";
                      GetObjectResponse response = client.GetObject(request);
                      response.WriteResponseStreamToFile("C:\\MyFile.docx");
      }
  </code></pre></li>
  </ol>