private void Addobject(string filePath, string bucketKey) { try { var attribute = File.GetAttributes(filePath); string relativePath = filePath.Replace(Utilities.Path + "\\", "").Replace("\\", "/"); if (attribute != FileAttributes.Directory && attribute != (FileAttributes.Archive | FileAttributes.Hidden)) { if (File.Exists(filePath) && !Utilities.IsFileUsedbyAnotherProcess(filePath)) { bool IsItSharedFile = false; string Username = string.Empty; if (relativePath.ToLower().IndexOf("shared") == 0) { IsItSharedFile = true; var folders = relativePath.Split('/'); if (folders.Length > 1) { Username = folders[1]; relativePath = ""; for (int i = 2; i < folders.Length; i++) relativePath += folders[i] + "/"; } else { relativePath = ""; for (int i = 1; i < folders.Length; i++) relativePath += folders[i] + "/"; } relativePath = relativePath.Substring(0, relativePath.Length - 1); } var appUpdateInfo = new AppUpdateInfo { Key = relativePath.Replace("\\", "/"), LastModifiedTime = new FileInfo(filePath).LastWriteTime, Status = UpdateStatus.Update }; _processingFiles.Add(filePath); try { var uploadResponses = new List<UploadPartResponse>(); byte[] bytes; long contentLength; 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)); } if (contentLength > 0) { var lastUploadedPartdetail = GetLastUploadedPartResponse(relativePath, bucketKey, uploadResponses); int alreadyUploadedParts = lastUploadedPartdetail.LastPartNumber; string uploadId; if (string.IsNullOrEmpty(lastUploadedPartdetail.UploadId)) { InitiateMultipartUploadRequest initiateRequest = new InitiateMultipartUploadRequest().WithBucketName(bucketKey).WithKey( relativePath); InitiateMultipartUploadResponse initResponse = _amazons3.InitiateMultipartUpload(initiateRequest); uploadId = initResponse.UploadId; } else uploadId = lastUploadedPartdetail.UploadId; try { long partSize = 5 * (long)Math.Pow(2, 20); // 5 MB long filePosition = partSize * alreadyUploadedParts; for (int i = alreadyUploadedParts + 1; filePosition < contentLength; i++) { // Before upload the next set of upload part, need to check the last modified because user might modify it in the mean time if (File.Exists(filePath) && appUpdateInfo.LastModifiedTime == new FileInfo(filePath).LastWriteTime) { 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); UploadPartRequest uploadRequest = new UploadPartRequest() .WithBucketName(bucketKey) .WithKey(relativePath) .WithUploadId(uploadId) .WithPartNumber(i) .WithPartSize(partSize) .WithFilePosition(filePosition) .WithTimeout(1000000000); uploadRequest.WithInputStream(stream); // Upload part and add response to our list. var response = _amazons3.UploadPart(uploadRequest); WriteResponseToFile(relativePath, bucketKey, uploadId, appUpdateInfo.LastModifiedTime, response); uploadResponses.Add(response); filePosition += partSize; ModifySyncStatus("Uploaded", contentLength <= filePosition ? contentLength : filePosition, contentLength, relativePath); } else { // need to abort the upload process _processingFiles.Remove(filePath); RemoveConfig(relativePath, lastUploadedPartdetail.BucketKey); _amazons3.AbortMultipartUpload(new AbortMultipartUploadRequest() .WithBucketName(bucketKey) .WithKey(relativePath) .WithUploadId(uploadId)); return; } } CompleteMultipartUploadRequest completeRequest = new CompleteMultipartUploadRequest() .WithBucketName(bucketKey) .WithKey(relativePath) .WithUploadId(uploadId) .WithPartETags(uploadResponses); CompleteMultipartUploadResponse completeUploadResponse = _amazons3.CompleteMultipartUpload(completeRequest); RemoveConfig(relativePath, completeUploadResponse.BucketName); //_service.AddObject(filePath, Utilities.MyConfig.BucketKey, relativePath); SetAcltoObject(relativePath); if (IsItSharedFile) { ProcessApplicationUpdates(appUpdateInfo, true, Username); } else ProcessApplicationUpdates(appUpdateInfo, false, string.Empty); _processingFiles.Remove(filePath); UploadContentForSearch(filePath, relativePath); } catch (Exception) { _processingFiles.Remove(filePath); _amazons3.AbortMultipartUpload(new AbortMultipartUploadRequest() .WithBucketName(bucketKey) .WithKey(relativePath) .WithUploadId(uploadId)); RemoveConfig(relativePath, Utilities.MyConfig.BucketKey); if (!_fileQueue.ContainsKey(filePath)) _fileQueue.Add(filePath, new FileQueue { Type = WatcherChangeTypes.Created, Name = Path.GetFileName(filePath) }); else _fileQueue.Add(filePath, new FileQueue { Type = WatcherChangeTypes.Created, Name = Path.GetFileName(filePath) }); } } } catch (Exception) { return; } } } } catch (Exception) { return; } }
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)); } }