/*******************************************************************************
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
* this file except in compliance with the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and
* limitations under the License.
* *****************************************************************************
* __ _ _ ___
* ( )( \/\/ )/ __)
* /__\ \ / \__ \
* (_)(_) \/\/ (___/
*
* AWS SDK for .NET
* API Version: 2010-11-15
*/
using System;
using System.Security;
using System.Text;
using Amazon.Runtime;
using Amazon.Util;
namespace Amazon.EC2.Util
{
///
/// This class represents S3 upload policy. Policy string
/// representaion and signature to be used within EC2 bundling API.
///
public class S3UploadPolicy
{
private string policySignature;
private string policyString;
private string awsSecretAccessKey;
///
/// S3 Upload policy to be used by EC2 API.
///
/// Secret Key of the signer of the policy
/// Bucket name to upload
/// Prefix for the object keys
/// Expire, minutes from now
public S3UploadPolicy(
string awsSecretAccessKey,
string bucketName,
string prefix,
int expireInMinutes)
{
string policy = BuildPolicyString(bucketName, prefix, expireInMinutes);
this.policyString = Convert.ToBase64String(Encoding.UTF8.GetBytes(policy.ToCharArray()));
this.awsSecretAccessKey = awsSecretAccessKey;
}
/*
* Builds the policy string based on the input parameters
*/
private static string BuildPolicyString(
string bucketName,
string prefix,
int expireInMinutes)
{
StringBuilder policy = new StringBuilder("{", 512);
policy.Append("\"expiration\": \"");
policy.Append(AWSSDKUtils.GetFormattedTimestampISO8601(expireInMinutes));
policy.Append("\",");
policy.Append("\"conditions\": [");
policy.Append("{\"bucket\": \"");
policy.Append(bucketName);
policy.Append("\"},");
policy.Append("{\"acl\": \"");
policy.Append("ec2-bundle-read");
policy.Append("\"},");
policy.Append("[\"starts-with\", \"$key\", \"");
policy.Append(prefix);
policy.Append("\"]");
policy.Append("]}");
return policy.ToString();
}
///
/// Base64 representation of the serialized policy.
/// Use policy generated by this method
/// for passing to EC2 bunding calls.
///
/// Base64 policy
public string PolicyString
{
get
{
return this.policyString;
}
}
///
/// Policy signature in base64 format
/// Use signature generated by this method
/// for passing to EC2 bunding calls along with policy.
///
/// Base64 signature
public string PolicySignature
{
get
{
if (this.policySignature == null)
{
this.policySignature = CryptoUtilFactory.CryptoInstance.HMACSign(
policyString,
awsSecretAccessKey,
SigningAlgorithm.HmacSHA1
);
}
return this.policySignature;
}
}
}
}