Creazione di collegamenti in scadenza a contenuto ospitato su S3 o Cloudfront con ASP .Net
-
05-07-2019 - |
Domanda
Qualcuno ha un esempio di creazione di un URL firmato con scadenza con ASP .Net? Sto esplorando utilizzando LitS3 o ThreeSharp nel mio progetto e non ho visto alcun metodo specifico per farlo in nessuno di questi progetti. Grazie.
Soluzione
Ecco cosa ha funzionato per me con AWS SDK e MVC 3 (basato sulle risposte sopra e cosa ho trovato su 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));
}
}
Altri suggerimenti
utilizzando l'ammontare .kard SDK di Amazon puoi ottenere 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);
}
Trovato questo (menzionato in questo thread in la libreria di classi dei forum di discussione AWS) per la generazione di URL firmati in Amazon S3. Se qualcuno ha ulteriori suggerimenti / metodi da provare, fammi sapere.
Modifica: ThreeSharp ha la funzionalità che stavo cercando. Dall'app ThreeSharpConsoleSample:
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();