/// <summary> /// Determines whether an S3 bucket exists or not. /// This is done by: /// 1. Creating a PreSigned Url for the bucket. To work with Signature V4 only regions, as /// well as Signature V4-optional regions, we keep the expiry to within the maximum for V4 /// (which is one week). /// 2. Making a HEAD request to the Url /// </summary> /// <param name="bucketName">The name of the bucket to check.</param> /// <param name="s3Client">The Amazon S3 Client to use for S3 specific operations.</param> /// <returns></returns> public static bool DoesS3BucketExist(IAmazonS3 s3Client, string bucketName) { if (s3Client == null) { throw new ArgumentNullException("s3Client", "The s3Client cannot be null!"); } if (String.IsNullOrEmpty(bucketName)) { throw new ArgumentNullException("bucketName", "The bucketName cannot be null or the empty string!"); } var request = new GetPreSignedUrlRequest { BucketName = bucketName, Expires = DateTime.Now.AddDays(1), Verb = HttpVerb.HEAD, Protocol = Protocol.HTTP }; var url = s3Client.GetPreSignedURL(request); var uri = new Uri(url); var config = s3Client.Config; var response = AmazonS3HttpUtil.GetHead(s3Client, config, url, HeaderKeys.XAmzBucketRegion); if (response.StatusCode == null) { // there was a problem with the request and we weren't able // conclusively determine if the bucket exists return false; } else { AmazonS3Uri s3Uri; var mismatchDetected = AmazonS3Uri.TryParseAmazonS3Uri(uri, out s3Uri) && BucketRegionDetector.GetCorrectRegion(s3Uri, response.StatusCode.Value, response.HeaderValue) != null; var statusCodeAcceptable = response.StatusCode != HttpStatusCode.NotFound && response.StatusCode != HttpStatusCode.BadRequest; return statusCodeAcceptable || mismatchDetected; } }
public virtual string GeneratePreSignedUrl(AmazonS3Client s3Client, string bucketName, string key) { // Create the request var getPreSignedUrlRequest = new GetPreSignedUrlRequest { BucketName = bucketName, Key = key, Expires = DateTime.Now.AddHours(1.0) }; // Submit the request return s3Client.GetPreSignedURL(getPreSignedUrlRequest); }
static void GetPreSigUrl() { String fileKey = "test-txt.log"; Amazon.Runtime.BasicAWSCredentials cred = new Amazon.Runtime.BasicAWSCredentials("61140DEQBD8L2QSRATPS", "1oZedqSsM2DLjer2VvZ74ACpn998TamNbz4LEURN"); Amazon.RegionEndpoint endpoint = Amazon.RegionEndpoint.APNortheast1; //首先创建一个s3的客户端操作对象(需要amazon提供的密钥) AmazonS3Client s3 = new AmazonS3Client(cred, endpoint); try { // 验证名称为bucketName的bucket是否存在,不存在则创建 Amazon.S3.Model.GetPreSignedUrlRequest urlRequest = new Amazon.S3.Model.GetPreSignedUrlRequest(); urlRequest.BucketName = bucketName; urlRequest.Key = fileKey; urlRequest.Protocol = Protocol.HTTP; urlRequest.Verb = HttpVerb.GET; urlRequest.Expires = DateTime.Now.AddHours(12); // ;.Now + dtSpan; //new TimeSpan(12,0,0); //12小时有效期 String strSinnedURL = s3.GetPreSignedURL(urlRequest); Console.WriteLine(strSinnedURL); } catch (Amazon.S3.AmazonS3Exception e) { Console.WriteLine("GenerateFileSignURL AmazonS3Exception:" + e.ToString()); } catch (Exception e) { Console.WriteLine("GenerateFileSignURL Exception:" + e.ToString()); } }
public string GetSignedUrl(string bucket, string key, SignedUrlType type, int timeoutInMinutes, S3CannedACL acl = null) { this.Logger.LogDebug($"[{nameof(this.GetSignedUrl)}]"); this.Logger.LogTrace(JsonConvert.SerializeObject(new { bucket, key, type, timeoutInMinutes, acl })); if (string.IsNullOrWhiteSpace(bucket)) { throw new ArgumentNullException(nameof(bucket)); } if (string.IsNullOrWhiteSpace(key)) { throw new ArgumentNullException(nameof(key)); } var request = new Amazon.S3.Model.GetPreSignedUrlRequest { BucketName = bucket, Key = key, Expires = DateTime.UtcNow.AddMinutes(value: timeoutInMinutes), Verb = type == SignedUrlType.Download ? HttpVerb.GET : HttpVerb.PUT, }; this.Logger.LogTrace(JsonConvert.SerializeObject(value: request)); var response = this.Repository.GetPreSignedURL(request: request); this.Logger.LogTrace(JsonConvert.SerializeObject(value: response)); return(response); }
public BucketRegionTestRunner(bool useSigV4, bool useSigV4SetExplicitly = false) { originalUseSignatureVersion4 = AWSConfigsS3.UseSignatureVersion4; originalUseSigV4SetExplicitly = GetAWSConfigsS3InternalProperty(); AWSConfigsS3.UseSignatureVersion4 = useSigV4; SetAWSConfigsS3InternalProperty(useSigV4SetExplicitly); CreateAndCheckTestBucket(); if (TestBucketIsReady) { GetObjectMetadataRequest = new GetObjectMetadataRequest() { BucketName = TestBucket.BucketName, Key = TestObjectKey }; PutObjectRequest = new PutObjectRequest() { BucketName = TestBucket.BucketName, Key = TestObjectKey, ContentBody = TestContent }; PreSignedUrlRequest = new GetPreSignedUrlRequest { BucketName = BucketName, Key = BucketRegionTestRunner.TestObjectKey, Expires = DateTime.Now.AddHours(1) }; } }
public void SetHttpVerb() { var request = new GetPreSignedUrlRequest(); var values = new Dictionary<string, object> { { "Verb", "DELETE" } }; InternalSDKUtils.ApplyValues(request, values); Assert.AreEqual(request.Verb, HttpVerb.DELETE); }
/// <summary> /// Determines whether an S3 bucket exists or not. /// This is done by: /// 1. Creating a PreSigned Url for the bucket (with an expiry date at the end of this decade) /// 2. Making a HEAD request to the Url /// </summary> /// <param name="bucketName">The name of the bucket to check.</param> /// <param name="s3Client">The Amazon S3 Client to use for S3 specific operations.</param> /// <returns></returns> public static bool DoesS3BucketExist(IAmazonS3 s3Client, string bucketName) { if (s3Client == null) { throw new ArgumentNullException("s3Client", "The s3Client cannot be null!"); } if (String.IsNullOrEmpty(bucketName)) { throw new ArgumentNullException("bucketName", "The bucketName cannot be null or the empty string!"); } GetPreSignedUrlRequest request = new GetPreSignedUrlRequest(); request.BucketName = bucketName; if (AWSConfigs.S3Config.UseSignatureVersion4) request.Expires = DateTime.Now.AddDays(6); else request.Expires = new DateTime(2019, 12, 31); request.Verb = HttpVerb.HEAD; request.Protocol = Protocol.HTTP; string url = s3Client.GetPreSignedURL(request); Uri uri = new Uri(url); HttpWebRequest httpRequest = WebRequest.Create(uri) as HttpWebRequest; httpRequest.Method = "HEAD"; AmazonS3Client concreteClient = s3Client as AmazonS3Client; if (concreteClient != null) { concreteClient.ConfigureProxy(httpRequest); } try { using (HttpWebResponse httpResponse = httpRequest.GetResponse() as HttpWebResponse) { // If all went well, the bucket was found! return true; } } catch (WebException we) { using (HttpWebResponse errorResponse = we.Response as HttpWebResponse) { if (errorResponse != null) { HttpStatusCode code = errorResponse.StatusCode; return code != HttpStatusCode.NotFound && code != HttpStatusCode.BadRequest; } // The Error Response is null which is indicative of either // a bad request or some other problem return false; } } }
static void GetPreSigUrl1() { String fileKey = "test-txt.log"; AmazonS3Config config = new AmazonS3Config(); config.RegionEndpoint = null; config.ServiceURL = "http://obs.myhwclouds.com"; config.SignatureMethod = Amazon.Runtime.SigningAlgorithm.HmacSHA1; Amazon.AWSConfigsS3.UseSignatureVersion4 = false; config.ForcePathStyle = false; using (client = new AmazonS3Client("61140DEQBD8L2QSRATPS", "1oZedqSsM2DLjer2VvZ74ACpn998TamNbz4LEURN", config)) { Amazon.S3.Model.GetPreSignedUrlRequest urlRequest = new Amazon.S3.Model.GetPreSignedUrlRequest(); urlRequest.BucketName = bucketName; urlRequest.Key = fileKey; urlRequest.Protocol = Protocol.HTTP; urlRequest.Verb = HttpVerb.GET; urlRequest.Expires = DateTime.Now.AddHours(12); // ;.Now + dtSpan; //new TimeSpan(12,0,0); //12小时有效期 String strSinnedURL = client.GetPreSignedURL(urlRequest); Console.WriteLine(strSinnedURL); } //Amazon.Runtime.BasicAWSCredentials cred = new Amazon.Runtime.BasicAWSCredentials("61140DEQBD8L2QSRATPS", "1oZedqSsM2DLjer2VvZ74ACpn998TamNbz4LEURN"); //Amazon.RegionEndpoint endpoint = Amazon.RegionEndpoint.APNortheast1; ////首先创建一个s3的客户端操作对象(需要amazon提供的密钥) //AmazonS3Client s3 = new AmazonS3Client(cred, endpoint); //try //{ // // 验证名称为bucketName的bucket是否存在,不存在则创建 // Amazon.S3.Model.GetPreSignedUrlRequest urlRequest = new Amazon.S3.Model.GetPreSignedUrlRequest(); // urlRequest.BucketName = bucketName; // urlRequest.Key = fileKey; // urlRequest.Protocol = Protocol.HTTP; // urlRequest.Verb = HttpVerb.GET; // urlRequest.Expires = DateTime.Now.AddHours(12); // ;.Now + dtSpan; //new TimeSpan(12,0,0); //12小时有效期 // String strSinnedURL = s3.GetPreSignedURL(urlRequest); // Console.WriteLine(strSinnedURL); //} //catch (Amazon.S3.AmazonS3Exception e) //{ // Console.WriteLine("GenerateFileSignURL AmazonS3Exception:" + e.ToString()); //} //catch (Exception e) //{ // Console.WriteLine("GenerateFileSignURL Exception:" + e.ToString()); //} }
protected override void ProcessRecord() { AmazonS3 client = base.GetClient(); Amazon.S3.Model.GetPreSignedUrlRequest request = new Amazon.S3.Model.GetPreSignedUrlRequest(); request.BucketName = this._BucketName; request.Key = this._Key; request.Expires = this._Expires; request.VersionId = this._VersionId; string response = client.GetPreSignedURL(request); }
public static string GetPrivateImageJour(string filename) { string accessKeyID = Conf.AppSettings["AWSAccessKey"]; string secretAccessKeyID = Conf.AppSettings["AWSSecretKey"]; using (AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKeyID, secretAccessKeyID)) { GetPreSignedUrlRequest request = new GetPreSignedUrlRequest() .WithBucketName(Conf.AppSettings["bucketJour"]) .WithKey(filename) .WithExpires(DateTime.Now.Add(new TimeSpan(0, 24, 0, 0))); return client.GetPreSignedURL(request); } }
public static string GetPdfKotter(string klotterid) { string accessKeyID = Conf.AppSettings["AWSAccessKey"]; string secretAccessKeyID = Conf.AppSettings["AWSSecretKey"]; using (AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKeyID, secretAccessKeyID)) { GetPreSignedUrlRequest request = new GetPreSignedUrlRequest() .WithBucketName(Conf.AppSettings["bucketKlotter"]) .WithKey(klotterid + "/report.pdf") .WithExpires(DateTime.Now.Add(new TimeSpan(0, 24, 0, 0))); return client.GetPreSignedURL(request); } }
public static string GeneratePreSignedURL(string bucketName, string objectKey) { string urlString = ""; IAmazonS3 s3Client; using (s3Client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1)) { GetPreSignedUrlRequest request1 = new GetPreSignedUrlRequest { BucketName = bucketName, Key = objectKey, Expires = DateTime.Now.AddMinutes(5) }; try { urlString = s3Client.GetPreSignedURL(request1); //string url = s3Client.GetPreSignedURL(request1); } catch (AmazonS3Exception amazonS3Exception) { if (amazonS3Exception.ErrorCode != null && (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity"))) { Console.WriteLine("Check the provided AWS Credentials."); Console.WriteLine( "To sign up for service, go to http://aws.amazon.com/s3"); } else { Console.WriteLine( "Error occurred. Message:'{0}' when listing objects", amazonS3Exception.Message); } } catch (Exception e) { Console.WriteLine(e.Message); } } return urlString; }
private void DisplayImageList() { ListBucketsResponse response = S3Client.ListBuckets(); foreach (S3Bucket bucket in response.Buckets) { HtmlGenericControl header = new HtmlGenericControl("div") { InnerText = bucket.BucketName }; DivlistOfImages.Controls.Add(header); ListObjectsRequest request = new ListObjectsRequest { BucketName = bucket.BucketName }; ListObjectsResponse imageList = S3Client.ListObjects(request); foreach (S3Object s3Object in imageList.S3Objects) { HtmlAnchor imageLink = new HtmlAnchor { InnerText = s3Object.Key }; string bucketName = bucket.BucketName; string objectKey = s3Object.Key; GetPreSignedUrlRequest preSignedUrlRequest = new GetPreSignedUrlRequest { BucketName = bucketName, Key = objectKey, Protocol = Protocol.HTTP, Expires = DateTime.Now.AddDays(7) }; imageLink.HRef = S3Client.GetPreSignedURL(preSignedUrlRequest); DivlistOfImages.Controls.Add(imageLink); } } }
public string GeneratePreSignedURL(string key) { string urlString = ""; var request1 = new GetPreSignedUrlRequest { BucketName = AppConfigWebValues.Instance.S3BucketName, Key = key, Verb = HttpVerb.PUT, Expires = DateTime.Now.AddMinutes(5) }; try { urlString = _client.GetPreSignedURL(request1); } catch (AmazonS3Exception amazonS3Exception) { if (amazonS3Exception.ErrorCode != null && (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity"))) { Console.WriteLine("Check the provided AWS Credentials."); Console.WriteLine("To sign up for service, go to http://aws.amazon.com/s3"); } else { Console.WriteLine( "Error occurred. Message:'{0}' when listing objects", amazonS3Exception.Message); } } catch (Exception e) { Console.WriteLine(e.Message); } return urlString; }
// Assumption that we only need this when the source is S3. // Therefore use SourceAWS. public static string GeneratePreSignedUrl( string bucket, string key, int timeout=30 ) { // set for 5 hours... just incase. GetPreSignedUrlRequest request = new GetPreSignedUrlRequest() { BucketName = bucket, Key = key, Expires = DateTime.Now.AddMinutes( ConfigHelper.SharedAccessSignatureDurationInSeconds /60), Protocol = Protocol.HTTPS }; using (IAmazonS3 client = GenerateS3Client(ConfigHelper.SrcAWSAccessKeyID, ConfigHelper.SrcAWSSecretAccessKeyID, bucket)) { string url = client.GetPreSignedURL(request); return url; } }
/// <summary> /// Create a signed URL allowing access to a resource that would /// usually require authentication. /// </summary> /// <remarks> /// <para> /// When using query string authentication you create a query, /// specify an expiration time for the query, sign it with your /// signature, place the data in an HTTP request, and distribute /// the request to a user or embed the request in a web page. /// </para> /// <para> /// A PreSigned URL can be generated for GET, PUT, DELETE and HEAD /// operations on your bucketName, keys, and versions. /// </para> /// </remarks> /// <param name="request">The GetPreSignedUrlRequest that defines the /// parameters of the operation.</param> /// <returns>A string that is the signed http request.</returns> /// <exception cref="T:System.ArgumentException" /> /// <exception cref="T:System.ArgumentNullException" /> public string GetPreSignedURL(GetPreSignedUrlRequest request) { if (Credentials == null) throw new AmazonS3Exception("Credentials must be specified, cannot call method anonymously"); if (request == null) throw new ArgumentNullException("request", "The PreSignedUrlRequest specified is null!"); if (!request.IsSetExpires()) throw new InvalidOperationException("The Expires specified is null!"); var aws4Signing = AWSConfigs.S3Config.UseSignatureVersion4; var region = AWS4Signer.DetermineSigningRegion(Config, "s3", alternateEndpoint: null, request: null); if (aws4Signing && string.IsNullOrEmpty(region)) throw new InvalidOperationException("To use AWS4 signing, a region must be specified in the client configuration using the AuthenticationRegion or Region properties, or be determinable from the service URL."); RegionEndpoint endpoint = RegionEndpoint.GetBySystemName(region); if (endpoint.GetEndpointForService("s3").SignatureVersionOverride == "4") aws4Signing = true; var immutableCredentials = Credentials.GetCredentials(); var irequest = Marshall(request, immutableCredentials.AccessKey, immutableCredentials.Token, aws4Signing); irequest.Endpoint = EndpointResolver.DetermineEndpoint(this.Config, irequest); var context = new Amazon.Runtime.Internal.ExecutionContext(new Amazon.Runtime.Internal.RequestContext(true) { Request = irequest, ClientConfig = this.Config }, null); AmazonS3PostMarshallHandler handler = new AmazonS3PostMarshallHandler(); handler.ProcessRequestHandlers(context); var metrics = new RequestMetrics(); string authorization; if (aws4Signing) { var aws4Signer = new AWS4PreSignedUrlSigner(); var signingResult = aws4Signer.SignRequest(irequest, this.Config, metrics, immutableCredentials.AccessKey, immutableCredentials.SecretKey); authorization = "&" + signingResult.ForQueryParameters; } else { this.Signer.Sign(irequest, this.Config, metrics, immutableCredentials.AccessKey, immutableCredentials.SecretKey); authorization = irequest.Headers[HeaderKeys.AuthorizationHeader]; authorization = authorization.Substring(authorization.IndexOf(":", StringComparison.Ordinal) + 1); authorization = "&Signature=" + AmazonS3Util.UrlEncode(authorization, false); } Uri url = AmazonServiceClient.ComposeUrl(irequest); string result = url.AbsoluteUri + authorization; Protocol protocol = DetermineProtocol(); if (request.Protocol != protocol) { switch (protocol) { case Protocol.HTTP: result = result.Replace("http://", "https://"); break; case Protocol.HTTPS: result = result.Replace("https://", "http://"); break; } } return result; }
/// <summary> /// Marshalls the parameters for a presigned url for a preferred signing protocol. /// </summary> /// <param name="getPreSignedUrlRequest"></param> /// <param name="accessKey"></param> /// <param name="token"></param> /// <param name="aws4Signing"> /// True if AWS4 signing will be used; if the expiry period in the request exceeds the /// maximum allowed for AWS4 (one week), an ArgumentException is thrown. /// </param> /// <returns></returns> private static IRequest Marshall(GetPreSignedUrlRequest getPreSignedUrlRequest, string accessKey, string token, bool aws4Signing) { IRequest request = new DefaultRequest(getPreSignedUrlRequest, "AmazonS3"); request.HttpMethod = getPreSignedUrlRequest.Verb.ToString(); var headers = getPreSignedUrlRequest.Headers; foreach (var key in headers.Keys) request.Headers[key] = headers[key]; AmazonS3Util.SetMetadataHeaders(request, getPreSignedUrlRequest.Metadata); if (!string.IsNullOrEmpty(token)) request.Headers[HeaderKeys.XAmzSecurityTokenHeader] = token; if (getPreSignedUrlRequest.ServerSideEncryptionMethod != null && getPreSignedUrlRequest.ServerSideEncryptionMethod != ServerSideEncryptionMethod.None) request.Headers.Add(HeaderKeys.XAmzServerSideEncryptionHeader, S3Transforms.ToStringValue(getPreSignedUrlRequest.ServerSideEncryptionMethod)); if (getPreSignedUrlRequest.IsSetServerSideEncryptionCustomerMethod()) request.Headers.Add(HeaderKeys.XAmzSSECustomerAlgorithmHeader, getPreSignedUrlRequest.ServerSideEncryptionCustomerMethod); if (getPreSignedUrlRequest.IsSetServerSideEncryptionKeyManagementServiceKeyId()) request.Headers.Add(HeaderKeys.XAmzServerSideEncryptionAwsKmsKeyIdHeader, getPreSignedUrlRequest.ServerSideEncryptionKeyManagementServiceKeyId); var queryParameters = request.Parameters; var uriResourcePath = new StringBuilder("/"); if (!string.IsNullOrEmpty(getPreSignedUrlRequest.BucketName)) uriResourcePath.Append(S3Transforms.ToStringValue(getPreSignedUrlRequest.BucketName)); if (!string.IsNullOrEmpty(getPreSignedUrlRequest.Key)) { if (uriResourcePath.Length > 1) uriResourcePath.Append("/"); uriResourcePath.Append(S3Transforms.ToStringValue(getPreSignedUrlRequest.Key)); } var baselineTime = aws4Signing ? DateTime.UtcNow : new DateTime(1970, 1, 1); var expires = Convert.ToInt64((getPreSignedUrlRequest.Expires.ToUniversalTime() - baselineTime).TotalSeconds); if (aws4Signing && expires > AWS4PreSignedUrlSigner.MaxAWS4PreSignedUrlExpiry) { var msg = string.Format(CultureInfo.InvariantCulture, "The maximum expiry period for a presigned url using AWS4 signing is {0} seconds", AWS4PreSignedUrlSigner.MaxAWS4PreSignedUrlExpiry); throw new ArgumentException(msg); } queryParameters.Add(aws4Signing ? "X-Amz-Expires" : "Expires", expires.ToString(CultureInfo.InvariantCulture)); if (!string.IsNullOrEmpty(token)) queryParameters.Add("x-amz-security-token", token); if (!aws4Signing) queryParameters.Add("AWSAccessKeyId", accessKey); if (getPreSignedUrlRequest.IsSetVersionId()) request.AddSubResource("versionId", S3Transforms.ToStringValue(getPreSignedUrlRequest.VersionId)); var responseHeaderOverrides = getPreSignedUrlRequest.ResponseHeaderOverrides; if (!string.IsNullOrEmpty(responseHeaderOverrides.CacheControl)) queryParameters.Add("response-cache-control", responseHeaderOverrides.CacheControl); if (!string.IsNullOrEmpty(responseHeaderOverrides.ContentType)) queryParameters.Add("response-content-type", responseHeaderOverrides.ContentType); if (!string.IsNullOrEmpty(responseHeaderOverrides.ContentLanguage)) queryParameters.Add("response-content-language", responseHeaderOverrides.ContentLanguage); if (!string.IsNullOrEmpty(responseHeaderOverrides.Expires)) queryParameters.Add("response-expires", responseHeaderOverrides.Expires); if (!string.IsNullOrEmpty(responseHeaderOverrides.ContentDisposition)) queryParameters.Add("response-content-disposition", responseHeaderOverrides.ContentDisposition); if (!string.IsNullOrEmpty(responseHeaderOverrides.ContentEncoding)) queryParameters.Add("response-content-encoding", responseHeaderOverrides.ContentEncoding); request.ResourcePath = uriResourcePath.ToString(); request.UseQueryString = true; return request; }
public string RetrieveSecureFileUri(string bin, string fileName) { // Secure not supported by amazon GetPreSignedUrlRequest request = new GetPreSignedUrlRequest(); request.BucketName = bin; request.Key = fileName; request.ResponseHeaderOverrides.Expires = DateTime.Now.AddDays(1).ToUniversalTime().ToString(); request.ResponseHeaderOverrides.CacheControl = "private, max-age=864000;"; request.Expires = DateTime.Now.AddHours(1); return client.GetPreSignedURL(request); }
public override string RetrieveFileUri(string bin, string fileName, string contentType, string renderFileName) { GetPreSignedUrlRequest request = new GetPreSignedUrlRequest(); request.BucketName = bin; request.Key = fileName; request.ResponseHeaderOverrides.ContentType = contentType; request.ResponseHeaderOverrides.ContentDisposition = "inline; filename=" + renderFileName; request.ResponseHeaderOverrides.Expires = DateTime.Now.AddDays(1).ToUniversalTime().ToString(); request.ResponseHeaderOverrides.CacheControl = "private, max-age=864000;"; request.Expires = DateTime.Now.AddHours(1); return client.GetPreSignedURL(request); }
string ICoreAmazonS3.GeneratePreSignedURL(string bucketName, string objectKey, DateTime expiration, IDictionary<string, object> additionalProperties) { var request = new GetPreSignedUrlRequest { BucketName = bucketName, Key = objectKey, Expires = expiration }; InternalSDKUtils.ApplyValues(request, additionalProperties); return this.GetPreSignedURL(request); }
/// <summary> /// Create a signed URL allowing access to a resource that would usually require authentication. cts /// </summary> /// <param name="key">The key under which the Amazon S3 object is stored.</param> /// <param name="version">The identifier for the specific version of the object to be deleted, if required.</param> /// <param name="expires">The expiry date and time for the pre-signed url.</param> /// <param name="settings">The <see cref="S3Settings"/> required to download from Amazon S3.</param> public string GetPreSignedURL(string key, string version, DateTime expires, S3Settings settings) { AmazonS3Client client = this.GetClient(settings); GetPreSignedUrlRequest request = new GetPreSignedUrlRequest() { BucketName = settings.BucketName, Key = key, VersionId = version, Expires = expires }; _Log.Verbose("Get object {0} from bucket {1}...", key, settings.BucketName); try { return client.GetPreSignedURL(request); } catch { _Log.Verbose("The object {0} does not exist in bucket {1}...", key, settings.BucketName); return ""; } }
/// <summary> /// git the first item in the list, and return a signed url for it, /// which expires in 5 minutes. /// </summary> internal void SigningDemo() { var objectList = GetS3ObjectList(); var s3obj = objectList[0]; GetPreSignedUrlRequest urlRequest = new GetPreSignedUrlRequest() { BucketName = this._bucketName, Key = s3obj.Key, Expires = DateTime.Now.AddMinutes(5) }; string url = _client.GetPreSignedURL(urlRequest); Console.WriteLine(url); }
// ************************************** // GetContentMediaUrl // ************************************** public static string GetContentMediaUrl(ContentMedia contentMedia) { using (var awsclient = Amazon.AWSClientFactory.CreateAmazonS3Client( RemoteMediaConfiguration.AccessKeyID, RemoteMediaConfiguration.SecretAccessKeyID)) { var key = GetContentMediaKey(contentMedia); var request = new GetPreSignedUrlRequest() .WithProtocol(Amazon.S3.Model.Protocol.HTTP) .WithBucketName(RemoteMediaConfiguration.BucketName) .WithKey(key) .WithExpires(DateTime.Now.Date.AddDays(1)); return awsclient.GetPreSignedURL(request); } }
public static bool DoesS3BucketExist(string bucketName, AmazonS3 s3Client) { bool flag; if (s3Client == null) { throw new ArgumentNullException("s3Client", "The s3Client cannot be null!"); } if (string.IsNullOrEmpty(bucketName)) { throw new ArgumentNullException("bucketName", "The bucketName cannot be null or the empty string!"); } GetPreSignedUrlRequest request = new GetPreSignedUrlRequest(); request.BucketName = bucketName; request.Expires = new DateTime(0x7e3, 12, 0x1f); request.Verb = HttpVerb.HEAD; HttpWebRequest request2 = WebRequest.Create(s3Client.GetPreSignedURL(request)) as HttpWebRequest; request2.Method = "HEAD"; try { request2.GetResponse(); flag = true; } catch (WebException exception) { using (HttpWebResponse response = exception.Response as HttpWebResponse) { if (response != null) { HttpStatusCode statusCode = response.StatusCode; return ((statusCode != HttpStatusCode.NotFound) && (statusCode != HttpStatusCode.BadRequest)); } flag = false; } } return flag; }
public override Uri GetUri(string domain, string path, TimeSpan expire, IEnumerable<string> headers) { if (expire == TimeSpan.Zero || expire == TimeSpan.MinValue || expire == TimeSpan.MaxValue) { expire = GetExpire(domain); } if (expire == TimeSpan.Zero || expire == TimeSpan.MinValue || expire == TimeSpan.MaxValue) { return GetUriShared(domain, path); } var pUrlRequest = new GetPreSignedUrlRequest() .WithBucketName(_bucket) .WithExpires(DateTime.UtcNow.Add(expire)) .WithKey(MakePath(domain, path)) .WithProtocol(SecureHelper.IsSecure() ? Protocol.HTTPS : Protocol.HTTP) .WithVerb(HttpVerb.GET); if (headers != null && headers.Any()) { var headersOverrides = new ResponseHeaderOverrides(); foreach (var h in headers) { if (h.StartsWith("Content-Disposition")) headersOverrides.WithContentDisposition(h.Substring("Content-Disposition".Length + 1)); else if (h.StartsWith("Cache-Control")) headersOverrides.WithCacheControl(h.Substring("Cache-Control".Length + 1)); else if (h.StartsWith("Content-Encoding")) headersOverrides.WithContentEncoding(h.Substring("Content-Encoding".Length + 1)); else if (h.StartsWith("Content-Language")) headersOverrides.WithContentLanguage(h.Substring("Content-Language".Length + 1)); else if (h.StartsWith("Content-Type")) headersOverrides.WithContentType(h.Substring("Content-Type".Length + 1)); else if (h.StartsWith("Expires")) headersOverrides.WithExpires(h.Substring("Expires".Length + 1)); else throw new FormatException(string.Format("Invalid header: {0}", h)); } pUrlRequest.WithResponseHeaderOverrides(headersOverrides); } return MakeUri(GetClient().GetPreSignedURL(pUrlRequest)); }
public override string SavePrivate(string domain, string path, Stream stream, DateTime expires) { using (AmazonS3 client = GetClient()) { var request = new PutObjectRequest(); string objectKey = MakePath(domain, path); request.WithBucketName(_bucket) .WithKey(objectKey) .WithCannedACL(S3CannedACL.BucketOwnerFullControl) .WithContentType("application/octet-stream") .WithMetaData("private-expire", expires.ToFileTimeUtc().ToString()); var headers = new NameValueCollection(); headers.Add("Cache-Control", string.Format("public, maxage={0}", (int)TimeSpan.FromDays(5).TotalSeconds)); headers.Add("Etag", (DateTime.UtcNow.Ticks).ToString()); headers.Add("Last-Modified", DateTime.UtcNow.ToString("R")); headers.Add("Expires", DateTime.UtcNow.Add(TimeSpan.FromDays(5)).ToString("R")); headers.Add("Content-Disposition", "attachment"); request.AddHeaders(headers); request.WithInputStream(stream); client.PutObject(request); //Get presigned url GetPreSignedUrlRequest pUrlRequest = new GetPreSignedUrlRequest() .WithBucketName(_bucket) .WithExpires(expires) .WithKey(objectKey) .WithProtocol(Protocol.HTTP) .WithVerb(HttpVerb.GET); string url = client.GetPreSignedURL(pUrlRequest); //TODO: CNAME! return url; } }
public string GetSecuredUrl(Uri uri) { CheckUri(uri); try { using (var client = CreateAmazonS3Client()) { var absolutePath = HttpUtility.UrlDecode(uri.AbsolutePath); var key = absolutePath.TrimStart('/'); var request = new GetPreSignedUrlRequest(); request.WithBucketName(bucketName) .WithKey(key) .WithExpires(DateTime.UtcNow.Add(tokenExpiryTime)); var url = client.GetPreSignedURL(request); return url; } } catch (Exception e) { throw new StorageException(string.Format("Failed to get signed URL for file. Uri: {0}.", uri), e); } }
/// <summary> /// Determines whether an S3 bucket exists or not. /// This is done by: /// 1. Creating a PreSigned Url for the bucket. To work with Signature V4 only regions, as /// well as Signature V4-optional regions, we keep the expiry to within the maximum for V4 /// (which is one week). /// 2. Making a HEAD request to the Url /// </summary> /// <param name="bucketName">The name of the bucket to check.</param> /// <param name="s3Client">The Amazon S3 Client to use for S3 specific operations.</param> /// <returns></returns> public static async System.Threading.Tasks.Task<bool> DoesS3BucketExistAsync(IAmazonS3 s3Client, string bucketName) { if (s3Client == null) { throw new ArgumentNullException("s3Client", "The s3Client cannot be null!"); } if (String.IsNullOrEmpty(bucketName)) { throw new ArgumentNullException("bucketName", "The bucketName cannot be null or the empty string!"); } var request = new GetPreSignedUrlRequest { BucketName = bucketName, Expires = DateTime.Now.AddDays(1), Verb = HttpVerb.HEAD, Protocol = Protocol.HTTP }; var url = s3Client.GetPreSignedURL(request); var uri = new Uri(url); var httpRequest = WebRequest.Create(uri) as HttpWebRequest; httpRequest.Method = "HEAD"; var concreteClient = s3Client as AmazonS3Client; if (concreteClient != null) { concreteClient.ConfigureProxy(httpRequest); } try { #if PCL var result = httpRequest.BeginGetResponse(null, null); using (var httpResponse = httpRequest.EndGetResponse(result) as HttpWebResponse) #else using (var httpResponse = await httpRequest.GetResponseAsync().ConfigureAwait(false) as HttpWebResponse) #endif { // If all went well, the bucket was found! return true; } } catch (WebException we) { using (var errorResponse = we.Response as HttpWebResponse) { if (errorResponse != null) { var code = errorResponse.StatusCode; return code != HttpStatusCode.NotFound && code != HttpStatusCode.BadRequest; } // The Error Response is null which is indicative of either // a bad request or some other problem return false; } } }
/// <summary> /// Create a signed URL allowing access to a resource that would /// usually require authentication. /// </summary> /// <remarks> /// <para> /// When using query string authentication you create a query, /// specify an expiration time for the query, sign it with your /// signature, place the data in an HTTP request, and distribute /// the request to a user or embed the request in a web page. /// </para> /// <para> /// A PreSigned URL can be generated for GET, PUT, DELETE and HEAD /// operations on your bucketName, keys, and versions. /// </para> /// </remarks> /// <param name="request">The GetPreSignedUrlRequest that defines the /// parameters of the operation.</param> /// <returns>A string that is the signed http request.</returns> /// <exception cref="T:System.ArgumentException" /> /// <exception cref="T:System.ArgumentNullException" /> public string GetPreSignedURL(GetPreSignedUrlRequest request) { return GetPreSignedURLInternal(request); }
/// <summary> /// Provides a URL for accessing the S3 object managed by S3Link /// </summary> /// <param name="expiration">The time the link should become invalid</param> /// <returns>A URL directing to the S3 object</returns> public string GetPreSignedURL(DateTime expiration) { var request = new GetPreSignedUrlRequest { BucketName = this.linker.s3.bucket, Key = this.linker.s3.key, Expires = expiration }; return this.s3ClientCache.GetClient(this.RegionAsEndpoint).GetPreSignedURL(request); }
/// <summary> /// Method retrives URL to an object in Amazon S3 for the Bucket name provided by IAmazonConfigProvider and identified by /// the keyName parameter. The URL returned will expire in the time provided. Returns null if url is not found. /// </summary> public string GetPreSignedUrl(string keyName, DateTime expirationTime) { try { using (AmazonS3 s3 = AWSClientFactory.CreateAmazonS3Client(this.cloudServiceConfigProvider.AWSAccessKeyId, this.cloudServiceConfigProvider.AWSSecretKey)) { GetPreSignedUrlRequest preSignedUrlReq = new GetPreSignedUrlRequest() .WithBucketName(this.cloudServiceConfigProvider.AmazonBucket) .WithKey(keyName) .WithProtocol(Protocol.HTTPS) .WithVerb(HttpVerb.GET) .WithExpires(expirationTime); string url = s3.GetPreSignedURL(preSignedUrlReq); return url; } } catch (Exception ex) { throw new CloudServiceException(ex, "Unable to retrieve signed URL for {0}", keyName); } }
private static string GetFileUrl(string key, string bucket) { if (key == null) return ""; string AWSAccessKey = ConfigurationManager.AppSettings["AWSAccessKey"]; string AWSSecretKey = ConfigurationManager.AppSettings["AWSSecretKey"]; using (AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(AWSAccessKey, AWSSecretKey)) { GetPreSignedUrlRequest get = new GetPreSignedUrlRequest() .WithBucketName(bucket) .WithKey(key) .WithExpires(DateTime.Now.AddMinutes(30)); return client.GetPreSignedURL(get); } }
public object Execute(ExecutorContext context) { var cmdletContext = context as CmdletContext; // create request var request = new Amazon.S3.Model.GetPreSignedUrlRequest(); if (cmdletContext.BucketName != null) { request.BucketName = cmdletContext.BucketName; } if (cmdletContext.Key != null) { request.Key = cmdletContext.Key; } if (cmdletContext.ContentType != null) { request.ContentType = cmdletContext.ContentType; } if (cmdletContext.Expires != null) { request.Expires = cmdletContext.Expires.Value; } if (cmdletContext.Protocol != null) { request.Protocol = cmdletContext.Protocol.Value; } if (cmdletContext.Verb != null) { request.Verb = cmdletContext.Verb.Value; } if (cmdletContext.VersionId != null) { request.VersionId = cmdletContext.VersionId; } if (cmdletContext.ServerSideEncryptionMethod != null) { request.ServerSideEncryptionMethod = cmdletContext.ServerSideEncryptionMethod; } if (cmdletContext.ServerSideEncryptionKeyManagementServiceKeyId != null) { request.ServerSideEncryptionKeyManagementServiceKeyId = cmdletContext.ServerSideEncryptionKeyManagementServiceKeyId; } if (cmdletContext.ServerSideEncryptionCustomerMethod != null) { request.ServerSideEncryptionCustomerMethod = cmdletContext.ServerSideEncryptionCustomerMethod; } // populate ResponseHeaderOverrides bool requestResponseHeaderOverridesIsNull = true; request.ResponseHeaderOverrides = new Amazon.S3.Model.ResponseHeaderOverrides(); System.String requestResponseHeaderOverrides_responseHeaderOverrides_ContentType = null; if (cmdletContext.ResponseHeaderOverrides_ContentType != null) { requestResponseHeaderOverrides_responseHeaderOverrides_ContentType = cmdletContext.ResponseHeaderOverrides_ContentType; } if (requestResponseHeaderOverrides_responseHeaderOverrides_ContentType != null) { request.ResponseHeaderOverrides.ContentType = requestResponseHeaderOverrides_responseHeaderOverrides_ContentType; requestResponseHeaderOverridesIsNull = false; } System.String requestResponseHeaderOverrides_responseHeaderOverrides_ContentLanguage = null; if (cmdletContext.ResponseHeaderOverrides_ContentLanguage != null) { requestResponseHeaderOverrides_responseHeaderOverrides_ContentLanguage = cmdletContext.ResponseHeaderOverrides_ContentLanguage; } if (requestResponseHeaderOverrides_responseHeaderOverrides_ContentLanguage != null) { request.ResponseHeaderOverrides.ContentLanguage = requestResponseHeaderOverrides_responseHeaderOverrides_ContentLanguage; requestResponseHeaderOverridesIsNull = false; } System.String requestResponseHeaderOverrides_responseHeaderOverrides_Expire = null; if (cmdletContext.ResponseHeaderOverrides_Expires != null) { requestResponseHeaderOverrides_responseHeaderOverrides_Expire = cmdletContext.ResponseHeaderOverrides_Expires; } if (requestResponseHeaderOverrides_responseHeaderOverrides_Expire != null) { request.ResponseHeaderOverrides.Expires = requestResponseHeaderOverrides_responseHeaderOverrides_Expire; requestResponseHeaderOverridesIsNull = false; } System.String requestResponseHeaderOverrides_responseHeaderOverrides_CacheControl = null; if (cmdletContext.ResponseHeaderOverrides_CacheControl != null) { requestResponseHeaderOverrides_responseHeaderOverrides_CacheControl = cmdletContext.ResponseHeaderOverrides_CacheControl; } if (requestResponseHeaderOverrides_responseHeaderOverrides_CacheControl != null) { request.ResponseHeaderOverrides.CacheControl = requestResponseHeaderOverrides_responseHeaderOverrides_CacheControl; requestResponseHeaderOverridesIsNull = false; } System.String requestResponseHeaderOverrides_responseHeaderOverrides_ContentDisposition = null; if (cmdletContext.ResponseHeaderOverrides_ContentDisposition != null) { requestResponseHeaderOverrides_responseHeaderOverrides_ContentDisposition = cmdletContext.ResponseHeaderOverrides_ContentDisposition; } if (requestResponseHeaderOverrides_responseHeaderOverrides_ContentDisposition != null) { request.ResponseHeaderOverrides.ContentDisposition = requestResponseHeaderOverrides_responseHeaderOverrides_ContentDisposition; requestResponseHeaderOverridesIsNull = false; } System.String requestResponseHeaderOverrides_responseHeaderOverrides_ContentEncoding = null; if (cmdletContext.ResponseHeaderOverrides_ContentEncoding != null) { requestResponseHeaderOverrides_responseHeaderOverrides_ContentEncoding = cmdletContext.ResponseHeaderOverrides_ContentEncoding; } if (requestResponseHeaderOverrides_responseHeaderOverrides_ContentEncoding != null) { request.ResponseHeaderOverrides.ContentEncoding = requestResponseHeaderOverrides_responseHeaderOverrides_ContentEncoding; requestResponseHeaderOverridesIsNull = false; } // determine if request.ResponseHeaderOverrides should be set to null if (requestResponseHeaderOverridesIsNull) { request.ResponseHeaderOverrides = null; } if (cmdletContext.RequestorPays) { request.RequestPayer = RequestPayer.Requester; } CmdletOutput output; // issue call var client = Client ?? CreateClient(_CurrentCredentials, _RegionEndpoint); try { var response = CallAWSServiceOperation(client, request); object pipelineOutput = response; output = new CmdletOutput { PipelineOutput = pipelineOutput, ServiceResponse = response }; } catch (Exception e) { output = new CmdletOutput { ErrorResponse = e }; } return(output); }
public override UploadResult Upload(Stream stream, string fileName) { if (string.IsNullOrEmpty(s3Settings.AccessKeyID)) Errors.Add("'Access Key' must not be empty."); if (string.IsNullOrEmpty(s3Settings.SecretAccessKey)) Errors.Add("'Secret Access Key' must not be empty."); if (string.IsNullOrEmpty(s3Settings.Bucket)) Errors.Add("'Bucket' must not be empty."); if (GetCurrentRegion(s3Settings) == UnknownEndpoint) Errors.Add("Please select an endpoint."); if (IsError) { return null; } var region = GetCurrentRegion(s3Settings); var s3ClientConfig = new AmazonS3Config(); if (region.AmazonRegion == null) { s3ClientConfig.ServiceURL = URLHelpers.ForcePrefix(region.Hostname); } else { s3ClientConfig.RegionEndpoint = region.AmazonRegion; } using (var client = new AmazonS3Client(GetCurrentCredentials(), s3ClientConfig)) { var putRequest = new GetPreSignedUrlRequest { BucketName = s3Settings.Bucket, Key = GetObjectKey(fileName), Verb = HttpVerb.PUT, Expires = DateTime.UtcNow.AddMinutes(5), ContentType = Helpers.GetMimeType(fileName) }; var requestHeaders = new NameValueCollection(); requestHeaders["x-amz-acl"] = "public-read"; requestHeaders["x-amz-storage-class"] = GetObjectStorageClass(); putRequest.Headers["x-amz-acl"] = "public-read"; putRequest.Headers["x-amz-storage-class"] = GetObjectStorageClass(); var responseHeaders = SendRequestStreamGetHeaders(client.GetPreSignedURL(putRequest), stream, Helpers.GetMimeType(fileName), requestHeaders, method: HttpMethod.PUT); if (responseHeaders.Count == 0) { Errors.Add("Upload to Amazon S3 failed. Check your access credentials."); return null; } var eTag = responseHeaders.Get("ETag"); if (eTag == null) { Errors.Add("Upload to Amazon S3 failed."); return null; } if (GetMd5Hash(stream) == eTag.Replace("\"", "")) { return new UploadResult { IsSuccess = true, URL = GetObjectURL(putRequest.Key) }; } Errors.Add("Upload to Amazon S3 failed, uploaded data did not match."); return null; } }
private static System.String CallAWSServiceOperation(IAmazonS3 client, Amazon.S3.Model.GetPreSignedUrlRequest request) { return(client.GetPreSignedURL(request)); }