I uploaded an empty .txt or .csv file to Google Cloud Storage.
When I fetch the file's metadata using bucket.file(filename).getMetadata(), the size field shows 20 bytes although the file is empty.
However, when I download the same file, its size is 0 bytes.
The file is completely empty (0 bytes) before upload and after download. The discrepancy is consistent: getMetadata().size always adds 20 bytes for empty files. API used: @google-cloud/storage, nodejs
Questions:
I’ve searched for documentation but couldn’t find anything explaining this behavior.
const storage = new Storage({ projectId })
const bucket = storage.bucket(bucketName)
let options = {
gzip: true,
destination: path,
metadata: { cacheControl: "public, max-age=31536000" }
}
const [file] = await bucket.upload(path, options)
P.S. I suspect it might be metadata-related, but I’d appreciate any concrete explanations or documentation(prefered) links regarding this behavior.
The 20 bytes you're seeing is almost certainly the minimal overhead added by the gzip compression algorithm that you selected in your upload options. gzip is not totally free (nor is any lossless compression algo), but it typically becomes worthwhile when it's used against data that can actually be made smaller without losing information.
You can see this in action without nodejs or Cloud Storage. Just run the following commands on a unix prompt to 1) create an empty file, 2) compress it with gzip, 3) check its size:
$ touch file
# ls -l file
-rw-r--r-- 1 uid gid 0 Jan 22 17:38 file
^-- the original file is empty
$ gzip file
$ ls -l file.gz
-rw-r--r-- 1 uid gid 25 Jan 22 17:38 file.gz
^-- the gzipped file is now 25 bytes
You can see that an empty file, after compression, became 25 bytes using the default compression level. Again, this has nothing to do with Google SDKs or services - it's just the way compression works.