[MultipleProviders(S3Provider.AmazonS3 | S3Provider.GoogleCloudStorage)] //BackBlaze does not support suspending versioning public async Task PutBucketVersioningRequest(S3Provider provider, string _, ISimpleClient client) { await CreateTempBucketAsync(provider, client, async tempBucket => { //Check if versioning is enabled (it shouldn't be) GetBucketVersioningResponse getResp = await client.GetBucketVersioningAsync(tempBucket); Assert.Equal(200, getResp.StatusCode); Assert.False(getResp.MfaDelete); if (provider != S3Provider.BackBlazeB2) //backblaze always return true for versioning { Assert.False(getResp.Status); } //Enable versioning PutBucketVersioningResponse putResp = await client.PutBucketVersioningAsync(tempBucket, true); Assert.Equal(200, putResp.StatusCode); //Check if versioning is enabled (it should be) GetBucketVersioningResponse getResp2 = await client.GetBucketVersioningAsync(tempBucket); Assert.Equal(200, getResp2.StatusCode); Assert.True(getResp2.Status); //Disable versioning PutBucketVersioningResponse putResp2 = await client.PutBucketVersioningAsync(tempBucket, false); Assert.Equal(200, putResp2.StatusCode); //Check if versioning is enabled (it shouldn't be) GetBucketVersioningResponse getResp3 = await client.GetBucketVersioningAsync(tempBucket); Assert.Equal(200, putResp2.StatusCode); Assert.False(getResp3.Status); }).ConfigureAwait(false); }
private static void UnmarshallResult(XmlUnmarshallerContext context, GetBucketVersioningResponse response) { int currentDepth = context.get_CurrentDepth(); int num = currentDepth + 1; if (context.get_IsStartOfDocument()) { num += 2; } while (context.Read()) { if (context.get_IsStartElement() || context.get_IsAttribute()) { if (context.TestExpression("Status", num)) { response.VersioningConfig.Status = StringUnmarshaller.GetInstance().Unmarshall(context); } else if (context.TestExpression("MfaDelete", num)) { response.VersioningConfig.EnableMfaDelete = string.Equals(StringUnmarshaller.GetInstance().Unmarshall(context), "Enabled"); } } else if (context.get_IsEndElement() && context.get_CurrentDepth() < currentDepth) { break; } } }
public async Task <string> RetrieveBucketVersioningConfiguration(AmazonS3Client client) { GetBucketVersioningRequest request = new GetBucketVersioningRequest { BucketName = s3config.bucketName }; GetBucketVersioningResponse response = await client.GetBucketVersioningAsync(request); return(response.VersioningConfig.Status); }
public override AmazonWebServiceResponse Unmarshall(XmlUnmarshallerContext context) { GetBucketVersioningResponse getBucketVersioningResponse = new GetBucketVersioningResponse(); while (context.Read()) { if (context.get_IsStartElement()) { UnmarshallResult(context, getBucketVersioningResponse); } } return(getBucketVersioningResponse); }
private static void GetBucketVersioning() { try { GetBucketVersioningRequest request = new GetBucketVersioningRequest() { BucketName = bucketName }; GetBucketVersioningResponse response = client.GetBucketVersioning(request); Console.WriteLine("GetBucketVersioning response: {0}", response.StatusCode); Console.WriteLine("GetBucketVersioning version status: {0}", response.Configuration.Status); } catch (ObsException ex) { Console.WriteLine("Exception errorcode: {0}, when get bucket versioning", ex.ErrorCode); Console.WriteLine("Exception errormessage: {0}", ex.ErrorMessage); } }
static void ReadBucketVersioning() { string bucket = Common.InputString("Bucket:", null, false); GetBucketVersioningRequest request = new GetBucketVersioningRequest(); request.BucketName = bucket; GetBucketVersioningResponse response = _S3Client.GetBucketVersioningAsync(request).Result; if (response != null) { Console.WriteLine("Success"); Console.WriteLine(" MFA delete : " + response.VersioningConfig.EnableMfaDelete.ToString()); Console.WriteLine(" Versioning : " + response.VersioningConfig.Status.Value); } else { Console.WriteLine("Failed"); } }
private static void UnmarshallResult(XmlUnmarshallerContext context, GetBucketVersioningResponse response) { int originalDepth = context.CurrentDepth; int targetDepth = originalDepth + 1; if (context.IsStartOfDocument) { targetDepth += 2; } while (context.Read()) { if (context.IsStartElement || context.IsAttribute) { if (context.TestExpression("Status", targetDepth)) { response.VersioningConfig.Status = StringUnmarshaller.GetInstance().Unmarshall(context); continue; } if (context.TestExpression("MfaDelete", targetDepth)) { response.VersioningConfig.EnableMfaDelete = string.Equals(StringUnmarshaller.GetInstance().Unmarshall(context), "Enabled"); continue; } } else if (context.IsEndElement && context.CurrentDepth < originalDepth) { return; } } return; }
[MultipleProviders(S3Provider.AmazonS3 | S3Provider.BackBlazeB2)] //This test returns the wrong IsLatest on Google public async Task ListObjectVersions(S3Provider provider, string _, ISimpleClient client) { await CreateTempBucketAsync(provider, client, async tempBucket => { //Enable versioning on the bucket await client.PutBucketVersioningAsync(tempBucket, true); //Verify that we enabled bucket versioning GetBucketVersioningResponse getVerResp = await client.GetBucketVersioningAsync(tempBucket); Assert.True(getVerResp.Status); PutObjectResponse putResp1 = await client.PutObjectStringAsync(tempBucket, "1", "a").ConfigureAwait(false); PutObjectResponse putResp2 = await client.PutObjectStringAsync(tempBucket, "2", "aa").ConfigureAwait(false); PutObjectResponse putResp3 = await client.PutObjectStringAsync(tempBucket, "3", "aaa").ConfigureAwait(false); DeleteObjectResponse putResp4 = await client.DeleteObjectAsync(tempBucket, "2"); //Delete object 2 PutObjectResponse putResp5 = await client.PutObjectStringAsync(tempBucket, "3", "aaaa").ConfigureAwait(false); //Overwrite object 3 ListObjectVersionsResponse listResp = await client.ListObjectVersionsAsync(tempBucket); Assert.True(listResp.IsSuccess); Assert.Equal(4, listResp.Versions.Count); if (provider == S3Provider.AmazonS3) { Assert.Equal(1, listResp.DeleteMarkers.Count); Assert.Equal(1000, listResp.MaxKeys); } Assert.Equal(tempBucket, listResp.BucketName); Assert.False(listResp.IsTruncated); S3Version version1 = listResp.Versions[0]; Assert.Equal("1", version1.ObjectKey); Assert.Equal(putResp1.VersionId, version1.VersionId); Assert.True(version1.IsLatest); Assert.Equal(DateTimeOffset.UtcNow.DateTime, version1.LastModified.DateTime, TimeSpan.FromMinutes(1)); Assert.Equal("\"0cc175b9c0f1b6a831c399e269772661\"", version1.Etag); Assert.Equal(1, version1.Size); if (provider != S3Provider.GoogleCloudStorage) { Assert.Equal(StorageClass.Standard, version1.StorageClass); } if (provider == S3Provider.AmazonS3) { Assert.Equal(TestConstants.TestUserId, version1.Owner?.Id); Assert.Equal(TestConstants.TestUsername, version1.Owner?.Name); } S3Version version2 = listResp.Versions[1]; Assert.Equal("2", version2.ObjectKey); Assert.Equal(putResp2.VersionId, version2.VersionId); Assert.False(version2.IsLatest); Assert.Equal(DateTimeOffset.UtcNow.DateTime, version2.LastModified.DateTime, TimeSpan.FromMinutes(1)); Assert.Equal("\"4124bc0a9335c27f086f24ba207a4912\"", version2.Etag); Assert.Equal(2, version2.Size); if (provider != S3Provider.GoogleCloudStorage) { Assert.Equal(StorageClass.Standard, version2.StorageClass); } if (provider == S3Provider.AmazonS3) { Assert.Equal(TestConstants.TestUserId, version2.Owner?.Id); Assert.Equal(TestConstants.TestUsername, version2.Owner?.Name); } //This is the latest version of object 3 and should be 4 in size S3Version version3 = listResp.Versions[2]; Assert.Equal("3", version3.ObjectKey); Assert.Equal(putResp5.VersionId, version3.VersionId); Assert.True(version3.IsLatest); Assert.Equal(DateTimeOffset.UtcNow.DateTime, version3.LastModified.DateTime, TimeSpan.FromMinutes(1)); Assert.Equal("\"74b87337454200d4d33f80c4663dc5e5\"", version3.Etag); Assert.Equal(4, version3.Size); if (provider != S3Provider.GoogleCloudStorage) { Assert.Equal(StorageClass.Standard, version3.StorageClass); } if (provider == S3Provider.AmazonS3) { Assert.Equal(TestConstants.TestUserId, version3.Owner?.Id); Assert.Equal(TestConstants.TestUsername, version3.Owner?.Name); } //This was the previous version of object 3, so it should not be the latest and have 3 in size S3Version version3A = listResp.Versions[3]; Assert.Equal("3", version3A.ObjectKey); Assert.Equal(putResp3.VersionId, version3A.VersionId); Assert.False(version3A.IsLatest); Assert.Equal(DateTimeOffset.UtcNow.DateTime, version3A.LastModified.DateTime, TimeSpan.FromMinutes(1)); Assert.Equal("\"47bce5c74f589f4867dbd57e9ca9f808\"", version3A.Etag); Assert.Equal(3, version3A.Size); if (provider != S3Provider.GoogleCloudStorage) { Assert.Equal(StorageClass.Standard, version3A.StorageClass); } if (provider == S3Provider.AmazonS3) { Assert.Equal(TestConstants.TestUserId, version3A.Owner?.Id); Assert.Equal(TestConstants.TestUsername, version3A.Owner?.Name); } //This is the latest version of object 2, since it was deleted S3DeleteMarker delMarker = listResp.DeleteMarkers[0]; Assert.True(delMarker.IsLatest); Assert.Equal("2", delMarker.ObjectKey); Assert.Equal(putResp4.VersionId, delMarker.VersionId); Assert.Equal(DateTimeOffset.UtcNow.DateTime, delMarker.LastModified.DateTime, TimeSpan.FromMinutes(1)); if (provider == S3Provider.AmazonS3) { Assert.Equal(TestConstants.TestUserId, delMarker.Owner.Id); Assert.Equal(TestConstants.TestUsername, delMarker.Owner.Name); } }).ConfigureAwait(false); }
public static void Main(string[] args) { // create the AWS S3 client AmazonS3Client s3 = AWSS3Factory.getS3Client(); String bucketName = String.Join("-", AWSS3Factory.S3_BUCKET, DateTime.Now.ToString("yyyyMMddHHmmss")); //********************// // 1. Create a bucket // //********************// Console.Write(string.Format(" [*] Creating bucket '{0}'... ", bucketName)); PutBucketResponse pbRes = s3.PutBucket(bucketName); if (pbRes.HttpStatusCode != System.Net.HttpStatusCode.OK) { Console.WriteLine("fail"); Console.ReadLine(); System.Environment.Exit(1); } Console.WriteLine("done"); //*******************************************// // 2. Get current bucket versioning status // //*******************************************// Console.Write(string.Format(" [*] Getting bucket versioning status for bucket '{0}'... ", bucketName)); GetBucketVersioningRequest gvr = new GetBucketVersioningRequest() { BucketName = bucketName }; GetBucketVersioningResponse gvrResponse = s3.GetBucketVersioning(gvr); Console.Write(string.Format("status: {0}", gvrResponse.VersioningConfig.Status)); //*******************************************// // 3. Enable object versioning on the bucket // //*******************************************// // enabled versioning if not yet enabled if (gvrResponse.VersioningConfig.Status != VersionStatus.Enabled) { Console.Write(string.Format(" [*] Enabling bucket versioning for bucket '{0}'... ", bucketName)); PutBucketVersioningRequest pvr = new PutBucketVersioningRequest() { BucketName = bucketName, VersioningConfig = new S3BucketVersioningConfig() { Status = VersionStatus.Enabled } }; PutBucketVersioningResponse pvrResponse = s3.PutBucketVersioning(pvr); if (pvrResponse.HttpStatusCode != System.Net.HttpStatusCode.OK) { Console.WriteLine("fail"); Console.ReadLine(); System.Environment.Exit(1); } Console.WriteLine("done"); } //***********************************************************************// // 4. Upload three object with three versions each (Total of 9 versions) // //***********************************************************************// Console.Write(string.Format(" [*] Uploading 3 objects with 3 versions each to bucket '{0}'... ", bucketName)); for (int i = 0; i < 3; i++) { string objectKey = String.Format("object-{0}", i); for (int j = 0; j < 3; j++) { string objectContent = String.Format("This is object {0}, revision {1}", i, j); PutObjectRequest poRequest = new PutObjectRequest() { BucketName = bucketName, Key = objectKey, ContentBody = objectContent }; PutObjectResponse poResponse = s3.PutObject(poRequest); if (poResponse.HttpStatusCode != System.Net.HttpStatusCode.OK) { Console.WriteLine("fail"); Console.ReadLine(); System.Environment.Exit(1); } Console.Write("."); } } Console.WriteLine("done"); //*******************************************// // 5. List the object versions in the bucket // //*******************************************// Console.WriteLine(" [*] Listing object versions..."); ListVersionsRequest request = new ListVersionsRequest() { BucketName = bucketName, // You can optionally specify key name prefix in the request // if you want list of object versions of a specific object. // For this example we limit response to return list of 2 versions. MaxKeys = 2 }; bool moreRecords = true; while (moreRecords) { ListVersionsResponse response = s3.ListVersions(request); foreach (S3ObjectVersion version in response.Versions) { Console.WriteLine(string.Format(" [x] -> Object key: {0}", version.Key)); Console.WriteLine(string.Format(" [x] VersionId: {0}", version.VersionId)); Console.WriteLine(string.Format(" [x] IsDeleteMarker: {0}", version.IsDeleteMarker)); Console.WriteLine(string.Format(" [x] LastModified: {0}", version.LastModified)); } // If response is truncated, set the marker to get the next // set of keys. if (response.IsTruncated) { request.KeyMarker = response.NextKeyMarker; request.VersionIdMarker = response.NextVersionIdMarker; } else { moreRecords = false; } Console.WriteLine(string.Format(" [x] More records? {0}", moreRecords)); } //*******************************************// // 6. Permanently delete the object versions // //*******************************************// Console.Write(" [*] Permanently deleting all object versions... "); ListVersionsResponse lv2Response = s3.ListVersions(bucketName); if (lv2Response.HttpStatusCode != System.Net.HttpStatusCode.OK) { Console.WriteLine("fail"); Console.ReadLine(); System.Environment.Exit(1); } foreach (S3ObjectVersion version in lv2Response.Versions) { DeleteObjectRequest do2Request = new DeleteObjectRequest() { BucketName = bucketName, Key = version.Key, VersionId = version.VersionId }; DeleteObjectResponse do2Response = s3.DeleteObject(do2Request); if (do2Response.HttpStatusCode != System.Net.HttpStatusCode.NoContent) { Console.WriteLine("fail"); Console.ReadLine(); System.Environment.Exit(1); } } Console.WriteLine("done"); //***********************// // 7. Delete the bucket // //***********************// Console.Write(String.Format(" [*] Deleting bucket '{0}' (sleeping 5 seconds)... ", bucketName)); System.Threading.Thread.Sleep(5000); DeleteBucketResponse dbRes = s3.DeleteBucket(bucketName); if (dbRes.HttpStatusCode != System.Net.HttpStatusCode.NoContent) { Console.WriteLine("fail"); Console.ReadLine(); System.Environment.Exit(1); } Console.WriteLine("done"); Console.WriteLine(" [*] Example is completed. Press any key to exit..."); Console.ReadLine(); }
public void SimplePutTest() { GetBucketVersioningResponse versioning = null; Exception exception = null; var mre = new AutoResetEvent(false); Client.GetBucketVersioningAsync(bucketName, (result) => { versioning = result.Response; exception = result.Exception; mre.Set(); }, options); mre.WaitOne(); Assert.IsNull(exception); var status = versioning.VersioningConfig.Status; Assert.AreEqual(VersionStatus.Off, status); ListObjectsResponse response = null; Client.ListObjectsAsync(bucketName, (result) => { exception = result.Exception; response = result.Response; mre.Set(); }, options); mre.WaitOne(); Assert.IsNull(exception); var objects = response.S3Objects; Assert.IsNotNull(objects); var count = objects.Count; var key = "test.txt"; var contents = "Sample content"; PutObjectResponse putResult = null; Client.PutObjectAsync(new PutObjectRequest { BucketName = bucketName, Key = key, ContentBody = contents }, (result) => { exception = result.Exception; putResult = result.Response; mre.Set(); }, options); mre.WaitOne(); Assert.IsNull(exception); var etag = putResult.ETag; Assert.IsNotNull(etag); Client.ListObjectsAsync(bucketName, (result) => { exception = result.Exception; response = result.Response; mre.Set(); }, options); mre.WaitOne(); Assert.IsNull(exception); objects = response.S3Objects; Assert.IsNotNull(objects); Assert.AreEqual(count + 1, objects.Count); GetObjectMetadataResponse metadataResult = null; Client.GetObjectMetadataAsync(bucketName, key, (result) => { metadataResult = result.Response; exception = result.Exception; mre.Set(); }, options); mre.WaitOne(); Assert.IsNull(exception); Assert.AreEqual(etag, metadataResult.ETag); Client.DeleteObjectAsync(bucketName, key, (result) => { exception = result.Exception; mre.Set(); }, options); mre.WaitOne(); Assert.IsNull(exception); Client.ListObjectsAsync(bucketName, (result) => { exception = result.Exception; objects = result.Response.S3Objects; mre.Set(); }, options); mre.WaitOne(); Assert.IsNull(exception); Assert.IsNotNull(objects); Assert.AreEqual(count, objects.Count); }