Skip to content

Commit 0ec3e5c

Browse files
committed
Add configurable chunk size for GCS uploads through SetUploadChunkSizeByte
1 parent 2f116ee commit 0ec3e5c

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

gcsfs/file.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ func NewGcsFileFromOldFH(
9090
return res
9191
}
9292

93+
// SetUploadChunkSizeByte The default chunk size for uploading files to GCS is 16MB.
94+
// This buffer size may use memory excessively. You can adjust it to a value that is a multiple of 256KiB.
95+
func (o *GcsFile) SetUploadChunkSizeByte(size int) {
96+
o.resource.uploadChunkSizeByte = size
97+
}
98+
9399
func (o *GcsFile) Close() error {
94100
if o.closed {
95101
// the afero spec expects the call to Close on a closed file to return an error

gcsfs/file_resource.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ type gcsFileResource struct {
4646
name string
4747
fileMode os.FileMode
4848

49+
uploadChunkSizeByte int
50+
4951
currentGcsSize int64
5052
offset int64
5153
reader io.ReadCloser
@@ -185,6 +187,16 @@ func (o *gcsFileResource) WriteAt(b []byte, off int64) (n int, err error) {
185187
//
186188
// It will however require a download and upload of the original file but it
187189
// can't be avoided if we should support seek-write-operations on GCS.
190+
191+
chunkSize := o.fs.UploadChunkSizeByte
192+
if o.uploadChunkSizeByte != 0 {
193+
chunkSize = o.uploadChunkSizeByte
194+
}
195+
196+
if chunkSize != 0 {
197+
w.SetChunkSize(chunkSize)
198+
}
199+
188200
objAttrs, err := o.obj.Attrs(o.ctx)
189201
if err != nil {
190202
if off > 0 {

gcsfs/fs.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type Fs struct {
4343
rawGcsObjects map[string]*GcsFile
4444

4545
autoRemoveEmptyFolders bool // trigger for creating "virtual folders" (not required by GCSs)
46+
UploadChunkSizeByte int
4647
}
4748

4849
func NewGcsFs(ctx context.Context, client stiface.Client) *Fs {
@@ -60,6 +61,12 @@ func NewGcsFsWithSeparator(ctx context.Context, client stiface.Client, folderSep
6061
}
6162
}
6263

64+
// SetUploadChunkSizeByte The default chunk size for uploading files to GCS is 16MB.
65+
// This buffer size may use memory excessively. You can adjust it to a value that is a multiple of 256KiB.
66+
func (fs *Fs) SetUploadChunkSizeByte(uploadChunkSize int) {
67+
fs.UploadChunkSizeByte = uploadChunkSize
68+
}
69+
6370
// normSeparators will normalize all "\\" and "/" to the provided separator
6471
func (fs *Fs) normSeparators(s string) string {
6572
return strings.Replace(strings.Replace(s, "\\", fs.separator, -1), "/", fs.separator, -1)
@@ -147,6 +154,7 @@ func (fs *Fs) Create(name string) (*GcsFile, error) {
147154
return nil, err
148155
}
149156
w := obj.NewWriter(fs.ctx)
157+
150158
err = w.Close()
151159
if err != nil {
152160
return nil, err

gcsfs/gcs.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ func (fs *GcsFs) Name() string {
8585
return fs.source.Name()
8686
}
8787

88+
func (fs *GcsFs) SetUploadChunkSizeByte(size int) {
89+
fs.source.SetUploadChunkSizeByte(size)
90+
}
91+
8892
func (fs *GcsFs) Create(name string) (afero.File, error) {
8993
return fs.source.Create(name)
9094
}

0 commit comments

Comments
 (0)