multipartstreamer

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2013 License: MIT Imports: 8 Imported by: 335

README

multipartstreamer

Package multipartstreamer helps you encode large files in MIME multipart format without reading the entire content into memory. It uses io.MultiReader to combine an inner multipart.Reader with a file handle.

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

Overview

Package multipartstreamer helps you encode large files in MIME multipart format without reading the entire content into memory. It uses io.MultiReader to combine an inner multipart.Reader with a file handle.

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)

New initializes a new MultipartStreamer.

func (*MultipartStreamer) Boundary

func (m *MultipartStreamer) Boundary() string

func (*MultipartStreamer) GetReader

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

GetReader gets an io.ReadCloser for passing to an http.Request.

func (*MultipartStreamer) Len

func (m *MultipartStreamer) Len() int64

Len calculates the byte size of the multipart content.

func (*MultipartStreamer) SetupRequest

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

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

func (*MultipartStreamer) WriteFields

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

WriteFields writes multiple form fields to the multipart.Writer.

func (*MultipartStreamer) WriteFile

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

WriteFile is a shortcut for adding a local file as an io.Reader.

func (*MultipartStreamer) WriteReader

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

WriteReader adds an io.Reader to get the content of a file. The 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