Creating expiring links to S3 or Cloudfront hosted content with ASP .Net
Anyone have an example of creating a signed URL with an expiration using ASP .Net? I'm exploring using LitS3 or ThreeSharp in my project, and have not seen any specific methods to do this in either of thos开发者_开发知识库e projects. Thanks.
Here's what worked for me with the AWS SDK and MVC 3 (based on the answers above and what I found on http://www.ec2studio.com/articles/s3.html):
public ActionResult GetS3Object(string bucket, string key)
{
string accessKeyID = ConfigurationManager.AppSettings["AWSAccessKey"];
string secretAccessKeyID = ConfigurationManager.AppSettings["AWSSecretKey"];
using (AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKeyID, secretAccessKeyID))
{
GetPreSignedUrlRequest request = new GetPreSignedUrlRequest()
.WithBucketName(bucket)
.WithKey(key)
.WithExpires(DateTime.Now.Add(new TimeSpan(7, 0, 0, 0)));
return Redirect(client.GetPreSignedURL(request));
}
}
using the amazon .net SDK you can get preSignedUrl
using (AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client("your access key ID", "you secret key"))
{
GetPreSignedUrlRequest getPreSignedUrl = new GetPreSignedUrlRequest().WithBucketName(bucketName);
getPreSignedUrl.Key = key;
getPreSignedUrl.Expires = DateTime.Now.AddSeconds(60);
}
Found this (mentioned in this thread in the AWS discussion forums) class library for generating signed URLs in Amazon S3. If anyone has any additional suggestions/methods to try, let me know.
Edit: ThreeSharp has the functionality I was looking for. From the ThreeSharpConsoleSample app:
using (UrlGetRequest request = new UrlGetRequest("mytestbucket", "mytestfile.txt"))
{
request.ExpiresIn = 60 * 10000;
using (UrlGetResponse response = service.UrlGet(request))
{
Console.WriteLine("Try this url in your web browser (it will only work for 60 seconds)\n");
string url = response.StreamResponseToString();
Console.WriteLine(url);
}
}
Console.WriteLine("\npress enter >");
Console.ReadLine();
精彩评论