| 1 | package blobs |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "io" |
| 7 | "os" |
| 8 | ) |
| 9 | |
| 10 | // PutBlockBlobFromFile is a helper method which takes a file, and automatically chunks it up, rather than having to do this yourself |
| 11 | func (client Client) PutBlockBlobFromFile(ctx context.Context, accountName, containerName, blobName string, file *os.File, input PutBlockBlobInput) error { |
| 12 | fileInfo, err := file.Stat() |
| 13 | if err != nil { |
| 14 | return fmt.Errorf("Error loading file info: %s", err) |
| 15 | } |
| 16 | |
| 17 | fileSize := fileInfo.Size() |
| 18 | bytes := make([]byte, fileSize) |
| 19 | |
| 20 | _, err = file.ReadAt(bytes, 0) |
| 21 | if err != nil { |
| 22 | if err != io.EOF { |
| 23 | return fmt.Errorf("Error reading bytes: %s", err) |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | input.Content = &bytes |
| 28 | |
| 29 | if _, err = client.PutBlockBlob(ctx, accountName, containerName, blobName, input); err != nil { |
| 30 | return fmt.Errorf("Error putting bytes: %s", err) |
| 31 | } |
| 32 | |
| 33 | return nil |
| 34 | } |