private async Task <int> DeleteSessionFilesAsync(Guid sessionId, IEnumerable <S3Object> s3Objects) { var count = 0; foreach (var obj in s3Objects) { await _cephS3Client.DeleteObjectAsync(_filesBucketName, obj.Key); if (obj.Key.EndsWith(Tokens.SessionPostfix)) { _removedSessionsMetric.Inc(); } else if (!obj.Key.EndsWith("/")) { _removedBinariesMetric.Inc(); } _logger.LogInformation("File {fileKey} deleted while archiving or deleting session '{sessionId}'.", obj.Key, sessionId); ++count; } return(count); }
public async Task <string> CompleteMultipartUpload(MultipartUploadSession uploadSession) { var uploadKey = uploadSession.SessionId.AsS3ObjectKey(uploadSession.FileKey); var partETags = uploadSession.Parts.Select(x => new PartETag(x.PartNumber, x.Etag)).ToList(); var uploadResponse = await _cephS3Client.CompleteMultipartUploadAsync( new CompleteMultipartUploadRequest { BucketName = _filesBucketName, Key = uploadKey, UploadId = uploadSession.UploadId, PartETags = partETags }); uploadSession.Complete(); if (SessionDescriptor.IsSessionExpired(uploadSession.SessionExpiresAt)) { throw new SessionExpiredException(uploadSession.SessionId, uploadSession.SessionExpiresAt); } try { using (var getResponse = await _cephS3Client.GetObjectAsync(_filesBucketName, uploadKey)) { var memoryStream = new MemoryStream(); using (getResponse.ResponseStream) { getResponse.ResponseStream.CopyTo(memoryStream); memoryStream.Position = 0; } using (memoryStream) { var sessionDescriptor = uploadSession.SessionDescriptor; var elementDescriptor = uploadSession.ElementDescriptor; EnsureFileContentIsValid( elementDescriptor.TemplateCode, elementDescriptor.Type, elementDescriptor.Constraints.For(sessionDescriptor.Language), memoryStream, uploadSession.UploadedFileMetadata); } var metadataWrapper = MetadataCollectionWrapper.For(getResponse.Metadata); var fileName = metadataWrapper.Read <string>(MetadataElement.Filename); var fileExtension = Path.GetExtension(fileName).ToLowerInvariant(); var fileKey = Path.ChangeExtension(uploadSession.SessionId.AsS3ObjectKey(uploadResponse.ETag), fileExtension); var copyRequest = new CopyObjectRequest { ContentType = uploadSession.UploadedFileMetadata.ContentType, SourceBucket = _filesBucketName, SourceKey = uploadKey, DestinationBucket = _filesBucketName, DestinationKey = fileKey, MetadataDirective = S3MetadataDirective.REPLACE, CannedACL = S3CannedACL.PublicRead }; foreach (var metadataKey in getResponse.Metadata.Keys) { copyRequest.Metadata.Add(metadataKey, getResponse.Metadata[metadataKey]); } await _cephS3Client.CopyObjectAsync(copyRequest); _uploadedBinariesMetric.Inc(); _memoryCache.Set(fileKey, new BinaryMetadata(fileName, getResponse.ContentLength), uploadSession.SessionExpiresAt); return(fileKey); } } finally { await _cephS3Client.DeleteObjectAsync(_filesBucketName, uploadKey); } }