I am trying to implement a Read and Write method for a program that uploads file []byte to the cloud and downloads file []byte from the cloud.
So far my read method is stuck in an endless read loop reading only same 32KiB.
I am using my opened file to as input to satisfy io.Copy
.
I am sure my implementation is messy somehow
func (s S3File) Read(buffer []byte) (int, error){
if s.mode != "rb"{
fmt.Println("Wrong mode used")
}
buf, err := s.DownBufferInfo(s.path)
if err != nil{
fmt.Println("Error from getting buffer for reader. at `rb` ")
}
allbytes := buf.Bytes() //BYTES READ FROM A FILE IN THE CLOUD
fmt.Printf("len of bytes from reader --%v", len(allbytes))
/////////////////////////////////////////////////////////
//result := copy(buffer, allbytes)
//n := append(buffer, allbytes...)
////////////////////////////////////////////////////////
fileBytes := bytes.NewBuffer(allbytes)
result, err := fileBytes.Read(buffer)
////////////////////////////////////////////////////////
//fileBytes := bytes.NewReader(allbytes)
//result, err := fileBytes.Read(buffer)
fmt.Printf("len of bytes read to buffer --%v", result)
fmt.Printf("Total buffer len after read --%v", len(buffer))
return result, err
}
EDIT: I was able to get it to work and the answer should follow below.
While my main motivation was to able to use io.Copy
in my code conveniently, i realized later it was unnecessary with my current Reader which still worked regardless.
Function func Copy(dst Writer, src Reader) (written int64, err error)
takes source Reader and destination Writer.
My generic reader already provided me with a slice of []byte
. All i needed was a Writer to take in those bytes.
Conveniently enough, dst Writer
implements Writer interface as follows:
type Writer interface {
Write(p []byte) (n int, err error)
}
All i did was just do:
dst.Write(my_slice_byte)
Alternatively i re-wrote my Read implementation but it was unnecessary.