本文整理汇总了C#中AmazonS3.GetPreSignedURL方法的典型用法代码示例。如果您正苦于以下问题:C# AmazonS3.GetPreSignedURL方法的具体用法?C# AmazonS3.GetPreSignedURL怎么用?C# AmazonS3.GetPreSignedURL使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类AmazonS3
的用法示例。
在下文中一共展示了AmazonS3.GetPreSignedURL方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DoesS3BucketExist
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;
}
示例2: DoesS3BucketExist
/// <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(string bucketName, AmazonS3 s3Client)
{
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(2019, 12, 31);
request.Verb = HttpVerb.HEAD;
request.Protocol = Protocol.HTTP;
string url = s3Client.GetPreSignedURL(request);
HttpWebRequest httpRequest = WebRequest.Create(url) as HttpWebRequest;
httpRequest.Method = "HEAD";
try
{
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;
}
}
}
示例3: MakeUrl
public static String MakeUrl(AmazonS3 s3Client, string filekey)
{
string preSignedURL = s3Client.GetPreSignedURL(new GetPreSignedUrlRequest()
{
BucketName = BUCKET_NAME,
Key = filekey,
Expires = System.DateTime.Now.AddYears(10)
});
return preSignedURL;
}