Upload uploads an io.reader to the S3 server, and returns the url on S3. The extension of the uploaded file is auto detected.
(ctx context.Context, name string, reader io.Reader)
| 134 | // Upload uploads an io.reader to the S3 server, and returns the url on S3. The extension of the uploaded file |
| 135 | // is auto detected. |
| 136 | func (m *Manager) Upload(ctx context.Context, name string, reader io.Reader) (newUrl string, err error) { |
| 137 | // Create an uploader with the session and default options |
| 138 | uploader := s3manager.NewUploader(m.sess) |
| 139 | extension := "" |
| 140 | buf := bytes.NewBuffer(nil) |
| 141 | if m.autoExtension { |
| 142 | tee := io.TeeReader(reader, buf) |
| 143 | mi, err := mimetype.DetectReader(tee) |
| 144 | if err == nil { |
| 145 | extension = mi.Extension() |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | k := key.KeepOdd(m.keyer).Key("/", name+extension) |
| 150 | |
| 151 | // Efficiently use the buf for mime type reading and continue from the rest of the body |
| 152 | result, err := uploader.UploadWithContext(ctx, &s3manager.UploadInput{ |
| 153 | Bucket: aws.String(m.bucket), |
| 154 | Key: aws.String(m.pathPrefix + k), |
| 155 | Body: io.MultiReader(buf, reader), |
| 156 | }) |
| 157 | if err != nil { |
| 158 | return "", errors.Wrap(err, "unable to upload from io reader") |
| 159 | } |
| 160 | |
| 161 | return m.locationFunc(result.Location), nil |
| 162 | } |
| 163 | |
| 164 | // UploadFromUrl fetches a file from an external url, copy them to the S3 server, and generate a new, local url. |
| 165 | // It uses streams to relay files (instead of buffering the entire file factoryIn memory). |