Esempio n. 1
0
		public void PutObject()
		{
			var bucketName = "ravendb";
			var key = "testKey";

			using (var client = new RavenAwsS3Client("<aws_access_key>", "<aws_secret_key>", "<aws_region_for_bucket>"))
			{
				client.PutObject(bucketName, key, new MemoryStream(Encoding.UTF8.GetBytes("321")), new Dictionary<string, string>
																										{
																											{ "property1", "value1" }, 
																											{ "property2", "value2" }
																										}, 60 * 60);
				var @object = client.GetObject(bucketName, key);
				Assert.NotNull(@object);

				using (var reader = new StreamReader(@object.Data))
					Assert.Equal("321", reader.ReadToEnd());

				var property1 = @object.Metadata.Keys.Single(x => x.Contains("property1"));
				var property2 = @object.Metadata.Keys.Single(x => x.Contains("property2"));

				Assert.Equal("value1", @object.Metadata[property1]);
				Assert.Equal("value2", @object.Metadata[property2]);
			}
		}
		private void UploadToS3(string backupPath, PeriodicExportSetup localExportConfigs, bool isFullBackup)
		{
			if (awsAccessKey == Constants.DataCouldNotBeDecrypted ||
				awsSecretKey == Constants.DataCouldNotBeDecrypted)
			{
				throw new InvalidOperationException("Could not decrypt the AWS access settings, if you are running on IIS, make sure that load user profile is set to true.");
			}
			using (var client = new RavenAwsS3Client(awsAccessKey, awsSecretKey, localExportConfigs.AwsRegionEndpoint ?? RavenAwsClient.DefaultRegion))
			using (var fileStream = File.OpenRead(backupPath))
			{
				var key = Path.GetFileName(backupPath);
				client.PutObject(localExportConfigs.S3BucketName, key, fileStream, new Dictionary<string, string>
			                                                                       {
				                                                                       { "Description", GetArchiveDescription(isFullBackup) }
			                                                                       }, 60 * 60);

				logger.Info(string.Format("Successfully uploaded backup {0} to S3 bucket {1}, with key {2}",
											  Path.GetFileName(backupPath), localExportConfigs.S3BucketName, key));
			}
		}
Esempio n. 3
0
		public void AuthorizationHeaderValueForAwsS3ShouldBeCalculatedCorrectly1()
		{
			using (var client = new RavenAwsS3Client("AKIAIOSFODNN7EXAMPLE", "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", "us-east-1"))
			{
				var date = new DateTime(2013, 5, 24);

				var stream = new MemoryStream(Encoding.UTF8.GetBytes("Welcome to Amazon S3."));
				var payloadHash = RavenAwsHelper.CalculatePayloadHash(stream);

				Assert.Equal("44ce7dd67c959e0d3524ffac1771dfbba87d2b6b4b4e99e42034a8b803f8b072", payloadHash);

				var url = client.GetUrl("examplebucket") + "/" + "test%24file.text";
				var headers = new Dictionary<string, string>
				              {
					              { "x-amz-date", RavenAwsHelper.ConvertToString(date) }, 
								  { "x-amz-content-sha256", payloadHash }, 
								  { "x-amz-storage-class", "REDUCED_REDUNDANCY" },
								  { "Date", date.ToString("R") },
								  { "Host", "examplebucket.s3.amazonaws.com" }
				              };

				var auth = client.CalculateAuthorizationHeaderValue("PUT", url, date, headers);

				Assert.Equal("AWS4-HMAC-SHA256", auth.Scheme);
				Assert.Equal("Credential=AKIAIOSFODNN7EXAMPLE/20130524/us-east-1/s3/aws4_request,SignedHeaders=date;host;x-amz-content-sha256;x-amz-date;x-amz-storage-class,Signature=98ad721746da40c64f1a55b78f14c238d841ea1380cd77a1b5971af0ece108bd", auth.Parameter);
			}
		}
Esempio n. 4
0
		public void AuthorizationHeaderValueForAwsS3ShouldBeCalculatedCorrectly2()
		{
			using (var client = new RavenAwsS3Client("AKIAIOSFODNN7EXAMPLE", "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", "us-east-1"))
			{
				var date = new DateTime(2013, 5, 24);
				var payloadHash = RavenAwsHelper.CalculatePayloadHash(null);

				Assert.Equal("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", payloadHash);

				var url = client.GetUrl("examplebucket") + "/" + "test.txt";
				var headers = new Dictionary<string, string>
				              {
					              { "x-amz-date", RavenAwsHelper.ConvertToString(date) }, 
								  { "x-amz-content-sha256", payloadHash }, 
								  { "Date", date.ToString("R") },
								  { "Host", "examplebucket.s3.amazonaws.com" },
								  { "Range", "bytes=0-9"}
				              };

				var auth = client.CalculateAuthorizationHeaderValue("GET", url, date, headers);

				Assert.Equal("AWS4-HMAC-SHA256", auth.Scheme);
				Assert.Equal("Credential=AKIAIOSFODNN7EXAMPLE/20130524/us-east-1/s3/aws4_request,SignedHeaders=host;range;x-amz-content-sha256;x-amz-date,Signature=f0e8bdb87c964420e857bd35b5d6ed310bd44f0170aba48dd91039c6036bdb41", auth.Parameter);
			}
		}
        private void UploadToS3(string backupPath, PeriodicExportSetup localExportConfigs, bool isFullBackup)
		{
	        using (var client = new RavenAwsS3Client(awsAccessKey, awsSecretKey, localExportConfigs.AwsRegionEndpoint ?? RavenAwsClient.DefaultRegion))
		    using (var fileStream = File.OpenRead(backupPath))
		    {
			    var key = Path.GetFileName(backupPath);
			    client.PutObject(localExportConfigs.S3BucketName, key, fileStream, new Dictionary<string, string>
			                                                                       {
				                                                                       { "Description", GetArchiveDescription(isFullBackup) }
			                                                                       }, 60 * 60);

				logger.Info(string.Format("Successfully uploaded backup {0} to S3 bucket {1}, with key {2}",
											  Path.GetFileName(backupPath), localExportConfigs.S3BucketName, key));
		    }
		}