multipartstreamer

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 11, 2013 License: MIT Imports: 8 Imported by: 369

README

multipartstreamer

Helps you build multipart for large files without reading the file until the multipart reader is being read. It does this by creating the file field last and using an io.MultiReader to combine the multipart.Reader with the file handle. The trailing boundary is manually added in another buffer.

The reason you don't want to just use the built-in multipart.Reader is that it will read the whole file into the buffer to build the multipart content.

package main

import (
  "github.com/technoweenie/multipartstreamer.go"
  "net/http"
)

func main() {
  ms := multipartstreamer.New()

  ms.WriteFields(map[string]string{
    "key":			"some-key",
    "AWSAccessKeyId":	"ABCDEF",
    "acl":			"some-acl",
  })

  // Add any io.Reader to the multipart.Reader.
  ms.WriteReader("file", "filename", some_ioReader, size)

  // Shortcut for adding local file.
  ms.WriteFile("file", "path/to/file")

  req, _ := http.NewRequest("POST", "someurl", nil)
  ms.SetupRequest(req)

  res, _ := http.DefaultClient.Do(req)
}

One limitation: You can only write a single file.

TODO

  • Multiple files?

Credits

Heavily inspired by James

https://groups.google.com/forum/?fromgroups=#!topic/golang-nuts/Zjg5l4nKcQ0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MultipartStreamer

type MultipartStreamer struct {
	ContentType string
	// contains filtered or unexported fields
}

func New

func New() (m *MultipartStreamer)

Helps you build multipart for large files without reading the file until the multipart reader is being read. It does this by creating the file field last and using an io.MultiReader to combine the multipart.Reader with the file handle. The trailing boundary is manually added in another buffer.

func (*MultipartStreamer) Boundary

func (m *MultipartStreamer) Boundary() string

func (*MultipartStreamer) GetReader

func (m *MultipartStreamer) GetReader() io.ReadCloser

Gets an io.ReadCloser for passing to an http.Request.

func (*MultipartStreamer) Len

func (m *MultipartStreamer) Len() int64

Calculates the byte size of the multipart content.

func (*MultipartStreamer) SetupRequest

func (m *MultipartStreamer) SetupRequest(req *http.Request)

Sets up the http.Request body, and some crucial HTTP headers.

func (*MultipartStreamer) WriteFields

func (m *MultipartStreamer) WriteFields(fields map[string]string) error

Writes form fields to the multipart.Writer.

func (*MultipartStreamer) WriteFile

func (m *MultipartStreamer) WriteFile(key, filename string) error

Shortcut for adding a local file as an io.Reader.

key - The name of the field for the file data.

filename - The full path of the file to upload. Only the basename of the

full path is sent to the multipart.Reader.

func (*MultipartStreamer) WriteReader

func (m *MultipartStreamer) WriteReader(key, filename string, size int64, reader io.Reader) (err error)

Adds a reader to of some file content for the multipart.Reader. This reader is not accessed until the multipart.Reader is copied to some output writer.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL