static async Task PutObjectToS3Async(BucketCredentials credentials, BucketProperties bucketProperties) { try { var client = new AmazonS3Client(credentials.AccessKeyId, credentials.SecretAccessKey, credentials.SessionToken, bucketProperties.BucketRegion); var putRequest = new PutObjectRequest { BucketName = bucketProperties.BucketName, Key = bucketProperties.BucketObjectKey, FilePath = bucketProperties.BucketFilePath, }; await client.PutObjectAsync(putRequest); Console.WriteLine($"Successfully put {bucketProperties.BucketObjectKey} to {bucketProperties.BucketName}"); } 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); } }
private static void FailureScenarioUploadingToS3(BucketProperties bucketProperties) { // Fails with following error // Error encountered ***. Message:'The provided token is malformed or otherwise invalid.' when writing an object var bucketCredentials = new BucketCredentials { AccessKeyId = "AKIAJ42OR63QAZ7REWNQ", // replace with the correct access key SecretAccessKey = "KjdlCjUf2Vag7sPtRGpqZjzvZfKbh5CRKbDBRxVy", // replace with correct secret key SessionToken = _roleSessionName }; PutObjectToS3Async(bucketCredentials, bucketProperties).Wait(); }