public override AmazonWebServiceResponse Unmarshall(XmlUnmarshallerContext context)
        {
            PutBucketVersioningResponse response = new PutBucketVersioningResponse();


            return(response);
        }
        public async Task <IActionResult> enableVersioning()
        {
            var status = VersionStatus.Enabled;

            using (var client = new AmazonS3Client(s3config.accesskey, s3config.secretkey, s3config.bucketRegion))
            {
                var currentStatus = await RetrieveBucketVersioningConfiguration(client);

                if (currentStatus == "Enabled")
                {
                    status = VersionStatus.Suspended;
                }

                PutBucketVersioningRequest request = new PutBucketVersioningRequest
                {
                    BucketName       = s3config.bucketName,
                    VersioningConfig = new S3BucketVersioningConfig
                    {
                        Status = status
                    }
                };


                PutBucketVersioningResponse response = await client.PutBucketVersioningAsync(request);
            }

            return(RedirectToAction(nameof(s3Manager)));
        }
        public override AmazonWebServiceResponse Unmarshall(XmlUnmarshallerContext context) 
        {
            PutBucketVersioningResponse response = new PutBucketVersioningResponse();
            

            return response;
        }
        static void WriteBucketVersioning()
        {
            string bucket     = Common.InputString("Bucket:", null, false);
            bool   versioning = Common.InputBoolean("Enable versioning:", true);

            PutBucketVersioningRequest request = new PutBucketVersioningRequest();

            request.BucketName       = bucket;
            request.VersioningConfig = new S3BucketVersioningConfig();
            request.VersioningConfig.EnableMfaDelete = false;

            if (versioning)
            {
                request.VersioningConfig.Status = VersionStatus.Enabled;
            }
            else
            {
                request.VersioningConfig.Status = VersionStatus.Suspended;
            }

            PutBucketVersioningResponse response = _S3Client.PutBucketVersioningAsync(request).Result;

            if (response != null)
            {
                Console.WriteLine("Success");
            }
            else
            {
                Console.WriteLine("Failed");
            }
        }
Пример #5
0
    [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);
    }
Пример #6
0
        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();
        }
Пример #7
0
        public static void Main(string[] args)
        {
            System.Net.ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);

            // 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. Enable object versioning on the bucket //
            //*******************************************//

            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");

            //************************************//
            // 3. Create a new object (version 1) //
            //************************************//

            String objectKey = "object-" + DateTime.Now.ToString("yyyyMMddHHmmssffff");

            Console.Write(string.Format(" [*] Creating a new object with key '{0}'... ", objectKey));

            PutObjectRequest poRequest = new PutObjectRequest()
            {
                BucketName  = bucketName,
                ContentBody = "Lorem ipsum dolor sit amet, consectetur adipiscing elit...",
                Key         = objectKey
            };

            PutObjectResponse poResponse = s3.PutObject(poRequest);

            if (poResponse.HttpStatusCode != System.Net.HttpStatusCode.OK)
            {
                Console.WriteLine("fail");
                Console.ReadLine();
                System.Environment.Exit(1);
            }
            Console.WriteLine("done");
            Console.WriteLine(string.Format(" [x] Object content: '{0}'", poRequest.ContentBody));

            //****************************************//
            // 4. Delete the object (deletion marker) //
            //****************************************//

            Console.Write(string.Format(" [*] Deleting object with key '{0}' (adding a deletion marker)... ", objectKey));

            DeleteObjectRequest doRequest = new DeleteObjectRequest()
            {
                BucketName = bucketName,
                Key        = objectKey
            };

            DeleteObjectResponse doResponse = s3.DeleteObject(doRequest);

            if (doResponse.HttpStatusCode != System.Net.HttpStatusCode.NoContent || doResponse.DeleteMarker != "true")
            {
                Console.WriteLine("fail");
                Console.ReadLine();
                System.Environment.Exit(1);
            }
            Console.WriteLine("done");

            //*************************************************//
            // 5. Try to get the object (expect 404 Not Found) //
            //*************************************************//

            Console.Write(string.Format(" [*] Trying to read object with key '{0}' (expecting 404 Not Found)... ", objectKey));

            GetObjectRequest goRequest = new GetObjectRequest()
            {
                BucketName = bucketName,
                Key        = objectKey,
            };

            try
            {
                // should throw an exception as the object is marked as deleted
                s3.GetObject(goRequest);
                Console.WriteLine("fail");
                Console.ReadLine();
                System.Environment.Exit(1);
            }
            catch (AmazonS3Exception e) {
                if (e.StatusCode != System.Net.HttpStatusCode.NotFound)
                {
                    Console.WriteLine("fail");
                    Console.ReadLine();
                    System.Environment.Exit(1);
                }
            }
            Console.WriteLine("done (404 Not Found)");

            //*************************************************************************//
            // 6. List the object versions and get the version ID of the first version //
            //*************************************************************************//

            Console.WriteLine(string.Format(" [*] Listing object versions for bucket '{0}' and getting version ID to restore... ", bucketName));

            ListVersionsResponse lvResponse = s3.ListVersions(bucketName);

            if (lvResponse.HttpStatusCode != System.Net.HttpStatusCode.OK)
            {
                Console.WriteLine("fail");
                Console.ReadLine();
                System.Environment.Exit(1);
            }

            String restoreVersion = String.Empty;

            foreach (S3ObjectVersion version in lvResponse.Versions)
            {
                if (version.Key != objectKey)
                {
                    // filtering out other objects
                    continue;
                }

                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 (!version.IsDeleteMarker)
                {
                    restoreVersion = version.VersionId;
                }
            }

            if (restoreVersion.Length == 0)
            {
                Console.WriteLine(" [*] Could not find a version to restore, exiting...");
                Console.ReadLine();
                System.Environment.Exit(1);
            }

            //******************************************************************//
            // 7. Restore the first version using a server-side copy operation. //
            //******************************************************************//

            Console.Write(string.Format(" [*] Restoring object version ID '{0}' (server-side copy)... ", restoreVersion));

            CopyObjectRequest coRequest = new CopyObjectRequest()
            {
                SourceBucket      = bucketName,
                SourceKey         = objectKey,
                SourceVersionId   = restoreVersion,
                DestinationBucket = bucketName,
                DestinationKey    = objectKey
            };

            CopyObjectResponse coResponse = s3.CopyObject(coRequest);

            if (coResponse.HttpStatusCode != System.Net.HttpStatusCode.OK)
            {
                Console.WriteLine("fail");
                Console.ReadLine();
                System.Environment.Exit(1);
            }
            Console.WriteLine("done");

            //************************************************************//
            // 8. Verify that the object can now be successfully obtained //
            //************************************************************//

            Console.Write(string.Format(" [*] Trying to read object '{0}'... ", objectKey));

            GetObjectResponse goResponse = s3.GetObject(goRequest);

            if (goResponse.HttpStatusCode != System.Net.HttpStatusCode.OK || goResponse.ContentLength != poRequest.ContentBody.Length)
            {
                Console.WriteLine("fail");
                Console.ReadLine();
                System.Environment.Exit(1);
            }

            Console.WriteLine("done");

            String responseBody = "";

            using (Stream responseStream = goResponse.ResponseStream)
                using (StreamReader reader = new StreamReader(responseStream))
                {
                    responseBody = reader.ReadToEnd();
                }

            Console.WriteLine(string.Format(" [x] Object '{0}' successfully restored. New VersionId: '{1}'. Content: '{2}'",
                                            goResponse.Key,
                                            goResponse.VersionId,
                                            responseBody));

            //*******************************************//
            // 9. 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");

            //***********************//
            // 10. 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();
        }