示例#1
0
        /// <summary>
        /// Unmarshaller the response from the service to the response class.
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override AmazonWebServiceResponse Unmarshall(JsonUnmarshallerContext context)
        {
            ListVersionsResponse response = new ListVersionsResponse();

            context.Read();
            int targetDepth = context.CurrentDepth;

            while (context.ReadAtDepth(targetDepth))
            {
                if (context.TestExpression("NextToken", targetDepth))
                {
                    var unmarshaller = StringUnmarshaller.Instance;
                    response.NextToken = unmarshaller.Unmarshall(context);
                    continue;
                }
                if (context.TestExpression("Versions", targetDepth))
                {
                    var unmarshaller = new ListUnmarshaller <string, StringUnmarshaller>(StringUnmarshaller.Instance);
                    response.Versions = unmarshaller.Unmarshall(context);
                    continue;
                }
            }

            return(response);
        }
示例#2
0
        public async Task <List <String> > GetS3FilesAsync()
        {
            ListVersionsResponse response =
                await this.awsClient.ListVersionsAsync(this.bucketName);

            return(response.Versions.Select(x => x.Key).ToList());
        }
示例#3
0
        private static void ListVersions()
        {
            try
            {
                ListVersionsRequest request = new ListVersionsRequest()
                {
                    BucketName = bucketName,
                    MaxKeys    = 10,
                    Delimiter  = "delimiter",
                    Prefix     = "prefix",
                    KeyMarker  = "keyMarker"
                };
                ListVersionsResponse response = client.ListVersions(request);
                Console.WriteLine("ListVersions response: {0}", response.StatusCode);

                foreach (ObsObjectVersion objectVersion in response.Versions)
                {
                    Console.WriteLine("ListVersions response Versions Key: {0}", objectVersion.ObjectKey);
                    Console.WriteLine("ListVersions response Versions VersionId: {0}", objectVersion.VersionId);
                }
            }
            catch (ObsException ex)
            {
                Console.WriteLine("Exception errorcode: {0}, when list versions.", ex.ErrorCode);
                Console.WriteLine("Exception errormessage: {0}", ex.ErrorMessage);
            }
        }
示例#4
0
        public async Task <S3ObjectVersion[]> ListVersionsAsync(string bucketName, string prefix, CancellationToken cancellationToken = default(CancellationToken))
        {
            ListVersionsResponse response = null;
            var results = new List <S3ObjectVersion>();

            while ((response = await _S3Client.ListVersionsAsync(new ListVersionsRequest()
            {
                VersionIdMarker = response?.NextVersionIdMarker,
                KeyMarker = response?.NextKeyMarker,
                BucketName = bucketName,
                Prefix = prefix,
                MaxKeys = 100000,
            }, cancellationToken).EnsureSuccessAsync()) != null)
            {
                if (!response.Versions.IsNullOrEmpty())
                {
                    results.AddRange(response.Versions);
                }

                if (!response.IsTruncated)
                {
                    break;
                }

                await Task.Delay(100);
            }

            return(results.ToArray());
        }
示例#5
0
        public static void Main(string[] args)
        {
            // create the AWS S3 client
            AmazonS3Client s3 = AWSS3Factory.getS3Client();

            bool   moreRecords = true;
            string nextMarker  = string.Empty;

            while (moreRecords)
            {
                ListVersionsRequest request = new ListVersionsRequest()
                {
                    BucketName = AWSS3Factory.S3_BUCKET,
                };

                if (nextMarker.Length > 0)
                {
                    request.KeyMarker = nextMarker;
                }

                ListVersionsResponse response = new ListVersionsResponse();
                response = s3.ListVersions(request);


                foreach (S3ObjectVersion theObject in response.Versions)
                {
                    s3.DeleteObject(new DeleteObjectRequest()
                    {
                        BucketName = AWSS3Factory.S3_BUCKET,
                        Key        = theObject.Key,
                        VersionId  = theObject.VersionId
                    });
                    Console.WriteLine("Deleted {0}/{1}", AWSS3Factory.S3_BUCKET, theObject.Key);
                }

                Console.WriteLine(string.Format("Next Marker: {0} Version Count: {1}", response.NextKeyMarker, response.Versions.Count.ToString()));

                if (response.IsTruncated)
                {
                    nextMarker = response.NextKeyMarker;
                }
                else
                {
                    moreRecords = false;
                }
            }



            s3.DeleteBucket(new DeleteBucketRequest()
            {
                BucketName = AWSS3Factory.S3_BUCKET
            });

            // print bucket name for validation
            Console.WriteLine(string.Format("Deleted bucket {0}", AWSS3Factory.S3_BUCKET));
            Console.ReadLine();
        }
示例#6
0
        public static void Main(string[] args)
        {
            // create the AWS S3 client
            AmazonS3Client s3 = AWSS3Factory.getS3Client();

            System.Net.ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);

            GetBucketVersioningRequest gvr = new GetBucketVersioningRequest()
            {
                BucketName = AWSS3Factory.S3_BUCKET
            };

            Console.WriteLine(s3.GetBucketVersioning(gvr).VersioningConfig.Status);


            bool   moreRecords = true;
            string nextMarker  = string.Empty;

            while (moreRecords)
            {
                ListVersionsRequest request = new ListVersionsRequest()
                {
                    BucketName = AWSS3Factory.S3_BUCKET,
                };

                //if (nextMarker.Length > 0)
                //request.KeyMarker = nextMarker;


                request.VersionIdMarker = "1472739256446";


                ListVersionsResponse response = new ListVersionsResponse();
                response = s3.ListVersions(request);


                foreach (S3ObjectVersion key in response.Versions)
                {
                    Console.WriteLine(key.Key);
                }

                Console.WriteLine(string.Format("Next Marker: {0} Version Count: {1}", response.NextKeyMarker, response.Versions.Count.ToString()));

                if (response.IsTruncated)
                {
                    nextMarker = response.NextKeyMarker;
                }
                else
                {
                    moreRecords = false;
                }
            }

            // print out object key/value for validation
            //Console.WriteLine(string.Format("Copied object {0}/{1} to {2}/{3}", AWSS3Factory.S3_BUCKET, key_source, AWSS3Factory.S3_BUCKET, key_target));
            Console.ReadLine();
        }
        public async Task <List <S3ObjectVersion> > GetS3ObjectswithVersions(AmazonS3Client client)
        {
            ListVersionsRequest request = new ListVersionsRequest()
            {
                BucketName = s3config.bucketName,
                MaxKeys    = 500
            };
            ListVersionsResponse response = await client.ListVersionsAsync(request);

            return(response.Versions);
        }
示例#8
0
        static void ReadBucketVersions()
        {
            string bucket = Common.InputString("Bucket:", null, false);

            ListVersionsRequest request = new ListVersionsRequest();

            request.BucketName = bucket;

            ListVersionsResponse response = _S3Client.ListVersionsAsync(request).Result;

            if (response != null)
            {
                Console.WriteLine("Success");
                Console.WriteLine("Bucket name     : " + response.Name);
                Console.WriteLine("Max keys        : " + response.MaxKeys);
                Console.WriteLine("Is truncated    : " + response.IsTruncated);

                Console.Write("Versions        : ");
                if (response.Versions != null && response.Versions.Count > 0)
                {
                    Console.WriteLine(response.Versions.Count);
                    foreach (S3ObjectVersion ver in response.Versions)
                    {
                        Console.Write("| " + ver.Key + "[" + ver.VersionId + "] ");
                        if (ver.IsDeleteMarker)
                        {
                            Console.WriteLine("(delete marker)");
                        }
                        else
                        {
                            Console.WriteLine(ver.Size + " bytes");
                        }
                    }
                }

                Console.Write("Common prefixes : ");
                if (response.CommonPrefixes != null && response.CommonPrefixes.Count > 0)
                {
                    Console.WriteLine(response.CommonPrefixes.Count);
                    foreach (string prefix in response.CommonPrefixes)
                    {
                        Console.WriteLine("| " + prefix);
                    }
                }
                else
                {
                    Console.WriteLine("0");
                }
            }
            else
            {
                Console.WriteLine("Failed");
            }
        }
示例#9
0
        static void GetObjectListWithAllVersions(IAmazonS3 client)
        {
            try
            {
                ListVersionsRequest request = new ListVersionsRequest()
                {
                    BucketName = bucketName,

                    MaxKeys = 2
                };
                do
                {
                    ListVersionsResponse response = client.ListVersions(request);
                    // Process response.
                    foreach (S3ObjectVersion entry in response.Versions)
                    {
                        Console.WriteLine("key = {0} size = {1}",
                                          entry.Key, entry.Size);
                    }

                    // 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
                    {
                        request = null;
                    }
                } while (request != null);
            }
            catch (AmazonS3Exception amazonS3Exception)
            {
                if (amazonS3Exception.ErrorCode != null &&
                    (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId")
                     ||
                     amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
                {
                    Console.WriteLine("Check the provided AWS Credentials.");
                    Console.WriteLine(
                        "To sign up for service, go to http://aws.amazon.com/s3");
                }
                else
                {
                    Console.WriteLine(
                        "Error occurred. Message:'{0}' when listing objects",
                        amazonS3Exception.Message);
                }
            }
        }
示例#10
0
        public override AmazonWebServiceResponse Unmarshall(XmlUnmarshallerContext context)
        {
            ListVersionsResponse listVersionsResponse = new ListVersionsResponse();

            while (context.Read())
            {
                if (context.get_IsStartElement())
                {
                    UnmarshallResult(context, listVersionsResponse);
                }
            }
            return(listVersionsResponse);
        }
示例#11
0
        public async Task <List <string> > FilesList()
        {
            try
            {
                ListVersionsResponse listVersionsResponse = await _awss3BucketHelper.FilesList();

                return(listVersionsResponse.Versions.Select(c => c.Key).ToList());
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
示例#12
0
        public async Task <List <string> > FilesList(string bucketName)
        {
            try
            {
                ListVersionsResponse listVersions = await _AWSS3BucketHelper.FilesList(bucketName);

                return(listVersions.Versions.Select(c => c.Key).ToList());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
示例#13
0
        public IEnumerator <S3ObjectVersion> GetEnumerator()
        {
            var cont = true;

            while (cont)
            {
                _response = _s3Client.ListVersionsAsync(_request).Result;
                foreach (var o in _response.Versions)
                {
                    yield return(o);
                }

                _request.KeyMarker = _response.NextKeyMarker;
                cont = !string.IsNullOrWhiteSpace(_request.KeyMarker);
            }
        }
示例#14
0
        /// <summary>
        /// Returns the list of restore points for a specific object
        /// </summary>
        public ObjectVersion[] GetVersions(string filename)
        {
            try
            {
                string fmtObject = $"{filename}.encrypted";

                ListVersionsRequest request = new ListVersionsRequest()
                {
                    BucketName = _bucketName,
                    MaxKeys    = int.MaxValue,
                    Prefix     = filename
                };

                ListVersionsResponse response = _client.ListVersionsAsync(request).Result;

                // Be sure the object exists
                if ((response.Versions == null) || (response.Versions.Count() == 0))
                {
                    _logger.WriteLog(ErrorCodes.S3ObjectRestore_ObjectNotFound,
                                     string.Format(ErrorResources.S3ObjectRestore_ObjectNotFound, filename),
                                     Severity.Error, VerboseLevel.User);
                    return(null);
                }

                var list = response.Versions;

                // Build a list of objects found
                return(list.Select(item =>
                                   new ObjectVersion()
                {
                    Name = item.Key,
                    TimeCreated = item.LastModified,
                    StorageClass = item.StorageClass,
                    Size = item.Size,
                    VersionId = item.VersionId
                })
                       .OrderByDescending(a => a.TimeCreated)
                       .ToArray());
            }
            catch (Exception ex)
            {
                _logger.WriteLog(ErrorCodes.S3ObjectRestore_GetVersionsException,
                                 ErrorResources.S3ObjectRestore_GetVersionsException + Environment.NewLine + ex.Message,
                                 Severity.Error, VerboseLevel.User);
                return(null);
            }
        }
        static async Task GetObjectListWithAllVersionsAsync()
        {
            try
            {
                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
                };
                do
                {
                    ListVersionsResponse response = await s3Client.ListVersionsAsync(request);

                    // Process response.
                    foreach (S3ObjectVersion entry in response.Versions)
                    {
                        Console.WriteLine("key = {0} size = {1}",
                                          entry.Key, entry.Size);
                    }

                    // 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
                    {
                        request = null;
                    }
                } while (request != null);
            }
            catch (AmazonS3Exception e)
            {
                Console.WriteLine("Error encountered on server. Message:'{0}' when writing an object", e.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", e.Message);
            }
        }
        /// <summary>
        /// This method lists all versions of the objects within an Amazon S3
        /// version enabled bucket.
        /// </summary>
        /// <param name="client">The initialized client object used to call
        /// ListVersionsAsync.</param>
        /// <param name="bucketName">The name of the version enabled S3 bucket
        /// for which you want to list the versions of the contained objects.</param>
        public static async Task GetObjectListWithAllVersionsAsync(IAmazonS3 client, string bucketName)
        {
            try
            {
                // When you instantiate the ListVersionRequest, you can
                // optionally specify a key name prefix in the request
                // if you want list of object versions of a specific object.

                // For this example we set a small limit in MaxKeys to return
                // a small list of versions.
                ListVersionsRequest request = new ()
                {
                    BucketName = bucketName,
                    MaxKeys    = 2,
                };

                do
                {
                    ListVersionsResponse response = await client.ListVersionsAsync(request);

                    // Process response.
                    foreach (S3ObjectVersion entry in response.Versions)
                    {
                        Console.WriteLine($"key: {entry.Key} size: {entry.Size}");
                    }

                    // 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
                    {
                        request = null;
                    }
                }while (request != null);
            }
            catch (AmazonS3Exception ex)
            {
                Console.WriteLine($"Error: '{ex.Message}'");
            }
        }
示例#17
0
        private static bool CleanBucket(string bucket)
        {
            bool   moreRecords = true;
            string nextMarker  = string.Empty;

            while (moreRecords)
            {
                ListVersionsRequest request = new ListVersionsRequest()
                {
                    BucketName = bucket,
                };

                if (nextMarker.Length > 0)
                {
                    request.KeyMarker = nextMarker;
                }

                ListVersionsResponse response = new ListVersionsResponse();
                response = client.ListVersions(request);


                foreach (S3ObjectVersion theObject in response.Versions)
                {
                    client.DeleteObject(new DeleteObjectRequest()
                    {
                        BucketName = bucket,
                        Key        = theObject.Key,
                        VersionId  = theObject.VersionId
                    });
                }

                if (response.IsTruncated)
                {
                    nextMarker = response.NextKeyMarker;
                }
                else
                {
                    moreRecords = false;
                }
            }

            return(true);
        }
        public IEnumerator <S3ObjectVersion> GetEnumerator()
        {
            _nextVersionMarker = null;
            _lastResponse      = null;

            do
            {
                var request = new ListVersionsRequest {
                    BucketName = _bucket, Prefix = _folder, KeyMarker = _nextVersionMarker
                };
                _lastResponse = _client.ListVersionsAsync(request).Result;

                foreach (var version in _lastResponse.Versions)
                {
                    yield return(version);
                }

                _nextVersionMarker = _lastResponse.NextKeyMarker;
            } while (_lastResponse.IsTruncated);
        }
        public async Task <List <String> > GetS3Files(String nombre)
        {
            ListVersionsResponse response = await awsClient.ListVersionsAsync(this.bucketName);

            return(response.Versions.Select(x => x.Key).Where(x => x.StartsWith(nombre)).ToList());
        }
        private static void UnmarshallResult(XmlUnmarshallerContext context, ListVersionsResponse 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("IsTruncated", targetDepth))
                    {
                        response.IsTruncated = BoolUnmarshaller.GetInstance().Unmarshall(context);

                        continue;
                    }
                    if (context.TestExpression("KeyMarker", targetDepth))
                    {
                        response.KeyMarker = StringUnmarshaller.GetInstance().Unmarshall(context);

                        continue;
                    }
                    if (context.TestExpression("Delimiter", targetDepth))
                    {
                        response.Delimiter = StringUnmarshaller.GetInstance().Unmarshall(context);

                        continue;
                    }
                    if (context.TestExpression("VersionIdMarker", targetDepth))
                    {
                        response.VersionIdMarker = StringUnmarshaller.GetInstance().Unmarshall(context);

                        continue;
                    }
                    if (context.TestExpression("NextKeyMarker", targetDepth))
                    {
                        response.NextKeyMarker = StringUnmarshaller.GetInstance().Unmarshall(context);

                        continue;
                    }
                    if (context.TestExpression("NextVersionIdMarker", targetDepth))
                    {
                        response.NextVersionIdMarker = StringUnmarshaller.GetInstance().Unmarshall(context);

                        continue;
                    }
                    if (context.TestExpression("Version", targetDepth))
                    {
                        response.Versions.Add(VersionsItemUnmarshaller.Instance.Unmarshall(context));

                        continue;
                    }
                    if (context.TestExpression("DeleteMarker", targetDepth))
                    {
                        var version = VersionsItemUnmarshaller.Instance.Unmarshall(context);
                        version.IsDeleteMarker = true;
                        response.Versions.Add(version);

                        continue;
                    }
                    if (context.TestExpression("Name", targetDepth))
                    {
                        response.Name = StringUnmarshaller.GetInstance().Unmarshall(context);

                        continue;
                    }
                    if (context.TestExpression("Prefix", targetDepth))
                    {
                        response.Prefix = StringUnmarshaller.GetInstance().Unmarshall(context);

                        continue;
                    }
                    if (context.TestExpression("MaxKeys", targetDepth))
                    {
                        response.MaxKeys = IntUnmarshaller.GetInstance().Unmarshall(context);

                        continue;
                    }
                    if (context.TestExpression("CommonPrefixes", targetDepth))
                    {
                        var prefix = CommonPrefixesItemUnmarshaller.Instance.Unmarshall(context);

                        if (prefix != null)
                        {
                            response.CommonPrefixes.Add(prefix);
                        }

                        continue;
                    }
                }
                else if (context.IsEndElement && context.CurrentDepth < originalDepth)
                {
                    return;
                }
            }



            return;
        }
        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();
        }
示例#22
0
        public static void DeleteBucketWithObjects(IAmazonS3 s3Client, string bucketName)
        {
            // Validations.
            if (s3Client == null)
            {
                throw new ArgumentNullException("s3Client", "The s3Client cannot be null!");
            }

            if (string.IsNullOrEmpty(bucketName))
            {
                throw new ArgumentNullException("bucketName", "The bucketName cannot be null or empty string!");
            }

            var listVersionsRequest = new ListVersionsRequest
            {
                BucketName = bucketName
            };

            ListVersionsResponse listVersionsResponse = null;
            string lastRequestId = null;

            var exception = new Exception();
            var mre       = new AutoResetEvent(false);

            // Iterate through the objects in the bucket and delete them.
            do
            {
                // List all the versions of all the objects in the bucket.
                s3Client.ListVersionsAsync(listVersionsRequest, (result) =>
                {
                    exception            = result.Exception;
                    listVersionsResponse = result.Response;
                    mre.Set();
                }, new AsyncOptions()
                {
                    ExecuteCallbackOnMainThread = false
                });
                mre.WaitOne();
                Utils.AssertExceptionIsNull(exception);


                lastRequestId = listVersionsResponse.ResponseMetadata.RequestId;

                if (listVersionsResponse.Versions.Count == 0)
                {
                    // If the bucket has no objects break the loop.
                    break;
                }

                var keyVersionList = new List <KeyVersion>(listVersionsResponse.Versions.Count);
                for (int index = 0; index < listVersionsResponse.Versions.Count; index++)
                {
                    keyVersionList.Add(new KeyVersion
                    {
                        Key       = listVersionsResponse.Versions[index].Key,
                        VersionId = listVersionsResponse.Versions[index].VersionId
                    });
                }


                var deleteObjectsResponse = new DeleteObjectsResponse();
                // Delete the current set of objects.
                s3Client.DeleteObjectsAsync(new DeleteObjectsRequest
                {
                    BucketName = bucketName,
                    Objects    = keyVersionList,
                    Quiet      = true
                }, (result) =>
                {
                    deleteObjectsResponse = result.Response;
                    exception             = result.Exception;
                    mre.Set();
                }, new AsyncOptions()
                {
                    ExecuteCallbackOnMainThread = false
                });
                mre.WaitOne();
                Utils.AssertExceptionIsNull(exception);

                // Set the markers to get next set of objects from the bucket.
                listVersionsRequest.KeyMarker       = listVersionsResponse.NextKeyMarker;
                listVersionsRequest.VersionIdMarker = listVersionsResponse.NextVersionIdMarker;
            }
            // Continue listing objects and deleting them until the bucket is empty.
            while (listVersionsResponse.IsTruncated);

            // Bucket is empty, delete the bucket.
            s3Client.DeleteBucketAsync(new DeleteBucketRequest
            {
                BucketName = bucketName
            }, (result) =>
            {
                exception = result.Exception;
                mre.Set();
            }, new AsyncOptions()
            {
                ExecuteCallbackOnMainThread = false
            });

            mre.WaitOne();
            Utils.AssertExceptionIsNull(exception);
        }
示例#23
0
        private static void UnmarshallResult(XmlUnmarshallerContext context, ListVersionsResponse 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("IsTruncated", num))
                    {
                        response.IsTruncated = BoolUnmarshaller.GetInstance().Unmarshall(context);
                    }
                    else if (context.TestExpression("KeyMarker", num))
                    {
                        response.KeyMarker = StringUnmarshaller.GetInstance().Unmarshall(context);
                    }
                    else if (context.TestExpression("Delimiter", num))
                    {
                        response.Delimiter = StringUnmarshaller.GetInstance().Unmarshall(context);
                    }
                    else if (context.TestExpression("VersionIdMarker", num))
                    {
                        response.VersionIdMarker = StringUnmarshaller.GetInstance().Unmarshall(context);
                    }
                    else if (context.TestExpression("NextKeyMarker", num))
                    {
                        response.NextKeyMarker = StringUnmarshaller.GetInstance().Unmarshall(context);
                    }
                    else if (context.TestExpression("NextVersionIdMarker", num))
                    {
                        response.NextVersionIdMarker = StringUnmarshaller.GetInstance().Unmarshall(context);
                    }
                    else if (context.TestExpression("Version", num))
                    {
                        response.Versions.Add(VersionsItemUnmarshaller.Instance.Unmarshall(context));
                    }
                    else if (context.TestExpression("DeleteMarker", num))
                    {
                        S3ObjectVersion s3ObjectVersion = VersionsItemUnmarshaller.Instance.Unmarshall(context);
                        s3ObjectVersion.IsDeleteMarker = true;
                        response.Versions.Add(s3ObjectVersion);
                    }
                    else if (context.TestExpression("Name", num))
                    {
                        response.Name = StringUnmarshaller.GetInstance().Unmarshall(context);
                    }
                    else if (context.TestExpression("Prefix", num))
                    {
                        response.Prefix = StringUnmarshaller.GetInstance().Unmarshall(context);
                    }
                    else if (context.TestExpression("MaxKeys", num))
                    {
                        response.MaxKeys = IntUnmarshaller.GetInstance().Unmarshall(context);
                    }
                    else if (context.TestExpression("CommonPrefixes", num))
                    {
                        string text = CommonPrefixesItemUnmarshaller.Instance.Unmarshall(context);
                        if (text != null)
                        {
                            response.CommonPrefixes.Add(text);
                        }
                    }
                }
                else if (context.get_IsEndElement() && context.get_CurrentDepth() < currentDepth)
                {
                    break;
                }
            }
        }
示例#24
0
        public async Task <List <string> > GetAllFiles()
        {
            ListVersionsResponse listVersions = await this.awsclient.ListVersionsAsync(this.bucketName);

            return(listVersions.Versions.Select(x => x.Key).ToList());
        }
示例#25
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();
        }