Пример #1
0
        /// <summary>
        /// Uploads stream to given folder and document name.
        /// </summary>
        /// <param name="uploadDocumentStreamRequest">Request parameters.</param>
        /// <returns>Response for document details.</returns>
        public async Task <UploadDocumentStreamResponse> UploadDocumentStreamAsync(UploadDocumentStreamRequest uploadDocumentStreamRequest)
        {
            InitiateDocumentVersionUploadRequest initiateDocumentVersionUploadRequest = new InitiateDocumentVersionUploadRequest()
            {
                ParentFolderId           = uploadDocumentStreamRequest.ParentFolderId,
                Name                     = uploadDocumentStreamRequest.DocumentName,
                ContentType              = uploadDocumentStreamRequest.ContentType,
                ContentCreatedTimestamp  = uploadDocumentStreamRequest.ContentCreatedTimestamp,
                ContentModifiedTimestamp = uploadDocumentStreamRequest.ContentModifiedTimestamp,
                DocumentSizeInBytes      = uploadDocumentStreamRequest.DocumentSizeInBytes,
                Id = uploadDocumentStreamRequest.DocumentId,
                AuthenticationToken = authenticationToken
            };

            InitiateDocumentVersionUploadResponse response = await client.InitiateDocumentVersionUploadAsync(initiateDocumentVersionUploadRequest).ConfigureAwait(false);

            UploadMetadata uploadMetadata = response.UploadMetadata;
            String         documentId     = response.Metadata.Id;
            String         versionId      = response.Metadata.LatestVersionMetadata.Id;
            String         uploadUrl      = uploadMetadata.UploadUrl;

            AWSStreamContent content = new AWSStreamContent(uploadDocumentStreamRequest.Stream);

            try
            {
                IDictionary <string, string> requestHeaders = new Dictionary <string, string>();
                requestHeaders.Add("x-amz-server-side-encryption", "AES256");

                content.RemoveHttpContentHeader("Content-Type");
                content.AddHttpContentHeader("Content-Type", uploadDocumentStreamRequest.ContentType);

                await httpClient.PutRequestUriAsync(uploadUrl, content, requestHeaders);

                var task = CompleteUpload(documentId, versionId);

                UploadDocumentStreamResponse uploadDocumentStreamResponse = new UploadDocumentStreamResponse(uploadDocumentStreamRequest)
                {
                    DocumentId = task.Result,
                    VersionId  = versionId
                };

                return(uploadDocumentStreamResponse);
            }
            finally
            {
                content.Dispose();
            }
        }
Пример #2
0
        /// <summary>
        /// Uploads stream to given folder and document name.
        /// </summary>
        /// <param name="uploadDocumentStreamRequest">Request parameters.</param>
        /// <returns>Response for document details.</returns>
        public async Task <UploadDocumentStreamResponse> UploadDocumentStreamAsync(UploadDocumentStreamRequest uploadDocumentStreamRequest)
        {
            InitiateDocumentVersionUploadRequest initiateDocumentVersionUploadRequest = new InitiateDocumentVersionUploadRequest()
            {
                ParentFolderId           = uploadDocumentStreamRequest.ParentFolderId,
                Name                     = uploadDocumentStreamRequest.DocumentName,
                ContentType              = uploadDocumentStreamRequest.ContentType,
                ContentCreatedTimestamp  = uploadDocumentStreamRequest.ContentCreatedTimestamp,
                ContentModifiedTimestamp = uploadDocumentStreamRequest.ContentModifiedTimestamp,
                DocumentSizeInBytes      = uploadDocumentStreamRequest.DocumentSizeInBytes,
                Id = uploadDocumentStreamRequest.DocumentId,
                AuthenticationToken = authenticationToken
            };

            InitiateDocumentVersionUploadResponse response = await client.InitiateDocumentVersionUploadAsync(initiateDocumentVersionUploadRequest).ConfigureAwait(false);

            UploadMetadata uploadMetadata = response.UploadMetadata;
            String         documentId     = response.Metadata.Id;
            String         versionId      = response.Metadata.LatestVersionMetadata.Id;
            String         uploadUrl      = uploadMetadata.UploadUrl;

            try
            {
                httpClient.DefaultRequestHeaders.Add("x-amz-server-side-encryption", "AES256");

                StreamContent content = new StreamContent(uploadDocumentStreamRequest.Stream);
                content.Headers.Remove("Content-Type");
                content.Headers.Add("Content-Type", uploadDocumentStreamRequest.ContentType);

                Task <HttpResponseMessage> responseTask = httpClient.PutAsync(uploadUrl, content);
                Task <Task <String> >      task         = responseTask.ContinueWith(t => CompleteUpload(documentId, versionId));

                UploadDocumentStreamResponse uploadDocumentStreamResponse = new UploadDocumentStreamResponse(uploadDocumentStreamRequest)
                {
                    DocumentId = task.Result.Result,
                    VersionId  = versionId
                };

                return(uploadDocumentStreamResponse);
            }
            finally
            {
                httpClient.DefaultRequestHeaders.Remove("x-amz-server-side-encryption");
            }
        }