public static string PreSignedUrl(TemporaryAWSCredentials creds, string fileKey) { string url = ""; //var s3Client = new AmazonS3Client(new SessionAWSCredentials(creds.AccessKeyId, creds.SecretAccessKey, creds.Token))) ResponseHeaderOverrides headerOverrides = new ResponseHeaderOverrides(); headerOverrides.ContentType = "application/pdf"; int secs = 0; do { using (var s3Client = AWSClientFactory.CreateAmazonS3Client(GetAccesskey(), GetSecretkey())) { GetPreSignedUrlRequest request = new GetPreSignedUrlRequest() .WithBucketName(GetBucketname()) .WithKey(fileKey.TrimStart('/')) .WithProtocol(Protocol.HTTP) .WithVerb(HttpVerb.GET) //.WithResponseHeaderOverrides(headerOverrides) .WithExpires(DateTime.Now.AddMinutes(120).AddSeconds(secs)); url = s3Client.GetPreSignedURL(request); secs++; } } while ((url.Contains("%2B") || url.Contains("%2b") || url.Contains("+")) && secs < 30); // try again until a signature with no + sign is generated. return(url); }
public static TemporaryAWSCredentials GetSecurityToken(string userName) { TemporaryAWSCredentials temporaryCreds = new TemporaryAWSCredentials(); Credentials sessionCredentials; // Create a client using the credentials from the Web.config file AmazonSecurityTokenServiceConfig config = new AmazonSecurityTokenServiceConfig(); AmazonSecurityTokenServiceClient client = new AmazonSecurityTokenServiceClient( GetAccesskey(), GetSecretkey(), config); // Build the aws username string awsUsername = BuildAWSUsername(userName); // Map policy based on whether this is an internal or external user. string policy = BuildAWSPolicy(UserType.Internal); // Store the attributes and request a new // Federated session(temporary security creds) GetFederationTokenRequest request = new GetFederationTokenRequest { DurationSeconds = 3600 * SESSION_DURATION, Name = awsUsername, Policy = policy }; GetFederationTokenResponse startSessionResponse = null; startSessionResponse = client.GetFederationToken(request); // Check the result returned i.e. Valid security credentials or null? if (startSessionResponse != null) { GetFederationTokenResult startSessionResult = startSessionResponse.GetFederationTokenResult; sessionCredentials = startSessionResult.Credentials; // Store all the returned keys and token to TemporarySecurityCreds object. temporaryCreds.User = userName; temporaryCreds.AccessKeyId = sessionCredentials.AccessKeyId; temporaryCreds.SecretAccessKey = sessionCredentials.SecretAccessKey; temporaryCreds.Expiration = sessionCredentials.Expiration; temporaryCreds.Token = sessionCredentials.SessionToken; return(temporaryCreds); } else { throw new Exception("Error in retrieving AWS temporary security creds,recieved NULL"); } }