I ran into the issue of all the uploaded files to a Google Storage Bucket being corrupted. The files were uploaded using the Go SDK. After a lot of fiddling with content types, size and other metadata, I found the culprit which was the mimetype package used to detect the file type before uploading.
This package would read a bit from the file Reader to check the file type. I had not considered that this would change the file pointer in the Reader. This meant that when I tried to upload the file, it would be corrupted.
The solution was to Tee the Reader as specified in the documentation. The file is passed into this recycleReader function which returns the mime type and a new reader that can then be passed to the file upload function.
func recycleReader(input io.Reader) (mimeType string, recycled io.Reader, err error) { header := bytes.NewBuffer(nil) mtype, err := mimetype.DetectReader(io.TeeReader(input, header)) if err != nil { return } recycled = io.MultiReader(header, input) return mtype.String(), recycled, err}