예제 #1
0
        public static void Main(string[] args)
        {
            // create the AWS S3 client
            AmazonS3Client s3 = AWSS3Factory.getS3Client();

            // create bucket request
            PutBucketRequest request = new PutBucketRequest()
            {
                BucketName = AWSS3Factory.S3_BUCKET
            };

            // create bucket - used for subsequent demos
            s3.PutBucket(request);

            // create bucket lising request
            ListObjectsRequest objects = new ListObjectsRequest()
            {
                BucketName = AWSS3Factory.S3_BUCKET
            };

            // get bucket lising to retrieve bucket name
            ListObjectsResponse result = s3.ListObjects(objects);

            // print bucket name for validation
            Console.WriteLine(string.Format("Successfully created bucket {0}.", result.Name));
            Console.ReadLine();
        }
예제 #2
0
        public static void Main(string[] args)
        {
            // create the AWS S3 Client
            AmazonS3Client s3 = AWSS3Factory.getS3Client();

            // retrieve the object key from the user
            Console.Write("Enter the object key: ");
            string key = Console.ReadLine();

            // create object metadata request
            GetObjectMetadataRequest request = new GetObjectMetadataRequest()
            {
                BucketName = AWSS3Factory.S3_BUCKET,
                Key        = key
            };

            // get object metadata - not actual content (HEAD request not GET).
            GetObjectMetadataResponse response = s3.GetObjectMetadata(request);

            // print out object key/value and metadata key/value for validation
            Console.WriteLine(string.Format("Metadata for {0}/{1}", AWSS3Factory.S3_BUCKET, key));

            MetadataCollection metadataCollection = response.Metadata;

            ICollection <string> metaKeys = metadataCollection.Keys;

            foreach (string metaKey in metaKeys)
            {
                Console.WriteLine("{0}={1}", metaKey, metadataCollection[metaKey]);
            }
            Console.ReadLine();
        }
예제 #3
0
        public static void Main(string[] args)
        {
            // create the AWS S3 client
            AmazonS3Client s3 = AWSS3Factory.getS3Client();

            // retrieve the object key and hours before expiration
            Console.Write("Enter the object key: ");
            string key = Console.ReadLine();

            Console.Write("How long should this tag be valid?: ");
            string hours = Console.ReadLine();

            // create expiration based on input
            DateTime expiration = DateTime.Now.AddHours(Convert.ToDouble(hours));

            // create the request object to generate pre-signed url
            GetPreSignedUrlRequest request = new GetPreSignedUrlRequest()
            {
                BucketName = AWSS3Factory.S3_BUCKET,
                Key        = key,
                Expires    = expiration,
                Verb       = HttpVerb.GET,
            };

            // get pre-signed url
            string url = s3.GetPreSignedURL(request);

            // print objects pre-signed url
            Console.WriteLine(string.Format("Object {0}/{1} pre-signed url: {2}", AWSS3Factory.S3_BUCKET, key, url));
            Console.ReadLine();
        }
예제 #4
0
        public static void Main(string[] args)
        {
            // create the AWS S3 client
            AmazonS3Client s3 = AWSS3Factory.getS3Client();

            // retrieve the object key and new object value from the user
            Console.Write("Enter the object key: ");
            string key = Console.ReadLine();

            Console.Write("Enter new object content: ");
            string content = Console.ReadLine();

            // create the request object
            PutObjectRequest request = new PutObjectRequest()
            {
                BucketName  = AWSS3Factory.S3_BUCKET,
                ContentBody = content,
                Key         = key
            };

            // update the object in the demo bucket
            s3.PutObject(request);

            // print out object key/value for validation
            Console.WriteLine(string.Format("Updated object {0}/{1} with content: {2}", AWSS3Factory.S3_BUCKET, key, content));
            Console.ReadLine();
        }
예제 #5
0
        public static void Main(string[] args)
        {
            // create the AWS S3 client
            AmazonS3Client s3 = AWSS3Factory.getS3Client();

            // retrieve the key value from user to copy
            Console.Write("Enter the object key you want to copy: ");
            string key_source = Console.ReadLine();

            // retrieve the key value from user to name copied object
            Console.Write("Enter the object key for the copied object: ");
            string key_target = Console.ReadLine();

            // create the request object
            CopyObjectRequest request = new CopyObjectRequest()
            {
                SourceBucket      = AWSS3Factory.S3_BUCKET,
                SourceKey         = key_source,
                DestinationBucket = AWSS3Factory.S3_BUCKET,
                DestinationKey    = key_target
            };

            // copy the object
            CopyObjectResponse response = s3.CopyObject(request);

            // 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();
        }
예제 #6
0
        public static void Main(string[] args)
        {
            // create the AWS S3 client
            AmazonS3Client s3 = AWSS3Factory.getS3Client();

            // retrieve the key value from user
            Console.Write("Enter the object key: ");
            string key = Console.ReadLine();

            // create the request object
            GetObjectRequest request = new GetObjectRequest()
            {
                BucketName = AWSS3Factory.S3_BUCKET,
                Key        = key,
            };

            // read the object from the demo bucket
            GetObjectResponse response = s3.GetObject(request);

            // convert the object to a text string
            Stream       responseStream = response.ResponseStream;
            StreamReader reader         = new StreamReader(responseStream);
            string       content        = reader.ReadToEnd();

            // print object key/value and content for validation
            Console.WriteLine(string.Format("Object {0} contents: {1}", response.Key, content));
            Console.ReadLine();
        }
예제 #7
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();
        }
예제 #8
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();
        }
예제 #9
0
        private static async Task <PartETag> ProcessChunk(UploadPartRequest upr)
        {
            Console.WriteLine(string.Format("Sending chunk {0} starting at position {1}", upr.PartNumber, upr.FilePosition));

            // upload the chucnk and return a new PartETag when upload completes
            UploadPartResponse response = await AWSS3Factory.getS3Client().UploadPartAsync(upr, new System.Threading.CancellationToken());

            return(new PartETag(response.PartNumber, response.ETag));
        }
예제 #10
0
        public static void Main(string[] args)
        {
            // create the AWS S3 client
            AmazonS3Client s3 = AWSS3Factory.getS3Client();

            // display object list to user
            bool   moreRecords = true;
            string nextMarker  = string.Empty;

            while (moreRecords)
            {
                // create the request object
                ListObjectsRequest request = new ListObjectsRequest()
                {
                    BucketName = AWSS3Factory.S3_BUCKET,
                    Delimiter  = "/",
                    Prefix     = "january/"
                };

                // if there's a marker from previous request, set it here
                if (nextMarker.Length > 0)
                {
                    request.Marker = nextMarker;
                }

                // get the list of objects
                ListObjectsResponse response = s3.ListObjects(request);

                // display common prefixes
                foreach (string prefix in response.CommonPrefixes)
                {
                    Console.WriteLine(string.Format("Prefix: {0}", prefix));
                }

                // display object keys to user
                foreach (S3Object s3Object in response.S3Objects)
                {
                    Console.WriteLine(string.Format("Object: {0}", s3Object.Key));
                }

                //Console.WriteLine(string.Format("Next Marker: {0}", response.NextMarker));

                // set next marker or exit
                if (response.IsTruncated)
                {
                    nextMarker = response.NextMarker;
                }
                else
                {
                    moreRecords = false;
                }
            }

            Console.ReadLine();
        }
예제 #11
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);
            //PutBucketVersioningRequest req = new PutBucketVersioningRequest()
            //{
            //    BucketName = AWSS3Factory.S3_BUCKET,

            //    VersioningConfig = new S3BucketVersioningConfig()
            //    {
            //        Status = VersionStatus.Enabled
            //    }
            //};

            //PutBucketVersioningResponse res = s3.PutBucketVersioning(req);

            // create bucket request
            PutBucketRequest request = new PutBucketRequest()
            {
                BucketName = AWSS3Factory.S3_BUCKET
            };

            // create bucket - used for subsequent demos
            s3.PutBucket(request);

            // create bucket lising request
            ListObjectsRequest objects = new ListObjectsRequest()
            {
                BucketName = AWSS3Factory.S3_BUCKET
            };

            // get bucket lising to retrieve bucket name
            ListObjectsResponse result = s3.ListObjects(objects);

            // print bucket name for validation
            Console.WriteLine(string.Format("Successfully created bucket {0}.", result.Name));
            Console.ReadLine();
        }
예제 #12
0
        public static void Main(string[] args)
        {
            // create the AWS S3 client
            AmazonS3Client s3 = AWSS3Factory.getS3Client();

            // create the transfer utility using AWS S3 client
            TransferUtility fileTransferUtility = new TransferUtility(s3);

            // retrieve the object key/value from user
            Console.Write("Enter the object key: ");
            string key = Console.ReadLine();

            Console.Write("Enter the file location: ");
            string filePath = Console.ReadLine();

            // configure transfer utility for parallel upload
            TransferUtilityUploadRequest uploadRequest = new TransferUtilityUploadRequest()
            {
                BucketName   = AWSS3Factory.S3_BUCKET,
                FilePath     = filePath,
                StorageClass = S3StorageClass.Standard,
                PartSize     = 1024 * 1024 * 2, // 2MB
                Key          = key,
            };

            // grab the start time of upload
            DateTime startDate = DateTime.Now;

            // upload the file
            fileTransferUtility.Upload(uploadRequest);

            // grab the end time of upload
            DateTime endDate = DateTime.Now;

            Console.WriteLine(string.Format("Completed multi-part upload for object {0}/{1} with file path: {2}", AWSS3Factory.S3_BUCKET, key, filePath));
            Console.WriteLine(string.Format("Process took: {0} seconds.", (endDate - startDate).TotalSeconds.ToString()));

            Console.ReadLine();
        }
        public static void Main(string[] args)
        {
            // create the AWS S3 Client
            AmazonS3Client s3 = AWSS3Factory.getS3Client();

            // retrieve the object key and object content from the user
            Console.Write("Enter the object key: ");
            string key = Console.ReadLine();

            Console.Write("Enter the object content: ");
            string content = Console.ReadLine();

            // retrieve the object metadata key and value from user
            Console.Write("Enter the metadata key: ");
            string metaKey = Console.ReadLine();

            Console.Write("Enter the metadata value: ");
            string metaValue = Console.ReadLine();

            // create object request with retrieved input
            PutObjectRequest request = new PutObjectRequest()
            {
                BucketName  = AWSS3Factory.S3_BUCKET,
                ContentBody = content,
                Key         = key
            };

            // add metadata to request
            request.Metadata.Add(metaKey, metaValue);

            // create the object with metadata in the demo bucket
            s3.PutObject(request);

            // print out object key/value and metadata key/value for validation
            Console.WriteLine(string.Format("Create object {0}/{1} with metadata {2}={3} and content: {4}",
                                            AWSS3Factory.S3_BUCKET, key, metaKey, metaValue, content));
            Console.ReadLine();
        }
예제 #14
0
        public static void Main(string[] args)
        {
            // create the AWS S3 client
            AmazonS3Client s3 = AWSS3Factory.getS3Client();

            // retrieve the object key from user
            Console.Write("Enter the object key: ");
            string key = Console.ReadLine();

            // create the request object
            DeleteObjectRequest request = new DeleteObjectRequest()
            {
                BucketName = AWSS3Factory.S3_BUCKET,
                Key        = key
            };

            // delete the object in the demo bucket
            s3.DeleteObject(request);


            // print out object key/value for validation
            Console.WriteLine(string.Format("Object {0}/{1} deleted", AWSS3Factory.S3_BUCKET, key));
            Console.ReadLine();
        }
예제 #15
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();
        }
예제 #16
0
        public static void Main(string[] args)
        {
            // create the AWS S3 client
            AmazonS3Client s3 = AWSS3Factory.getS3Client();

            // retrieve the object key/value from user
            Console.Write("Enter the object key: ");
            string key = Console.ReadLine();

            Console.Write("Enter the file location: ");
            string filePath = Console.ReadLine();

            // grab the start time of upload
            DateTime startDate = DateTime.Now;

            // part size for chunking in multi-part
            long partSize = 1024 * 1024 * 2; // 2 MB

            // list of upload part response objects for each part that is uploaded
            List <PartETag> partETags = new List <PartETag>();

            // Step 1: Initialize
            InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest()
            {
                BucketName = AWSS3Factory.S3_BUCKET,
                Key        = key,
            };

            InitiateMultipartUploadResponse initResponse = s3.InitiateMultipartUpload(initRequest);

            // get the file and file length
            long contentLength = new FileInfo(filePath).Length;

            Console.WriteLine(string.Format("Starting multi-part upload for object {0}/{1} with file path {2} and size {3} in {4} MB size chunks",
                                            AWSS3Factory.S3_BUCKET, key, filePath, Convert.ToString(contentLength), partSize / 1024 / 1024));

            try
            {
                // Step 2: Upload parts
                long filePosition = 0;
                for (int i = 1; filePosition < contentLength; i++)
                {
                    // get the size of the chunk. Note - the last part can be less than the chunk size
                    partSize = Math.Min(partSize, (contentLength - filePosition));

                    Console.WriteLine(string.Format("Sending chunk {0} starting at position {1}", i, filePosition));

                    // create request to upload  a part
                    UploadPartRequest uploadRequest = new UploadPartRequest()
                    {
                        BucketName   = AWSS3Factory.S3_BUCKET,
                        Key          = key,
                        UploadId     = initResponse.UploadId,
                        PartNumber   = i,
                        FilePosition = filePosition,
                        FilePath     = filePath,
                        PartSize     = partSize
                    };

                    UploadPartResponse partResponse = s3.UploadPart(uploadRequest);
                    PartETag           eTagPart     = new PartETag(partResponse.PartNumber, partResponse.ETag);
                    partETags.Add(eTagPart);

                    filePosition = filePosition += partSize;
                }

                // Step 3: complete
                Console.WriteLine("Waiting for completion of multi-part upload");
                CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest()
                {
                    BucketName = AWSS3Factory.S3_BUCKET,
                    Key        = key,
                    UploadId   = initResponse.UploadId,
                    PartETags  = partETags
                };

                s3.CompleteMultipartUpload(compRequest);
            }
            catch (Exception e)
            {
                s3.AbortMultipartUpload(new AbortMultipartUploadRequest()
                {
                    BucketName = AWSS3Factory.S3_BUCKET,
                    Key        = key,
                    UploadId   = initResponse.UploadId
                });
            }

            // grab the end time of upload
            DateTime endDate = DateTime.Now;

            Console.WriteLine(string.Format("Completed multi-part upload for object {0}/{1} with file path: {2}", AWSS3Factory.S3_BUCKET, key, filePath));
            Console.WriteLine(string.Format("Process took: {0} seconds.", (endDate - startDate).TotalSeconds.ToString()));
            Console.ReadLine();
        }
예제 #17
0
        public static async Task MainSync(string key, string filePath)
        {
            // create the AWS S3 client
            AmazonS3Client s3 = AWSS3Factory.getS3Client();

            // part size for chunking in multi-part
            long partSize = 1024 * 1024 * 2; // 2 MB

            // list of upload part response objects for each part that is uploaded
            IEnumerable <PartETag> partETags = new List <PartETag>();

            // Step 1: Initialize
            InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest()
            {
                BucketName = AWSS3Factory.S3_BUCKET,
                Key        = key,
            };

            // call initialize method -obtain upload id to be used for subsequent parts.
            InitiateMultipartUploadResponse initResponse = s3.InitiateMultipartUpload(initRequest);

            // get the file and file length
            fileSize = new FileInfo(filePath).Length;

            Console.WriteLine(string.Format("Starting multi-part upload for object {0}/{1} with file path {2} and size {3} in {4} MB size chunks",
                                            AWSS3Factory.S3_BUCKET, key, filePath, Convert.ToString(fileSize), partSize / 1024 / 1024));

            try
            {
                // STEP 2: generate list of parts to be uploaded
                long filePosition = 0;

                // the parts list representing each chunk to be uploaded
                List <UploadPartRequest> parts = new List <UploadPartRequest>();

                for (int i = 1; filePosition < fileSize; i++)
                {
                    // get the size of the chunk. Note - the last part can be less than the chunk size
                    partSize = Math.Min(partSize, (fileSize - filePosition));

                    // create request to upload  a part
                    UploadPartRequest uploadRequest = new UploadPartRequest()
                    {
                        BucketName   = AWSS3Factory.S3_BUCKET,
                        Key          = key,
                        UploadId     = initResponse.UploadId,
                        PartNumber   = i,
                        FilePosition = filePosition,
                        FilePath     = filePath,
                        PartSize     = partSize
                    };

                    parts.Add(uploadRequest);

                    filePosition = filePosition += partSize;
                }

                // generate query to simultaneously upload chunks
                IEnumerable <Task <PartETag> > uploadTasksQuery =
                    from part in parts select ProcessChunk(part);

                // execute the upload query
                List <Task <PartETag> > uploadTasks = uploadTasksQuery.ToList();

                //
                // Can do other work here while waiting ...
                Console.WriteLine("Waiting for completion of multi-part upload");
                //
                //

                // wait here for the query to complete
                partETags = await Task.WhenAll(uploadTasks);

                // STEP 3: complete the mpu
                CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest()
                {
                    BucketName = AWSS3Factory.S3_BUCKET,
                    Key        = key,
                    UploadId   = initResponse.UploadId,
                    PartETags  = partETags.ToList()
                };

                s3.CompleteMultipartUpload(compRequest);
            }
            catch (Exception e)
            {
                s3.AbortMultipartUpload(new AbortMultipartUploadRequest()
                {
                    BucketName = AWSS3Factory.S3_BUCKET,
                    Key        = key,
                    UploadId   = initResponse.UploadId
                });
            }
        }
예제 #18
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. Configure bucket lifecycle rules //
            //*************************************//

            Console.Write(string.Format(" [*] Updating lifecycle configuration for bucket '{0}'... ", bucketName));

            LifecycleConfiguration newConfiguration = new LifecycleConfiguration
            {
                Rules = new List <LifecycleRule>
                {
                             // Rule to delete keys with prefix "Test-" after 5 days
                                                       new LifecycleRule
                    {
                        Prefix     = "Test-",
                        Expiration = new LifecycleRuleExpiration {
                            Days = 5
                        },
                        Status = LifecycleRuleStatus.Enabled
                    },
                             // Rule to delete keys in subdirectory "Logs" after 2 days
                                        new LifecycleRule
                    {
                        Prefix     = "Logs/",
                        Expiration = new LifecycleRuleExpiration {
                            Days = 2
                        },
                        Id     = "log-file-removal",
                        Status = LifecycleRuleStatus.Enabled
                    }
                }
            };


            PutLifecycleConfigurationRequest plcReq = new PutLifecycleConfigurationRequest
            {
                BucketName    = bucketName,
                Configuration = newConfiguration
            };
            PutLifecycleConfigurationResponse plcRes = s3.PutLifecycleConfiguration(plcReq);

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

            //************************************************//
            // 3. Retrieve the bucket lifecycle configuration //
            //************************************************//

            Console.Write(string.Format(" [*] Retrieving current lifecycle configuration for bucket '{0}'... ", bucketName));

            GetLifecycleConfigurationResponse glcRes = s3.GetLifecycleConfiguration(bucketName);

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

            Console.WriteLine(String.Format(" [x] Configuration contains {0} rules", glcRes.Configuration.Rules.Count));
            foreach (LifecycleRule rule in glcRes.Configuration.Rules)
            {
                Console.WriteLine(" [x]     Rule:");
                Console.WriteLine(" [x]         Prefix = " + rule.Prefix);
                Console.WriteLine(" [x]         Expiration (days) = " + rule.Expiration.Days);
                Console.WriteLine(" [x]         Id = " + rule.Id);
                Console.WriteLine(" [x]         Status = " + rule.Status);
            }

            //**********************************************//
            // 4. Delete the bucket lifecycle configuration //
            //**********************************************//

            Console.Write(String.Format(" [*] Deleting current lifecycle configuration for bucket '{0}'... ", bucketName));

            DeleteLifecycleConfigurationResponse dlcRes = s3.DeleteLifecycleConfiguration(bucketName);

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


            Console.Write(String.Format(" [*] Verifying current lifecycle rules for bucket '{0}' are empty... ", bucketName));

            LifecycleConfiguration configuration = s3.GetLifecycleConfiguration(bucketName).Configuration;

            if (configuration.Rules.Count != 0)
            {
                Console.WriteLine("fail");
                Console.ReadLine();
                System.Environment.Exit(1);
            }
            Console.WriteLine("done");

            //************************//
            // 5. Delete the bucket   //
            //************************//

            Console.Write(String.Format(" [*] Deleting bucket '{0}'... ", bucketName));

            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();
        }
예제 #19
0
        public static void Main(string[] args)
        {
            // create the AWS S3 client
            AmazonS3Client s3 = AWSS3Factory.getS3Client();

            foreach (string key in KEY_LIST)
            {
                // create object request with retrieved input
                PutObjectRequest request = new PutObjectRequest()
                {
                    BucketName  = AWSS3Factory.S3_BUCKET,
                    ContentBody = key,
                    Key         = key
                };

                // create the object in demo bucket
                s3.PutObject(request);
            }


            while (true)
            {
                Console.Write("Enter the prefix (empty if none): ");
                string prefix = Console.ReadLine();
                Console.Write("Enter the delimiter (e.g. /, empty for none): ");
                string delimiter = Console.ReadLine();
                Console.Write("Enter the marker (empty if none): ");
                string marker = Console.ReadLine();
                Console.Write("Enter the max keys (empty for defaul): ");
                string maxKeys = Console.ReadLine();

                ListObjectsRequest request = new ListObjectsRequest()
                {
                    BucketName = AWSS3Factory.S3_BUCKET
                };

                if (prefix.Length > 0)
                {
                    request.Prefix = prefix;
                }

                if (delimiter.Length > 0)
                {
                    request.Delimiter = delimiter;
                }

                if (marker.Length > 0)
                {
                    request.Marker = marker;
                }

                if (maxKeys.Length > 0)
                {
                    request.MaxKeys = Int32.Parse(maxKeys);
                }

                ListObjectsResponse response = s3.ListObjects(request);

                Console.WriteLine("-----------------");
                Console.WriteLine("Bucket: " + AWSS3Factory.S3_BUCKET);
                Console.WriteLine("Prefix: " + response.Prefix);
                Console.WriteLine("Delimiter: " + response.Delimiter);
                Console.WriteLine("Marker: " + marker);
                Console.WriteLine("IsTruncated? " + response.IsTruncated);
                Console.WriteLine("NextMarker: " + response.NextMarker);

                Console.WriteLine();

                if (response.CommonPrefixes != null)
                {
                    foreach (string commonPrefix in response.CommonPrefixes)
                    {
                        Console.WriteLine("CommonPrefix: " + commonPrefix);
                    }
                }

                Console.WriteLine("Printing objects");
                Console.WriteLine("-----------------");


                foreach (S3Object s3Object in response.S3Objects)
                {
                    Console.WriteLine(String.Format("{0}    {1}     {2}", s3Object.LastModified.ToString(), s3Object.Size, s3Object.Key));
                }

                Console.Write("Another? (Y/N) ");
                string another = Console.ReadLine();

                if (another.ToUpper() == "N")
                {
                    break;
                }
            }

            foreach (string key in KEY_LIST)
            {
                s3.DeleteObject(AWSS3Factory.S3_BUCKET, key);
            }

            Console.ReadLine();
        }
예제 #20
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. Create and upload object        //
            //************************************//

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

            //****************************************//
            // 3. Obtain object metadata              //
            //****************************************//

            Console.Write(string.Format(" [*] Obtaining object size and other metadata for object '{0}'... ", objectKey));

            GetObjectMetadataRequest request = new GetObjectMetadataRequest()
            {
                BucketName = bucketName,
                Key        = objectKey
            };

            // get object metadata - not actual content (HEAD request not GET).
            GetObjectMetadataResponse gomResponse = s3.GetObjectMetadata(request);

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

            Console.WriteLine("done");

            // Obtain the object size in Bytes
            long objectSize = gomResponse.Headers.ContentLength;

            Console.WriteLine(string.Format(" [x] Object size is: {0} bytes", objectSize));

            Console.WriteLine(" [x] Other headers and metadata:");

            ICollection <string> headers = gomResponse.Headers.Keys;

            foreach (string header in headers)
            {
                Console.WriteLine("[x]     {0}: {1}", header, gomResponse.Headers[header]);
            }

            ICollection <string> metaKeys = gomResponse.Metadata.Keys;

            foreach (string metaKey in metaKeys)
            {
                Console.WriteLine("[x]     {0}: {1}", metaKey, gomResponse.Metadata[metaKey]);
            }

            //*******************************************//
            // 4. Delete the object                      //
            //*******************************************//

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

            // create the request object
            DeleteObjectRequest doRequest = new DeleteObjectRequest()
            {
                BucketName = bucketName,
                Key        = objectKey
            };

            // delete the object in the demo bucket
            DeleteObjectResponse doResponse = s3.DeleteObject(doRequest);

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


            Console.WriteLine("done");

            //***********************//
            // 5. 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();
        }
예제 #21
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();
        }