public UploadDocumentStreamResult uploadDocumentStream ( UploadDocumentStreamRequest uploadDocumentStreamRequest ) { InputStream stream = uploadDocumentStreamRequest . getStream ( ) ; if ( stream == null ) { throw new IllegalArgumentException ( "InputStream must be specified." ) ; } InitiateDocumentVersionUploadRequest initiateDocumentVersionUploadRequest = new InitiateDocumentVersionUploadRequest ( ) ; initiateDocumentVersionUploadRequest . setParentFolderId ( uploadDocumentStreamRequest . getParentFolderId ( ) ) ; initiateDocumentVersionUploadRequest . setName ( uploadDocumentStreamRequest . getDocumentName ( ) ) ; initiateDocumentVersionUploadRequest . setContentType ( uploadDocumentStreamRequest . getContentType ( ) ) ; initiateDocumentVersionUploadRequest . setContentCreatedTimestamp ( uploadDocumentStreamRequest . getContentCreatedTimestamp ( ) ) ; initiateDocumentVersionUploadRequest . setContentModifiedTimestamp ( uploadDocumentStreamRequest . getContentModifiedTimestamp ( ) ) ; initiateDocumentVersionUploadRequest . setDocumentSizeInBytes ( uploadDocumentStreamRequest . getDocumentSizeInBytes ( ) ) ; initiateDocumentVersionUploadRequest . setId ( uploadDocumentStreamRequest . getDocumentId ( ) ) ; String requestAuthenticationToken = uploadDocumentStreamRequest . getAuthenticationToken ( ) ; if ( requestAuthenticationToken != null ) { initiateDocumentVersionUploadRequest . setAuthenticationToken ( requestAuthenticationToken ) ; } else { initiateDocumentVersionUploadRequest . setAuthenticationToken ( authenticationToken ) ; } InitiateDocumentVersionUploadResult result = client . initiateDocumentVersionUpload ( initiateDocumentVersionUploadRequest ) ; UploadMetadata uploadMetadata = result . getUploadMetadata ( ) ; String documentId = result . getMetadata ( ) . getId ( ) ; String versionId = result . getMetadata ( ) . getLatestVersionMetadata ( ) . getId ( ) ; String uploadUrl = uploadMetadata . getUploadUrl ( ) ; try { URL url = new URL ( uploadUrl ) ; HttpURLConnection connection = ( HttpURLConnection ) url . openConnection ( ) ; connection . setDoOutput ( true ) ; connection . setRequestMethod ( "PUT" ) ; connection . setRequestProperty ( "Content-Type" , uploadDocumentStreamRequest . getContentType ( ) ) ; connection . setRequestProperty ( "x-amz-server-side-encryption" , "AES256" ) ; OutputStream outputStream = connection . getOutputStream ( ) ; IOUtils . copy ( stream , outputStream ) ; connection . getResponseCode ( ) ; } catch ( MalformedURLException e ) { throw new SdkClientException ( e ) ; } catch ( IOException e ) { throw new SdkClientException ( e ) ; } UpdateDocumentVersionRequest updateDocumentVersionRequest = new UpdateDocumentVersionRequest ( ) ; updateDocumentVersionRequest . setDocumentId ( documentId ) ; updateDocumentVersionRequest . setVersionId ( versionId ) ; updateDocumentVersionRequest . setVersionStatus ( DocumentVersionStatus . ACTIVE ) ; if ( authenticationToken != null ) { updateDocumentVersionRequest . setAuthenticationToken ( authenticationToken ) ; } UpdateDocumentVersionResult updateDocumentVersionResult = client . updateDocumentVersion ( updateDocumentVersionRequest ) ; UploadDocumentStreamResult uploadDocumentStreamResult = new UploadDocumentStreamResult ( uploadDocumentStreamRequest ) ; uploadDocumentStreamResult . setDocumentId ( documentId ) ; uploadDocumentStreamResult . setVersionId ( versionId ) ; return uploadDocumentStreamResult ; }
Uploads document stream to WorkDocs . If document ID is specified then it creates a new version under this document . If document ID is not specified then it creates a new document and uploads content to its only version . \ Client must close the input stream once upload operation is complete .