How do I go from io.ReadCloser to io.ReadSeeker? -
i'm trying download file s3 , upload file bucket in s3. copy api won't work here because i've been told not use it.
getting object s3 has response.body
that's io.readcloser
, upload file, payload takes body
that's io.readseeker
.
the way can figure out saving response.body
file passing file io.readseeker
. require writing entire file disk first reading entire file disk sounds pretty wrong.
what is:
resp, _ := conn.getobject(&s3.getobjectinput{key: "bla"}) conn.putobject(&s3.putobjectinput{body: resp.body}) // resp.body io.readcloser , field type expects io.readseeker
question is, how go io.readcloser
io.readseeker
in efficient way possible?
io.readseeker
interface groups basic read()
, seek()
methods. definition of seek()
method:
seek(offset int64, whence int) (int64, error)
an implementation of seek()
method requires able seek anywhere in source, requires all source available or reproducible. file perfect example, file saved permanently disk , part of can read @ time.
response.body
implemented read underlying tcp connection. reading underlying tcp connection gives data client @ other side sends you. data not cached, , client won't send data again upon request. that's why response.body
not implement io.seeker
(and io.readseeker
either).
so in order obtain io.readseeker
io.reader
or io.readcloser
, need caches all data, upon request can seek anywhere in that.
this caching mechanism may writing file mentioned, or can read memory, []byte
using ioutil.readall()
, , can use bytes.newreader()
obtain io.readseeker
[]byte
. of course has limitations: content must fit memory, , might not want reserve amount of memory file copy operation.
all in all, implementation of io.seeker
or io.readseeker
requires source data available, best bet writing file, or small files reading []byte
, streaming content of byte slice.
Comments
Post a Comment