private void CreateSignedPolicy(PostObjectRequest request) { if (request.ContentType == null) { int pos = request.Key.LastIndexOf('.'); string ext = null; if (pos != -1) { ext = request.Key.Substring(pos, request.Key.Length - pos); request.ContentType = AmazonS3Util.MimeTypeFromExtension(ext); } else { request.ContentType = "application/octet-stream"; } } string policyString = null; int position = request.Key.LastIndexOf('/'); if (position == -1) { policyString = "{\"expiration\": \"" + DateTime.UtcNow.AddHours(24).ToString("yyyy-MM-ddTHH:mm:ssZ") + "\",\"conditions\": [{\"bucket\": \"" + request.Bucket + "\"},[\"starts-with\", \"$key\", \"" + "\"],{\"acl\": \"private\"},[\"eq\", \"$Content-Type\", " + "\"" + request.ContentType + "\"" + "]]}"; } else { policyString = "{\"expiration\": \"" + DateTime.UtcNow.AddHours(24).ToString("yyyy-MM-ddTHH:mm:ssZ") + "\",\"conditions\": [{\"bucket\": \"" + request.Bucket + "\"},[\"starts-with\", \"$key\", \"" + request.Key.Substring(0, position) + "/\"],{\"acl\": \"private\"},[\"eq\", \"$Content-Type\", " + "\"" + request.ContentType + "\"" + "]]}"; } request.SignedPolicy = S3PostUploadSignedPolicy.GetSignedPolicy(policyString, base.Credentials); }
private void InvokeHelperForS3PostObject(AsyncResult asyncResult, PostObjectRequest request) { if (asyncResult.RetriesAttempt == 0 || Config.ResignRetries) { // Add Post policy if (request.PostPolicy == null) { int position = request.Key.LastIndexOf('.'); string ext = null, contentType = null; if (position != -1) { ext = request.Key.Substring(position, request.Key.Length - position); contentType = AmazonS3Util.MimeTypeFromExtension(ext); } else { contentType = "application/octet-stream"; } request.PostPolicy = S3PostPolicyBuilder.GetPostPolicy(request.Bucket, request.Key, contentType); } if (Credentials is CognitoAWSCredentials) { var cred = Credentials as CognitoAWSCredentials; // very hacky solution cred.GetCredentialsAsync(delegate(AmazonServiceResult voidResult) { if (voidResult.Exception != null) { asyncResult.IsCompleted = true; AmazonLogging.LogError(AmazonLogging.AmazonLoggingLevel.Errors, "CognitoAWSCredentials", voidResult.Exception.Message); asyncResult.HandleException(voidResult.Exception); return; } request.SignedPolicy = S3PostUploadSignedPolicy.GetSignedPolicy(request.PostPolicy, cred); ProcessS3PostRequest(asyncResult, request); }, null); return; } request.SignedPolicy = S3PostUploadSignedPolicy.GetSignedPolicy(request.PostPolicy, Credentials); } ProcessS3PostRequest(asyncResult, request); }
private void CreateSignedPolicy(PostObjectRequest request) { StringBuilder metadataPolicy = new StringBuilder(); foreach (var kvp in request.Metadata) { var metakey = kvp.Key.StartsWith(S3Constants.PostFormDataXAmzPrefix, StringComparison.Ordinal) ? kvp.Key : S3Constants.PostFormDataMetaPrefix + kvp.Key; metadataPolicy.Append(string.Format(",{{\"{0}\": \"{1}\"}}", metakey, kvp.Value)); } StringBuilder headersPolicy = new StringBuilder(); foreach (var key in request.Headers.Keys) { headersPolicy.Append(string.Format(",{{\"{0}\": \"{1}\"}}", key, request.Headers[key])); } string policyString = null; int position = request.Key.LastIndexOf('/'); if (position == -1) { policyString = "{\"expiration\": \"" + AWSSDKUtils.CorrectedUtcNow.AddHours(24).ToString("yyyy-MM-ddTHH:mm:ssZ") + "\",\"conditions\": [{\"bucket\": \"" + request.Bucket + "\"},[\"starts-with\", \"$key\", \"" + "\"],{\"acl\": \"" + request.CannedACL.Value + "\"},[\"eq\", \"$Content-Type\", " + "\"" + request.Headers.ContentType + "\"" + "]" + metadataPolicy.ToString() + headersPolicy.ToString() + "]}"; } else { policyString = "{\"expiration\": \"" + AWSSDKUtils.CorrectedUtcNow.AddHours(24).ToString("yyyy-MM-ddTHH:mm:ssZ") + "\",\"conditions\": [{\"bucket\": \"" + request.Bucket + "\"},[\"starts-with\", \"$key\", \"" + request.Key.Substring(0, position) + "/\"],{\"acl\": \"" + request.CannedACL.Value + "\"},[\"eq\", \"$Content-Type\", " + "\"" + request.Headers.ContentType + "\"" + "]" + metadataPolicy.ToString() + headersPolicy.ToString() + "]}"; } if (Config.SignatureVersion == "2") { request.SignedPolicy = S3PostUploadSignedPolicy.GetSignedPolicy(policyString, base.Credentials); } else { request.SignedPolicy = S3PostUploadSignedPolicy.GetSignedPolicyV4(policyString, base.Credentials, request.Region); } }
// ReSharper disable once InconsistentNaming async Task UploadToAWS(AWSUploadPolicy uploadRequest, FileStream inputStream) { var s3PostUploadSignedPolicy = S3PostUploadSignedPolicy.GetSignedPolicyFromJson(uploadRequest.EncryptedPolicy); s3PostUploadSignedPolicy.SecurityToken = uploadRequest.SecurityToken; var uploadResponse = await Task.Run(() => AmazonS3Util.PostUpload(new S3PostUploadRequest { Key = uploadRequest.Key, Bucket = uploadRequest.BucketName, CannedACL = uploadRequest.ACL, ContentType = uploadRequest.ContentType, SuccessActionRedirect = uploadRequest.CallbackUrl, InputStream = inputStream, SignedPolicy = s3PostUploadSignedPolicy })).ConfigureAwait(false); if (uploadResponse.StatusCode != HttpStatusCode.OK) { throw new Exception("Amazon upload failed: " + uploadResponse.StatusCode); } }
private void CreateSignedPolicy(PostObjectRequest request) { if (request.ContentType == null) { int pos = request.Key.LastIndexOf('.'); string ext = null; if (pos != -1) { ext = request.Key.Substring(pos, request.Key.Length - pos); request.ContentType = AmazonS3Util.MimeTypeFromExtension(ext); } else { request.ContentType = "application/octet-stream"; } } StringBuilder metadataPolicy = new StringBuilder(); foreach (var kvp in request.Metadata) { var metakey = kvp.Key.StartsWith(S3Constants.PostFormDataXAmzPrefix, StringComparison.Ordinal) ? kvp.Key : S3Constants.PostFormDataMetaPrefix + kvp.Key; metadataPolicy.Append(string.Format(",{{\"{0}\": \"{1}\"}}", metakey, kvp.Value)); } string policyString = null; int position = request.Key.LastIndexOf('/'); if (position == -1) { policyString = "{\"expiration\": \"" + DateTime.UtcNow.AddHours(24).ToString("yyyy-MM-ddTHH:mm:ssZ") + "\",\"conditions\": [{\"bucket\": \"" + request.Bucket + "\"},[\"starts-with\", \"$key\", \"" + "\"],{\"acl\": \"" + request.CannedACL.Value + "\"},[\"eq\", \"$Content-Type\", " + "\"" + request.ContentType + "\"" + "]" + metadataPolicy.ToString() + "]}"; } else { policyString = "{\"expiration\": \"" + DateTime.UtcNow.AddHours(24).ToString("yyyy-MM-ddTHH:mm:ssZ") + "\",\"conditions\": [{\"bucket\": \"" + request.Bucket + "\"},[\"starts-with\", \"$key\", \"" + request.Key.Substring(0, position) + "/\"],{\"acl\": \"" + request.CannedACL.Value + "\"},[\"eq\", \"$Content-Type\", " + "\"" + request.ContentType + "\"" + "]" + metadataPolicy.ToString() + "]}"; } request.SignedPolicy = S3PostUploadSignedPolicy.GetSignedPolicy(policyString, base.Credentials); }
private S3PostUploadResponse testPost(string key, string bucketName, Stream contentStream, string extraConditions, AWSCredentials credentials, RegionEndpoint region) { var expDate = DateTime.UtcNow.AddMinutes(5).ToString(AWSSDKUtils.ISO8601DateFormat, CultureInfo.InvariantCulture); var policy = policy_tmpl.Replace("EXPIRATIONDATE", expDate) .Replace("BUCKETNAME", bucketName) .Replace("MOARCONDITIONS", extraConditions); var signedPolicy = S3PostUploadSignedPolicy.GetSignedPolicy(policy, credentials); var req = new S3PostUploadRequest { Key = key, Bucket = bucketName, CannedACL = S3CannedACL.PublicRead, InputStream = contentStream, SignedPolicy = signedPolicy, Region = region }; return(AmazonS3Util.PostUpload(req)); }