public async Task can_get_correct_error_s3(string region1, string region2) { var bucketName = $"testing-{Guid.NewGuid()}"; var key = Guid.NewGuid().ToString(); using (var clientRegion1 = new RavenAwsS3Client(AwsAccessKey, AwsSecretKey, region1, bucketName)) using (var clientRegion2 = new RavenAwsS3Client(AwsAccessKey, AwsSecretKey, region2, bucketName)) { // make sure that the bucket doesn't exist await clientRegion1.DeleteBucket(); try { var sb = new StringBuilder(); for (var i = 0; i < 1 * 1024 * 1024; i++) { sb.Append("a"); } var error1 = await Assert.ThrowsAsync <BucketNotFoundException>(async() => { using (var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(sb.ToString()))) { await clientRegion1.PutObject(key, memoryStream, new Dictionary <string, string>()); } }); Assert.Equal($"Bucket name '{bucketName}' doesn't exist!", error1.Message); await clientRegion1.PutBucket(); var error2 = await Assert.ThrowsAsync <InvalidOperationException>(async() => { using (var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(sb.ToString()))) { await clientRegion2.PutObject(key, memoryStream, new Dictionary <string, string>()); } }); Assert.Equal($"AWS location as set to {region2}, but the bucket named: '{bucketName}' is located in: {region1}", error2.Message); } finally { await clientRegion1.DeleteBucket(); } } }
public async Task put_object(string region) { var bucketName = $"testing-{Guid.NewGuid()}"; var key = $"test-key-{Guid.NewGuid()}"; using (var client = new RavenAwsS3Client(AwsAccessKey, AwsSecretKey, region, bucketName)) { // make sure that the bucket doesn't exist await client.DeleteBucket(); try { await client.PutBucket(); var value1 = Guid.NewGuid().ToString(); var value2 = Guid.NewGuid().ToString(); await client.PutObject(key, new MemoryStream(Encoding.UTF8.GetBytes("231")), new Dictionary <string, string> { { "property1", value1 }, { "property2", value2 } }); // can't delete a bucket with existing objects var e = await Assert.ThrowsAsync <StorageException>(async() => await client.DeleteBucket()); Assert.True(e.Message.Contains("The bucket you tried to delete is not empty")); var @object = await client.GetObject(key); Assert.NotNull(@object); using (var reader = new StreamReader(@object.Data)) Assert.Equal("231", 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]); } finally { await client.DeleteObject(key); await client.DeleteBucket(); } } }
// ReSharper disable once InconsistentNaming private static async Task PutObject(string region, int sizeInMB, bool testBlobKeyAsFolder, UploadType uploadType) { var bucketName = $"testing-{Guid.NewGuid()}"; var key = testBlobKeyAsFolder == false? Guid.NewGuid().ToString() : $"{Guid.NewGuid()}/folder/testKey"; var uploadProgress = new UploadProgress(); var maxUploadPutObjectInBytesSetter = ExpressionHelper.CreateFieldSetter <RavenAwsS3Client, int>("MaxUploadPutObjectSizeInBytes"); var minOnePartUploadSizeLimitInBytesSetter = ExpressionHelper.CreateFieldSetter <RavenAwsS3Client, int>("MinOnePartUploadSizeLimitInBytes"); using (var client = new RavenAwsS3Client(AwsAccessKey, AwsSecretKey, region, bucketName, uploadProgress)) { maxUploadPutObjectInBytesSetter(client, 10 * 1024 * 1024); // 10MB minOnePartUploadSizeLimitInBytesSetter(client, 7 * 1024 * 1024); // 7MB // make sure that the bucket doesn't exist await client.DeleteBucket(); try { await client.PutBucket(); var value1 = Guid.NewGuid().ToString(); var value2 = Guid.NewGuid().ToString(); var sb = new StringBuilder(); for (var i = 0; i < sizeInMB * 1024 * 1024; i++) { sb.Append("a"); } long streamLength; using (var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(sb.ToString()))) { streamLength = memoryStream.Length; await client.PutObject(key, memoryStream, new Dictionary <string, string> { { "property1", value1 }, { "property2", value2 } }); } var @object = await client.GetObject(key); Assert.NotNull(@object); using (var reader = new StreamReader(@object.Data)) Assert.Equal(sb.ToString(), 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]); Assert.Equal(UploadState.Done, uploadProgress.UploadState); Assert.Equal(uploadType, uploadProgress.UploadType); Assert.Equal(streamLength, uploadProgress.TotalInBytes); Assert.Equal(streamLength, uploadProgress.UploadedInBytes); } finally { await client.DeleteObject(key); await client.DeleteBucket(); } } }