Implements some very commonly used AWS S3 functionality. (need to merge with my other AWS wrapper classes, Route53, EC2, etc)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Collections.Specialized;
using System.Configuration;
//uses AWSSDK.dll from amazon
using Amazon;
using Amazon.S3;
using Amazon.S3.Model;
using System.Xml.Linq;
using System.Xml;
using System.Data;
namespace AIS.Common
{
public static class AWSHelper
{
private static List<S3Bucket> LoadS3Buckets()
{
System.Collections.Specialized.NameValueCollection appConfig = System.Configuration.ConfigurationManager.AppSettings;
using (var s3client = Amazon.AWSClientFactory.CreateAmazonS3Client(ConfigValues.AWSAccessKey, ConfigValues.AWSSecretKey))
{
return s3client.ListBuckets().Buckets;
}
}
private static List<S3Object> LoadS3Objects(string bucketname)
{
System.Collections.Specialized.NameValueCollection appConfig = System.Configuration.ConfigurationManager.AppSettings;
using (var s3client = Amazon.AWSClientFactory.CreateAmazonS3Client(ConfigValues.AWSAccessKey, ConfigValues.AWSSecretKey))
{
return s3client.ListObjects(new ListObjectsRequest() { BucketName = bucketname }).S3Objects;
}
}
private static void LoadS3File(string bucketname, string keyname, HttpResponse response, string contenttype)
{
NameValueCollection appConfig = ConfigurationManager.AppSettings;
using (var s3client = Amazon.AWSClientFactory.CreateAmazonS3Client(ConfigValues.AWSAccessKey, ConfigValues.AWSSecretKey))
{
GetObjectRequest s3request = new GetObjectRequest()
.WithBucketName(bucketname).WithKey(keyname);
using (GetObjectResponse s3response = s3client.GetObject(s3request))
{
string title = s3response.Metadata["x-amz-meta-title"];
response.Clear();
//Response.Write(string.Format("The object's title is {0}", title));
//Response.AddHeader
//Response.ContentType="application/swf";
////Response.ContentType="contenttype";
response.ContentType = s3response.ContentType; //s3response.Headers["Content-Length"];
long filesize = s3response.ContentLength;
byte[] buffer = new byte[(int)filesize];
response.BinaryWrite(ConvertStreamToBytes(s3response.ResponseStream, filesize));
response.Flush();
response.Close();
}
}
}
public static string GetS3UrlToVideo(string bucketname, string keyname)
{
System.Collections.Specialized.NameValueCollection appConfig = System.Configuration.ConfigurationManager.AppSettings;
string url = "";
using (var s3client = Amazon.AWSClientFactory.CreateAmazonS3Client(ConfigValues.AWSAccessKey, ConfigValues.AWSSecretKey))
{
Amazon.S3.Model.GetPreSignedUrlRequest request = new Amazon.S3.Model.GetPreSignedUrlRequest()
.WithBucketName(bucketname)
.WithKey(keyname)
.WithProtocol(Amazon.S3.Model.Protocol.HTTP)
.WithVerb(HttpVerb.GET)
.WithExpires(DateTime.Now.AddMinutes(ConfigValues.VideoURLExpiration));
Amazon.S3.Model.GetPreSignedUrlRequest r = new GetPreSignedUrlRequest();
url = s3client.GetPreSignedURL(request);
url= "https://s3.amazonaws.com/" + bucketname + keyname;
}
//return System.Xml.XmlConvert.EncodeName(url);
return url;
}
public static byte[] ConvertStreamToBytes(Stream input, long filesize)
{
byte[] buffer = new byte[(int)filesize];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
}
}
Leave a comment