CompleteMultipartUpload() 공개 메소드

Completes a multipart upload by assembling previously uploaded parts.
public CompleteMultipartUpload ( CompleteMultipartUploadRequest request ) : CompleteMultipartUploadResponse
request Amazon.S3.Model.CompleteMultipartUploadRequest Container for the necessary parameters to execute the CompleteMultipartUpload service method.
리턴 Amazon.S3.Model.CompleteMultipartUploadResponse
        internal static void CreateMultiPartS3Blob(AmazonS3Client client, string key, S3CopyMemoryStream stream)
        {
            if (stream.InitiatingPart) {

                InitiateMultipartUploadRequest initiateMultipartUploadRequest =
                    new InitiateMultipartUploadRequest()
                        .WithBucketName("static.getbrickpile.com")
                        .WithCannedACL(S3CannedACL.PublicRead)
                        .WithKey(key);

                InitiateMultipartUploadResponse initiateResponse =
                    client.InitiateMultipartUpload(initiateMultipartUploadRequest);
                stream.UploadPartId = initiateResponse.UploadId;

            }

            stream.Position = 0;

            UploadPartRequest uploadPartRequest =
                new UploadPartRequest()
                    .WithBucketName("static.getbrickpile.com")
                    .WithKey(key)
                    .WithPartNumber(stream.WriteCount)
                    .WithPartSize(stream.Position)
                    .WithUploadId(stream.UploadPartId)
                    .WithInputStream(stream) as UploadPartRequest;

            UploadPartResponse response = client.UploadPart(uploadPartRequest);
            PartETag etag = new PartETag(response.PartNumber, response.ETag);
            stream.PartETagCollection.Add(etag);

            if (stream.EndOfPart) {

                CompleteMultipartUploadRequest completeMultipartUploadRequest =
                    new CompleteMultipartUploadRequest()
                        .WithBucketName("static.getbrickpile.com")
                        .WithKey(key)
                        .WithPartETags(stream.PartETagCollection)
                        .WithUploadId(stream.UploadPartId);

                CompleteMultipartUploadResponse completeMultipartUploadResponse = client.CompleteMultipartUpload(completeMultipartUploadRequest);
                string loc = completeMultipartUploadResponse.Location;

            }
        }
        public System.Tuple<IEnumerable<IObservable<PartUploadProgressChangedArgs>>, IObservable<bool>> UploadFile(string filePath, string bucket, string keyName)
        {
            var s3Client = new AmazonS3Client(GetCredentials());
            List<UploadPartResponse> uploadResponses = new List<UploadPartResponse>();

            // 1. Initialize.
            InitiateMultipartUploadRequest initRequest =
                new InitiateMultipartUploadRequest()
                .WithBucketName(bucket)
                .WithKey(keyName);

            InitiateMultipartUploadResponse initResponse =
                s3Client.InitiateMultipartUpload(initRequest);

            // 2. Upload Parts.
            long contentLength = new FileInfo(filePath).Length;
            long partSize = 5242880; // 5 MB

            var list = new List<UploadPartRequest>();
            long filePosition = 0;
            int i = 1;
            while (filePosition < contentLength)
            {
                // Create request to upload a part.
                var uploadRequest = new UploadPartRequest()
                    .WithBucketName(bucket)
                    .WithKey(keyName)
                    .WithUploadId(initResponse.UploadId)
                    .WithPartNumber(i)
                    .WithPartSize(partSize)
                    .WithFilePosition(filePosition)
                    .WithFilePath(filePath)
                    .WithTimeout(3600000);

                list.Add(uploadRequest);
                filePosition += partSize;
                ++i;
            }

            var q = list
                //.Select(req => s3Client.UploadPart(req))
                .Select(res => Observable.FromEvent<EventHandler<UploadPartProgressArgs>, PartUploadProgressChangedArgs>(
                            h => (s, e) => h(new PartUploadProgressChangedArgs(res, e)),
                            h => res.UploadPartProgressEvent += h,
                            h => res.UploadPartProgressEvent -= h));
            var q1 = Observable.Start<bool>(() =>
                {
                    try
                    {
                        var prs = Parallel.ForEach(list, res => uploadResponses.Add(s3Client.UploadPart(res)));

                        if (prs.IsCompleted)
                        {
                            CompleteMultipartUploadRequest compRequest =
                                        new CompleteMultipartUploadRequest()
                                        .WithBucketName(bucket)
                                        .WithKey(keyName)
                                        .WithUploadId(initResponse.UploadId)
                                        .WithPartETags(uploadResponses);

                            CompleteMultipartUploadResponse completeUploadResponse =
                                s3Client.CompleteMultipartUpload(compRequest);
                           // MessageBox.Show(completeUploadResponse.ResponseXml);
                            return true;
                        }
                        s3Client.AbortMultipartUpload(new AbortMultipartUploadRequest()
                            .WithBucketName(bucket)
                            .WithKey(keyName)
                            .WithUploadId(initResponse.UploadId));
                        return false;
                    }
                    catch(Exception e)
                    {
                        Console.WriteLine(e.StackTrace);
                        s3Client.AbortMultipartUpload(new AbortMultipartUploadRequest()
                            .WithBucketName(bucket)
                            .WithKey(keyName)
                            .WithUploadId(initResponse.UploadId));
                        return false;
                    }
                });

            //list.ToObservable(Scheduler.ThreadPool)
            //    .Do(res => { uploadResponses.Add(s3Client.UploadPart(res)); })
            //    .Finally(() =>
            //        {
            //            CompleteMultipartUploadRequest compRequest =
            //                new CompleteMultipartUploadRequest()
            //                .WithBucketName(bucket)
            //                .WithKey(keyName)
            //                .WithUploadId(initResponse.UploadId)
            //                .WithPartETags(uploadResponses);

            //            CompleteMultipartUploadResponse completeUploadResponse =
            //                s3Client.CompleteMultipartUpload(compRequest);
            //        })
            //        .Subscribe();
            return Tuple.Create(q, q1);
              //  return list;
            //return Observable.While(() => filePosition < contentLength,
            //    Observable.Return(new UploadPartRequest()
            //        .WithBucketName(bucket)
            //        .WithKey(keyName)
            //        .WithUploadId(initResponse.UploadId)
            //      //  .WithPartNumber(i)
            //      //  .WithPartSize(partSize)
            //      //  .WithFilePosition(filePosition)
            //        .WithFilePath(filePath), Scheduler.ThreadPool)
            //    .Do(req =>
            //    {
            //        req.PartNumber = i;
            //        req.PartSize = partSize;
            //        req.FilePosition = filePosition;
            //        uploadResponses.Add(s3Client.UploadPart(req));
            //        filePosition += partSize;
            //        ++i;
            //    }))
            //    //.Do(req =>
            //    //    {
            //    //        uploadResponses.Add(s3Client.UploadPart(req));
            //    //    })
            //    .Finally(() =>
            //    {
            //        CompleteMultipartUploadRequest compRequest =
            //        new CompleteMultipartUploadRequest()
            //        .WithBucketName(bucket)
            //        .WithKey(keyName)
            //        .WithUploadId(initResponse.UploadId)
            //        .WithPartETags(uploadResponses);

            //            CompleteMultipartUploadResponse completeUploadResponse =
            //                s3Client.CompleteMultipartUpload(compRequest);
            //    });
            //while (filePosition < contentLength)
            //{
            //    // Create request to upload a part.
            //    var uploadRequest = new UploadPartRequest()
            //        .WithBucketName(bucket)
            //        .WithKey(keyName)
            //        .WithUploadId(initResponse.UploadId)
            //        .WithPartNumber(i)
            //        .WithPartSize(partSize)
            //        .WithFilePosition(filePosition)
            //        .WithFilePath(filePath);

            //    yield return uploadRequest;

            //    uploadResponses.Add(s3Client.UploadPart(uploadRequest));

            //    filePosition += partSize;
            //    ++i;
            //}

            ////Observable.FromEvent<EventHandler<UploadPartProgressArgs>, PartUploadProgressChangedArgs>(
            ////    h => (s, e) => h(new PartUploadProgressChangedArgs(uploadRequest, e)),
            ////    h => uploadRequest.UploadPartProgressEvent += h,
            ////    h => uploadRequest.UploadPartProgressEvent -= h);
            //// Upload part and add response to our list.

            //// Step 3: complete.
            //CompleteMultipartUploadRequest compRequest =
            //    new CompleteMultipartUploadRequest()
            //    .WithBucketName(bucket)
            //    .WithKey(keyName)
            //    .WithUploadId(initResponse.UploadId)
            //    .WithPartETags(uploadResponses);

            //CompleteMultipartUploadResponse completeUploadResponse =
            //    s3Client.CompleteMultipartUpload(compRequest);

            //try
            //{

            //}
            //catch (Exception exception)
            //{
            //    Console.WriteLine("Exception occurred: {0}", exception.Message);
            //    s3Client.AbortMultipartUpload(new AbortMultipartUploadRequest()
            //        .WithBucketName(bucket)
            //        .WithKey(keyName)
            //        .WithUploadId(initResponse.UploadId));
            //}
            //return list.ToObservable();
        }
예제 #3
-1
        static void Main()
        {
            _accessKeyId = Utilities.AwsAccessKey;
            _secretAccessKey = Utilities.AwsSecretKey;
            AmazonS3 s3Client = new AmazonS3Client(_accessKeyId, _secretAccessKey);
            ListMultipartUploadsRequest allMultipartUploadsRequest = new ListMultipartUploadsRequest().WithBucketName(ExistingBucketName);
            ListMultipartUploadsResponse mpUploadsResponse = s3Client.ListMultipartUploads(allMultipartUploadsRequest);
            var objects = new List<Object>();
            foreach (MultipartUpload multipartUpload in mpUploadsResponse.MultipartUploads)
            {
                bool isObjectdFound = false;
                foreach (Object o in objects)
                {
                    if (o.UploadId == multipartUpload.UploadId)
                    {
                        o.Parts.Add(new Part { PartId = o.Parts.Count, Etag = "" });
                        isObjectdFound = true;
                    }
                }
                if (!isObjectdFound)
                {
                    objects.Add(new Object { Parts = new List<Part> { new Part() { Etag = "", PartId = 1 } }, UploadId = multipartUpload.UploadId });
                }
            }
            var result = JsonConvert.SerializeObject(objects);
            var objs = JsonConvert.DeserializeObject<List<Object>>(result);
            //return;

            // List to store upload part responses.
            var uploadResponses = new List<UploadPartResponse>();
            byte[] bytes;
            long contentLength = 0;
            using (var fileStream = new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                contentLength = fileStream.Length;
                bytes = new byte[contentLength];
                fileStream.Read(bytes, 0, Convert.ToInt32(contentLength));
            }
            // 1. Initialize.
            InitiateMultipartUploadRequest initiateRequest = new InitiateMultipartUploadRequest().WithBucketName(ExistingBucketName).WithKey(KeyName);
            InitiateMultipartUploadResponse initResponse = s3Client.InitiateMultipartUpload(initiateRequest);
            try
            {
                // 2. Upload Parts.
                long partSize = 5 * (long)Math.Pow(2, 20); // 5 MB
                long filePosition = 0;
                for (int i = 1; filePosition < contentLength; i++)
                {
                    byte[] bytesToStream;
                    if (filePosition + partSize < contentLength)
                    {
                        bytesToStream = new byte[partSize];
                        Array.Copy(bytes, filePosition, bytesToStream, 0, partSize);
                    }
                    else
                    {
                        bytesToStream = new byte[contentLength - filePosition];
                        Array.Copy(bytes, filePosition, bytesToStream, 0, contentLength - filePosition);
                    }

                    Stream stream = new MemoryStream(bytesToStream);
                    // Create request to upload a part.
                    UploadPartRequest uploadRequest = new UploadPartRequest()
                        .WithBucketName(ExistingBucketName)
                        .WithKey(KeyName)
                        .WithUploadId(initResponse.UploadId)
                        .WithPartNumber(i)
                        .WithPartSize(partSize)
                        .WithFilePosition(filePosition)
                        .WithTimeout(1000000000)
                        .WithMD5Digest(Convert.ToBase64String(MD5.Create().ComputeHash(bytesToStream)));
                    uploadRequest.WithInputStream(stream);
                    // Upload part and add response to our list.
                    uploadResponses.Add(s3Client.UploadPart(uploadRequest));
                    filePosition += partSize;
                }
                // Step 3: complete.
                CompleteMultipartUploadRequest completeRequest =
                    new CompleteMultipartUploadRequest()
                    .WithBucketName(ExistingBucketName)
                    .WithKey(KeyName)
                    .WithUploadId(initResponse.UploadId)
                    .WithPartETags(uploadResponses);

                CompleteMultipartUploadResponse completeUploadResponse = s3Client.CompleteMultipartUpload(completeRequest);
                Console.WriteLine(completeUploadResponse.ETag);
            }
            catch (Exception exception)
            {
                Console.WriteLine("Exception occurred: {0}", exception.Message);
                s3Client.AbortMultipartUpload(new AbortMultipartUploadRequest()
                    .WithBucketName(ExistingBucketName)
                    .WithKey(KeyName)
                    .WithUploadId(initResponse.UploadId));
            }
        }
예제 #4
-1
        public void ObjectSamples()
        {
            {
                #region ListObjects Sample

                // Create a client
                AmazonS3Client client = new AmazonS3Client();

                // List all objects
                ListObjectsRequest listRequest = new ListObjectsRequest
                {
                    BucketName = "SampleBucket",
                };

                ListObjectsResponse listResponse;
                do
                {
                    // Get a list of objects
                    listResponse = client.ListObjects(listRequest);
                    foreach (S3Object obj in listResponse.S3Objects)
                    {
                        Console.WriteLine("Object - " + obj.Key);
                        Console.WriteLine(" Size - " + obj.Size);
                        Console.WriteLine(" LastModified - " + obj.LastModified);
                        Console.WriteLine(" Storage class - " + obj.StorageClass);
                    }

                    // Set the marker property
                    listRequest.Marker = listResponse.NextMarker;
                } while (listResponse.IsTruncated);

                #endregion
            }

            {
                #region GetObject Sample

                // Create a client
                AmazonS3Client client = new AmazonS3Client();

                // Create a GetObject request
                GetObjectRequest request = new GetObjectRequest
                {
                    BucketName = "SampleBucket",
                    Key = "Item1"
                };

                // Issue request and remember to dispose of the response
                using (GetObjectResponse response = client.GetObject(request))
                {
                    using (StreamReader reader = new StreamReader(response.ResponseStream))
                    {
                        string contents = reader.ReadToEnd();
                        Console.WriteLine("Object - " + response.Key);
                        Console.WriteLine(" Version Id - " + response.VersionId);
                        Console.WriteLine(" Contents - " + contents);
                    }
                }

                #endregion
            }

            {
                #region GetObjectMetadata Sample

                // Create a client
                AmazonS3Client client = new AmazonS3Client();


                // Create a GetObjectMetadata request
                GetObjectMetadataRequest request = new GetObjectMetadataRequest
                {
                    BucketName = "SampleBucket",
                    Key = "Item1"
                };

                // Issue request and view the response
                GetObjectMetadataResponse response = client.GetObjectMetadata(request);
                Console.WriteLine("Content Length - " + response.ContentLength);
                Console.WriteLine("Content Type - " + response.Headers.ContentType);
                if (response.Expiration != null)
                {
                    Console.WriteLine("Expiration Date - " + response.Expiration.ExpiryDate);
                    Console.WriteLine("Expiration Rule Id - " + response.Expiration.RuleId);
                }

                #endregion
            }

            {
                #region PutObject Sample 1

                // Create a client
                AmazonS3Client client = new AmazonS3Client();

                // Create a PutObject request
                PutObjectRequest request = new PutObjectRequest
                {
                    BucketName = "SampleBucket",
                    Key = "Item1",
                    ContentBody = "This is sample content..."
                };

                // Put object
                PutObjectResponse response = client.PutObject(request);

                #endregion
            }

            {
                #region PutObject Sample 2

                // Create a client
                AmazonS3Client client = new AmazonS3Client();

                // Create a PutObject request
                PutObjectRequest request = new PutObjectRequest
                {
                    BucketName = "SampleBucket",
                    Key = "Item1",
                    FilePath = "contents.txt"
                };

                // Put object
                PutObjectResponse response = client.PutObject(request);

                #endregion
            }

            {
                #region PutObject Sample 3

                // Create a client
                AmazonS3Client client = new AmazonS3Client();

                // Create a PutObject request
                PutObjectRequest request = new PutObjectRequest
                {
                    BucketName = "SampleBucket",
                    Key = "Item1",
                };
                using (FileStream stream = new FileStream("contents.txt", FileMode.Open))
                {
                    request.InputStream = stream;

                    // Put object
                    PutObjectResponse response = client.PutObject(request);
                }

                #endregion
            }

            {
                #region DeleteObject Sample

                // Create a client
                AmazonS3Client client = new AmazonS3Client();

                // Create a DeleteObject request
                DeleteObjectRequest request = new DeleteObjectRequest
                {
                    BucketName = "SampleBucket",
                    Key = "Item1"
                };

                // Issue request
                client.DeleteObject(request);

                #endregion
            }

            {
                #region DeleteObjects Sample

                // Create a client
                AmazonS3Client client = new AmazonS3Client();

                // Create a DeleteObject request
                DeleteObjectsRequest request = new DeleteObjectsRequest
                {
                    BucketName = "SampleBucket",
                    Objects = new List<KeyVersion>
                    {
                        new KeyVersion() {Key = "Item1"},
                        // Versioned item
                        new KeyVersion() { Key = "Item2", VersionId = "Rej8CiBxcZKVK81cLr39j27Y5FVXghDK", },
                        // Item in subdirectory
                        new KeyVersion() { Key = "Logs/error.txt"}
                    }
                };

                try
                {
                    // Issue request
                    DeleteObjectsResponse response = client.DeleteObjects(request);
                }
                catch (DeleteObjectsException doe)
                {
                    // Catch error and list error details
                    DeleteObjectsResponse errorResponse = doe.Response;

                    foreach (DeletedObject deletedObject in errorResponse.DeletedObjects)
                    {
                        Console.WriteLine("Deleted item " + deletedObject.Key);
                    }
                    foreach (DeleteError deleteError in errorResponse.DeleteErrors)
                    {
                        Console.WriteLine("Error deleting item " + deleteError.Key);
                        Console.WriteLine(" Code - " + deleteError.Code);
                        Console.WriteLine(" Message - " + deleteError.Message);
                    }
                }

                #endregion
            }

            {
                #region CopyObject Sample

                // Create a client
                AmazonS3Client client = new AmazonS3Client();

                // Create a CopyObject request
                CopyObjectRequest request = new CopyObjectRequest
                {
                    SourceBucket = "SampleBucket",
                    SourceKey = "Item1",
                    DestinationBucket = "AnotherBucket",
                    DestinationKey = "Copy1",
                    CannedACL = S3CannedACL.PublicRead
                };

                // Issue request
                client.CopyObject(request);

                #endregion
            }

            {
                #region CopyObject Sample

                // Create a client
                AmazonS3Client client = new AmazonS3Client();

                // Create a CopyObject request
                CopyObjectRequest request = new CopyObjectRequest
                {
                    SourceBucket = "SampleBucket",
                    SourceKey = "Item1",
                    DestinationBucket = "AnotherBucket",
                    DestinationKey = "Copy1",
                    CannedACL = S3CannedACL.PublicRead
                };

                // Issue request
                client.CopyObject(request);

                #endregion
            }

            {
                #region ListVersions Sample

                // Create a client
                AmazonS3Client client = new AmazonS3Client();

                // Turn versioning on for a bucket
                client.PutBucketVersioning(new PutBucketVersioningRequest
                {
                    BucketName = "SampleBucket",
                    VersioningConfig = new S3BucketVersioningConfig { Status = "Enable" }
                });

                // Populate bucket with multiple items, each with multiple versions
                PopulateBucket(client, "SampleBucket");

                // Get versions
                ListVersionsRequest request = new ListVersionsRequest
                {
                    BucketName = "SampleBucket"
                };

                // Make paged ListVersions calls
                ListVersionsResponse response;
                do
                {
                    response = client.ListVersions(request);
                    // View information about versions
                    foreach (var version in response.Versions)
                    {
                        Console.WriteLine("Key = {0}, Version = {1}, IsLatest = {2}, LastModified = {3}, Size = {4}",
                            version.Key,
                            version.VersionId,
                            version.IsLatest,
                            version.LastModified,
                            version.Size);
                    }

                    request.KeyMarker = response.NextKeyMarker;
                    request.VersionIdMarker = response.NextVersionIdMarker;
                } while (response.IsTruncated);

                #endregion
            }

            {
                #region Multipart Upload Sample

                int MB = (int)Math.Pow(2, 20);

                // Create a client
                AmazonS3Client client = new AmazonS3Client();

                // Define input stream
                Stream inputStream = Create13MBDataStream();

                // Initiate multipart upload
                InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest
                {
                    BucketName = "SampleBucket",
                    Key = "Item1"
                };
                InitiateMultipartUploadResponse initResponse = client.InitiateMultipartUpload(initRequest);

                // Upload part 1
                UploadPartRequest uploadRequest = new UploadPartRequest
                {
                    BucketName = "SampleBucket",
                    Key = "Item1",
                    UploadId = initResponse.UploadId,
                    PartNumber = 1,
                    PartSize = 5 * MB,
                    InputStream = inputStream
                };
                UploadPartResponse up1Response = client.UploadPart(uploadRequest);

                // Upload part 2
                uploadRequest = new UploadPartRequest
                {
                    BucketName = "SampleBucket",
                    Key = "Item1",
                    UploadId = initResponse.UploadId,
                    PartNumber = 2,
                    PartSize = 5 * MB,
                    InputStream = inputStream
                };
                UploadPartResponse up2Response = client.UploadPart(uploadRequest);

                // Upload part 3
                uploadRequest = new UploadPartRequest
                {
                    BucketName = "SampleBucket",
                    Key = "Item1",
                    UploadId = initResponse.UploadId,
                    PartNumber = 3,
                    InputStream = inputStream
                };
                UploadPartResponse up3Response = client.UploadPart(uploadRequest);

                // List parts for current upload
                ListPartsRequest listPartRequest = new ListPartsRequest
                {
                    BucketName = "SampleBucket",
                    Key = "Item1",
                    UploadId = initResponse.UploadId
                };
                ListPartsResponse listPartResponse = client.ListParts(listPartRequest);
                Debug.Assert(listPartResponse.Parts.Count == 3);

                // Complete the multipart upload
                CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest
                {
                    BucketName = "SampleBucket",
                    Key = "Item1",
                    UploadId = initResponse.UploadId,
                    PartETags = new List<PartETag>
                    {
                        new PartETag { ETag = up1Response.ETag, PartNumber = 1 },
                        new PartETag { ETag = up2Response.ETag, PartNumber = 2 },
                        new PartETag { ETag = up3Response.ETag, PartNumber = 3 }
                    }
                };
                CompleteMultipartUploadResponse compResponse = client.CompleteMultipartUpload(compRequest);

                #endregion
            }
        }