Title: ChunkedUploadProvider for AttachmentItem halts after first chunk · Issue #423 · microsoftgraph/msgraph-sdk-java · GitHub
Open Graph Title: ChunkedUploadProvider for AttachmentItem halts after first chunk · Issue #423 · microsoftgraph/msgraph-sdk-java
X Title: ChunkedUploadProvider for AttachmentItem halts after first chunk · Issue #423 · microsoftgraph/msgraph-sdk-java
Description: Uploading chunks return reponse code 200 where the ChunkedUploadResponseHandler awaits 202. That finishes the chunked upload immediately and the upload will not finish with a 201 Created. You can get it to work if you extend the ChunkedU...
Open Graph Description: Uploading chunks return reponse code 200 where the ChunkedUploadResponseHandler awaits 202. That finishes the chunked upload immediately and the upload will not finish with a 201 Created. You can g...
X Description: Uploading chunks return reponse code 200 where the ChunkedUploadResponseHandler awaits 202. That finishes the chunked upload immediately and the upload will not finish with a 201 Created. You can g...
Opengraph URL: https://github.com/microsoftgraph/msgraph-sdk-java/issues/423
X: @github
Domain: github.com
IPv4 address: 140.82.112.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"ChunkedUploadProvider for AttachmentItem halts after first chunk ","articleBody":"Uploading chunks return reponse code 200 where the ChunkedUploadResponseHandler awaits 202. That finishes the chunked upload immediately and the upload will not finish with a 201 Created.\r\nYou can get it to work if you extend the ChunkedUploadResponseHandler and overwrite the Response Code and result return:\r\npackage com.example.ms.graph;\r\n\r\n```Java\r\nimport java.io.BufferedInputStream;\r\nimport java.io.IOException;\r\nimport java.io.InputStream;\r\n\r\nimport com.microsoft.graph.concurrency.ChunkedUploadResponseHandler;\r\nimport com.microsoft.graph.http.DefaultHttpProvider;\r\nimport com.microsoft.graph.http.GraphServiceException;\r\nimport com.microsoft.graph.http.HttpResponseCode;\r\nimport com.microsoft.graph.http.IConnection;\r\nimport com.microsoft.graph.http.IHttpRequest;\r\nimport com.microsoft.graph.logger.ILogger;\r\nimport com.microsoft.graph.models.extensions.AttachmentItem;\r\nimport com.microsoft.graph.models.extensions.UploadSession;\r\nimport com.microsoft.graph.requests.extensions.ChunkedUploadResult;\r\nimport com.microsoft.graph.serializer.ISerializer;\r\n\r\nimport okhttp3.Response;\r\n\r\npublic class ChunkedUploadResponseHandler2\u003cUploadType> extends ChunkedUploadResponseHandler\u003cUploadType> {\r\n\r\n\tprivate final Class\u003cUploadType> deserializeTypeClass;\r\n\r\n\tpublic ChunkedUploadResponseHandler2(Class\u003cUploadType> deserializeTypeClass) {\r\n\t\tsuper(deserializeTypeClass);\r\n\t\tthis.deserializeTypeClass = deserializeTypeClass;\r\n\t}\r\n\r\n\t/**\r\n\t * Generate the chunked upload response result\r\n\t *\r\n\t * @param request\r\n\t * the HTTP request\r\n\t * @param response\r\n\t * the HTTP response\r\n\t * @param serializer\r\n\t * the serializer\r\n\t * @param logger\r\n\t * the system logger\r\n\t * @return the chunked upload result, which could be either an uploaded item or error\r\n\t * @throws Exception\r\n\t * an exception occurs if the request was unable to complete for any reason\r\n\t */\r\n\t@Override\r\n\tpublic ChunkedUploadResult\u003cUploadType> generateResult(final IHttpRequest request, final Response response, final ISerializer serializer, final ILogger logger)\r\n\t\t\tthrows Exception {\r\n\t\tInputStream in = null;\r\n\t\ttry {\r\n\t\t\tif (response.code() == HttpResponseCode.HTTP_ACCEPTED || response.code() == HttpResponseCode.HTTP_OK) {\r\n\t\t\t\tlogger.logDebug(\"Chunk bytes has been accepted by the server.\");\r\n\t\t\t\tin = new BufferedInputStream(response.body().byteStream());\r\n\t\t\t\tfinal UploadSession session = serializer.deserializeObject(DefaultHttpProvider.streamToString(in), UploadSession.class);\r\n\r\n\t\t\t\treturn new ChunkedUploadResult\u003cUploadType>(session);\r\n\r\n\t\t\t}\r\n\t\t\telse if (response.code() == HttpResponseCode.HTTP_CREATED) { // || response.code() == HttpResponseCode.HTTP_OK\r\n\t\t\t\tlogger.logDebug(\"Upload session is completed, uploaded item returned.\");\r\n\t\t\t\t// in = new BufferedInputStream(response.body().byteStream());\r\n\t\t\t\t// String rawJson = DefaultHttpProvider.streamToString(in);\r\n\t\t\t\t// UploadType uploadedItem = serializer.deserializeObject(rawJson, this.deserializeTypeClass);\r\n\t\t\t\tString itemUrl = response.header(\"Location\");\r\n\t\t\t\tlogger.logDebug(String.format(\"Upload session is completed, upload url: %s\", itemUrl));\r\n\t\t\t\tAttachmentItem ai = new AttachmentItem();\r\n\t\t\t\tai.name = itemUrl;\r\n\t\t\t\tUploadType uploadedItem = (UploadType) ai;\r\n\t\t\t\treturn new ChunkedUploadResult\u003cUploadType>(uploadedItem);\r\n\t\t\t}\r\n\t\t\telse if (response.code() >= HttpResponseCode.HTTP_CLIENT_ERROR) {\r\n\t\t\t\tlogger.logDebug(\"Receiving error during upload, see detail on result error\");\r\n\r\n\t\t\t\treturn new ChunkedUploadResult\u003cUploadType>(GraphServiceException.createFromConnection(request, null, serializer, response, logger));\r\n\t\t\t}\r\n\t\t}\r\n\t\tfinally {\r\n\t\t\tif (in != null) {\r\n\t\t\t\ttry {\r\n\t\t\t\t\tin.close();\r\n\t\t\t\t}\r\n\t\t\t\tcatch (IOException e) {\r\n\t\t\t\t\tlogger.logError(e.getMessage(), e);\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t\treturn null;\r\n\t}\r\n}\r\n```\r\n\r\nThis will successfully upload the file and create the resource on the server.\r\nThe call will be as stated on the documentation:\r\n```Java\r\nFile file = new File(ea.getFilepath());\r\n\r\nAttachmentItem attachmentItem = new AttachmentItem();\r\nattachmentItem.attachmentType = AttachmentType.FILE;\r\nattachmentItem.name = file.getName();\r\nattachmentItem.size = file.length();\r\nattachmentItem.contentType = \"application/octet-stream\";\r\n\r\nInputStream fileStream = new FileInputStream(file);\r\nlong streamSize = attachmentItem.size;\r\n\r\n// Create a callback used by the upload provider\r\nIProgressCallback\u003cAttachmentItem> callback = new IProgressCallback\u003c>() {\r\n @Override\r\n // Called after each slice of the file is uploaded\r\n public void progress(final long current, final long max) {\r\n logger.logDebug(String.format(\"Uploaded %d bytes of %d total bytes\", current, max));\r\n }\r\n\r\n @Override\r\n public void success(final AttachmentItem result) {\r\n logger.logDebug(String.format(\"Uploaded file.\"));\r\n }\r\n\r\n public void failure(final ClientException ex) {\r\n logger.logError(String.format(\"Error uploading file: %s\", ex.getMessage()), ex);\r\n }\r\n};\r\n\r\nUploadSession uploadSession = getClient().me().messages(drafted.id).attachments().createUploadSession(attachmentItem).buildRequest().post();\r\nlogger.logDebug(String.format(\"Upload session URL: %s\", uploadSession.uploadUrl));\r\n\r\nChunkedUploadProvider\u003cAttachmentItem> chunkedUploadProvider = new ChunkedUploadProvider\u003c>(uploadSession, getClientUnauthorized(), fileStream,\r\n streamSize, AttachmentItem.class);\r\n\r\n// Config parameter is an array of integers\r\n// customConfig[0] indicates the max slice size\r\n// Max slice size must be a multiple of 320 KiB, numRetries\r\nint[] customConfig = {\r\n 320 * 1024, 1\r\n};\r\n\r\n// Do the upload\r\nchunkedUploadProvider.upload(callback, customConfig);\r\n```\r\n\r\nSo it needs to be checked if there is really consistent behaviour on the server side regarding the response codes for accepting chunks when using the different endpoints onedrive || messages || events etc.\r\nMaybe it is a good idea to establish separate reponsehandlers and uploadproviders for the different endpoints, especially when mixing the audience. The preauthenticated uploadurl also seems not to be the ideal solution for a permission handover.\r\nPS: Don't forget to add the permission Mail.ReadWrite in case you upload an attachment to an existing message, otherwise you will end up also in a 401!\r\nOriginally posted by @gantners in #409 (comment)\r\n[AB#6035](https://microsoftgraph.visualstudio.com/0985d294-5762-4bc2-a565-161ef349ca3e/_workitems/edit/6035)","author":{"url":"https://github.com/jball-vcra","@type":"Person","name":"jball-vcra"},"datePublished":"2020-08-18T17:51:40Z","url":"https://github.com/microsoftgraph/msgraph-sdk-java/issues/423"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:ccf5f007-75b7-90b6-54f4-8175274ff562 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C558:1F67:10908C:171700:6B0542D5 |
| html-safe-nonce | a29970edd04d2515c2c81e31fd718983db08b70c708403e98915b6b03400e8fb |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNTU4OjFGNjc6MTA5MDhDOjE3MTcwMDo2QjA1NDJENSIsInZpc2l0b3JfaWQiOiI0MDYzNzc4NDU4NTkxMDc1NDEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 54f11901773e9f2acb7df65e8a19c553ad37926f3f41f71cd7db013aec60124f |
| hovercard-subject-tag | issue:681224530 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoftgraph/msgraph-sdk-java/423/issue_layout |
| twitter:image | https://opengraph.githubassets.com/76a87101ccfb14a023561e528cb14a7f2f79558a01f03df304c4be54025a1baf/microsoftgraph/msgraph-sdk-java/issues/423 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/76a87101ccfb14a023561e528cb14a7f2f79558a01f03df304c4be54025a1baf/microsoftgraph/msgraph-sdk-java/issues/423 |
| og:image:alt | Uploading chunks return reponse code 200 where the ChunkedUploadResponseHandler awaits 202. That finishes the chunked upload immediately and the upload will not finish with a 201 Created. You can g... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jball-vcra |
| hostname | github.com |
| expected-hostname | github.com |
| None | 854d880890b5e847b3fce9d1b31f032107baeddeebad2aa379c48aa4f1451f6b |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoftgraph/msgraph-sdk-java git https://github.com/microsoftgraph/msgraph-sdk-java.git |
| octolytics-dimension-user_id | 17304259 |
| octolytics-dimension-user_login | microsoftgraph |
| octolytics-dimension-repository_id | 103300409 |
| octolytics-dimension-repository_nwo | microsoftgraph/msgraph-sdk-java |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 103300409 |
| octolytics-dimension-repository_network_root_nwo | microsoftgraph/msgraph-sdk-java |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 220e980097d94ce616d5d11fbd5316d208e3fc93 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width