Streaming upload to Google Storage API when the final stream size is not known

一曲冷凌霜 提交于 2021-02-11 14:44:11

问题


So Google Storage has this great API for resumable uploads: https://cloud.google.com/storage/docs/json_api/v1/how-tos/resumable-upload which I'd like to utilize to upload a large object in multiple chunks. However this is done a in stream processing pipeline where the total amount of bytes in the stream is not know in advance.

According to the documentation of the API, you're supposed to use Content-Range header to tell the Google Storage API that you're done uploading the file, e.g.:

PUT https://www.googleapis.com/upload/storage/v1/b/myBucket/o?uploadType=resumable&upload_id=xa298sd_sdlkj2 HTTP/1.1
Content-Length: 1024
Content-Range: bytes 1023-2048/2048

[BYTES 1023-2048]

If I'm understanding how this works correctly, that bytes 1023-2048/2048 value of the Content-Range header is how Google Storage determines that you're uploading the last chunk of data and it can successfully finish the resumable upload session after it's done.

In my case however the total stream size is not known in advance, so I need to keep uploading until there's no more data to upload. Is there a way to do this using the Google Storage API? Ideally I'd like some way of manually telling the API "hey I'm done, don't expect any more data from me".


回答1:


In my case however the total stream size is not known in advance,

In this case you need to send Content-Range: bytes 1023-2048/* in the PUT requests. Note however, that these requests must be in multiples of 256KiB:

https://cloud.google.com/storage/docs/json_api/v1/how-tos/resumable-upload#example_uploading_the_file

so I need to keep uploading until there's no more data to upload. Is there a way to do this using the Google Storage API?

Yes. You send the chunks with bytes NNNNN-MMMMM/*.

Ideally I'd like some way of manually telling the API "hey I'm done, don't expect any more data from me".

You do that by either (a) sending a chunk that is not a multiple of 256KiB, or (b) sending a chunk with bytes NNN-MMM/(MMM+1). That is, the last chunk contains the total size for the upload and indicates that it contains the last byte.




回答2:


The documentation you linked states that:

Content-Length. Required unless you are using chunked transfer encoding. Set to the number of bytes in the body of this initial request.

So if you click that link to chunked transfer encoding, the HTTP spec will explain how to send chunks of data until the transfer is complete:

Chunked enables content streams of unknown size to be transferred as a sequence of length-delimited buffers, which enables the sender to retain connection persistence and the recipient to know when it has received the entire message.

It likely not going to be easy to implement this on your own, so I suggest finding an HTTP client library that knows how to do this for you.



来源:https://stackoverflow.com/questions/58694712/streaming-upload-to-google-storage-api-when-the-final-stream-size-is-not-known

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!