File: put_block_blob_file.go

package info (click to toggle)
golang-github-tombuildsstuff-giovanni 0.20.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 15,908 kB
  • sloc: makefile: 3
file content (34 lines) | stat: -rw-r--r-- 840 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package blobs

import (
	"context"
	"fmt"
	"io"
	"os"
)

// PutBlockBlobFromFile is a helper method which takes a file, and automatically chunks it up, rather than having to do this yourself
func (client Client) PutBlockBlobFromFile(ctx context.Context, accountName, containerName, blobName string, file *os.File, input PutBlockBlobInput) error {
	fileInfo, err := file.Stat()
	if err != nil {
		return fmt.Errorf("Error loading file info: %s", err)
	}

	fileSize := fileInfo.Size()
	bytes := make([]byte, fileSize)

	_, err = file.ReadAt(bytes, 0)
	if err != nil {
		if err != io.EOF {
			return fmt.Errorf("Error reading bytes: %s", err)
		}
	}

	input.Content = &bytes

	if _, err = client.PutBlockBlob(ctx, accountName, containerName, blobName, input); err != nil {
		return fmt.Errorf("Error putting bytes: %s", err)
	}

	return nil
}