using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using com.amazonaws.s3;
public partial class _Default : System.Web.UI.Page
{
public string myAWSAccessKeyId = "[YOUR ID HERE]";
public string mySecretAccessKeyId = "[YOUR SECRET ID HERE]";
protected void cmdSubmit_Click(object sender, EventArgs e)
{
if (myFileUpload.HasFile)
{
MyPrint("Server received " + myFileUpload.FileName);
MyPrint("Attempting to save to S3");
AmazonS3 myS3 = new AmazonS3();
DateTime myTime = DateTime.Now;
// Create a signature for this operation
string strMySignature = S3Helper.GetSignature(
mySecretAccessKeyId,
"PutObjectInline",
myTime);
// Create a new Access grant for anonymous users.
Grant myGrant = new Grant();
Grant[] myGrants = new Grant[1];
// Setup Access control, allow Read access to all
Group myGroup = new Group();
myGroup.URI = "http://acs.amazonaws.com/groups/global/AllUsers";
myGrant.Grantee = myGroup;
myGrant.Permission = Permission.READ;
myGrants[0] = myGrant;
// Setup some metadata to indicate the content type
MetadataEntry myContentType = new MetadataEntry();
myContentType.Name = "ContentType";
myContentType.Value = myFileUpload.PostedFile.ContentType;
MetadataEntry[] myMetaData = new MetadataEntry[1];
myMetaData[0] = myContentType;
// Finally upload the object
PutObjectResult myResult = myS3.PutObjectInline(
txtBucketName.Text,
txtKey.Text,
myMetaData,
myFileUpload.FileBytes,
myFileUpload.FileBytes.Length,
myGrants,
StorageClass.STANDARD,
true,
myAWSAccessKeyId,
S3Helper.GetTimeStamp(myTime),
true,
strMySignature, null
);
// Print out the results.
if (myResult != null) MyPrint("ETag: " + myResult.ETag);
}
}
protected void cmdShowTime_Click(object sender, EventArgs e)
{
DateTime myTime = DateTime.Now;
MyPrint("Current TimeStamp = " + S3Helper.FormatTimeStamp(myTime));
}
protected void cmdShowSignature_Click(object sender, EventArgs e)
{
DateTime myTime = DateTime.Now;
MyPrint("Signature Digest = " + S3Helper.GetSignature(this.mySecretAccessKeyId, "ListAllMyBuckets", myTime));
}
protected void cmdListBuckets_Click(object sender, EventArgs e)
{
AmazonS3 myS3 = new AmazonS3();
DateTime myTime = DateTime.Now;
/// Lists all buckets under this user
ListAllMyBucketsResult myBuckets = myS3.ListAllMyBuckets(myAWSAccessKeyId,
S3Helper.GetTimeStamp(myTime),
true,
S3Helper.GetSignature(mySecretAccessKeyId, "ListAllMyBuckets", myTime));
lblBuckets.Text = "My Buckets
";
foreach (ListAllMyBucketsEntry b in myBuckets.Buckets)
{
lblBuckets.Text += (b.Name + ", created " + b.CreationDate + "
");
}
}
protected void cmdCreateBucket_Click(object sender, EventArgs e)
{
AmazonS3 myS3 = new AmazonS3();
DateTime myTime = DateTime.Now;
try
{
CreateBucketResult myCreateResult = myS3.CreateBucket(txtBucketName.Text, null,
myAWSAccessKeyId,
S3Helper.GetTimeStamp(myTime),
true,
S3Helper.GetSignature(mySecretAccessKeyId, "CreateBucket", myTime));
MyPrint("Bucket successfully created.");
}
catch (Exception ex)
{
MyPrint("CreateBucket Error: " + ex.Message);
}
}
void MyPrint(string strMsg)
{
lblResults.Text += strMsg + "
";
}
protected void cmdDeleteBucket_Click(object sender, EventArgs e)
{
AmazonS3 myS3 = new AmazonS3();
DateTime myTime = DateTime.Now;
try
{
Status myDeleteResult = myS3.DeleteBucket(txtBucketName.Text,
myAWSAccessKeyId,
S3Helper.GetTimeStamp(myTime),
true,
S3Helper.GetSignature(mySecretAccessKeyId, "DeleteBucket", myTime), null);
MyPrint("Bucket successfully deleted.");
}
catch (Exception ex)
{
MyPrint("DeleteBucket Error: " + ex.Message);
}
}
protected void cmdShowContents_Click(object sender, EventArgs e)
{
AmazonS3 myS3 = new AmazonS3();
DateTime myTime = DateTime.Now;
string strMySignature = S3Helper.GetSignature(
mySecretAccessKeyId,
"ListBucket",
myTime);
ListBucketResult myResults = myS3.ListBucket(
this.txtBucketName.Text,
"",
"",
0,
false,
"|",
myAWSAccessKeyId,
S3Helper.GetTimeStamp(myTime),
true,
strMySignature,
null);
// Iterate through the bucket contents
if (myResults.Contents != null)
{
lblResults.Text = "
";
lblResults.Text += " "; lblResults.Text += "" + myEntry.Key + ""; lblResults.Text += " |