Skip to content

Instantly share code, notes, and snippets.

@nonotest
Last active June 11, 2017 09:16
Show Gist options
  • Save nonotest/2b431eef97fe15e35326faf181fa297b to your computer and use it in GitHub Desktop.
Save nonotest/2b431eef97fe15e35326faf181fa297b to your computer and use it in GitHub Desktop.
Copy a file uploaded with react native background uploader to google cloud
// UploadAttachmentsEndpoint uses httprouter package..
func UploadAttachmentsEndpoint(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
// init your client
//...
// init your bucket
storageBucket := client.Bucket(storageBucketName)
w := storageBucket.Object(name).NewWriter(ctx)
w.ContentType = req.Header.Get("Content-Type")
// ... get the body of the file and copy it in a buffer
buf, _ := ioutil.ReadAll(req.Body)
rdr1 := ioutil.NopCloser(bytes.NewBuffer(buf))
if written, err := io.Copy(w, rdr1); err != nil {
// handle err if there was any...
res.Header().Set("500", "Internal Error")
http.Error(res, "Internal error while uploading. Contact support.", http.StatusInternalServerError)
return
}
// ... do whatever you want with the file now
if err := w.Close(); err != nil {
// handle if theres an error while closing
}
// write a response back...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment