Upload a file to Google Cloud Storage with built-in resumable behavior
$ npm install gcs-resumable-upload
const {upload} = require('gcs-resumable-upload');
const fs = require('fs');
fs.createReadStream('titanic.mov')
.pipe(upload({ bucket: 'legally-owned-movies', file: 'titanic.mov' }))
.on('progress', (progress) => {
console.log('Progress event:')
console.log('\t bytes: ', progress.bytesWritten);
})
.on('finish', () => {
// Uploaded!
});
Or from the command line:
$ npm install -g gcs-resumable-upload
$ cat titanic.mov | gcs-upload legally-owned-movies titanic.mov
If somewhere during the operation, you lose your connection to the internet or your tough-guy brother slammed your laptop shut when he saw what you were uploading, the next time you try to upload to that file, it will resume automatically from where you left off.
This module stores a file using ConfigStore that is written to when you first start an upload. It is aliased by the file name you are uploading to and holds the first 16kb chunk of data* as well as the unique resumable upload URI. (Resumable uploads are complicated)
If your upload was interrupted, next time you run the code, we ask the API how much data it has already, then simply dump all of the data coming through the pipe that it already has.
After the upload completes, the entry in the config file is removed. Done!
* The first 16kb chunk is stored to validate if you are sending the same data when you resume the upload. If not, a new resumable upload is started with the new data.
Oh, right. This module uses google-auth-library and accepts all of the configuration that module does to strike up a connection as config.authConfig
. See authConfig
.
const {gcsResumableUpload} = require('gcs-resumable-upload')
const upload = gcsResumableUpload(config)
upload
is an instance of Duplexify
.
- Type:
Error
Invoked if the authorization failed or the request to start a resumable session failed.
- Type:
String
The resumable upload session URI.
This will remove the config data associated with the provided file.
- Type:
object
Configuration object.
- Type:
GoogleAuth
- Optional
If you want to re-use an auth client from google-auth-library, pass an instance here.
- Type:
object
- Optional
See authConfig
.
- Type:
string
- Required
The name of the destination bucket.
- Type:
string
- Optional
Where the gcs-resumable-upload configuration file should be stored on your system. This maps to the configstore option by the same name.
- Type:
object
- Optional
For each API request we send, you may specify custom request options that we'll add onto the request. The request options follow the gaxios API: https://github.com/googleapis/gaxios#request-options.
For example, to set your own HTTP headers:
const stream = upload({
customRequestOptions: {
headers: {
'X-My-Header': 'My custom value',
},
},
})
- Type:
string
- Required
The name of the destination file.
- Type:
number
- Optional
This will cause the upload to fail if the current generation of the remote object does not match the one provided here.
- Type:
string|buffer
- Optional
A customer-supplied encryption key.
- Type:
string
- Optional
Resource name of the Cloud KMS key, of the form projects/my-project/locations/global/keyRings/my-kr/cryptoKeys/my-key
, that will be used to encrypt the object. Overrides the object metadata's kms_key_name
value, if any.
- Type:
object
- Optional
Any metadata you wish to set on the object.
Set the length of the file being uploaded.
Set the content type of the incoming data.
- Type:
number
- Optional
The starting byte of the upload stream, for resuming an interrupted upload.
- Type:
string
- Optional
Set an Origin header when creating the resumable upload URI.
- Type:
string
- Optional
Apply a predefined set of access controls to the created file.
Acceptable values are:
authenticatedRead
- Object owner getsOWNER
access, andallAuthenticatedUsers
getREADER
access.bucketOwnerFullControl
- Object owner getsOWNER
access, and project team owners getOWNER
access.bucketOwnerRead
- Object owner getsOWNER
access, and project team owners getREADER
access.private
- Object owner getsOWNER
access.projectPrivate
- Object owner getsOWNER
access, and project team members get access according to their roles.publicRead
- Object owner getsOWNER
access, andallUsers
getREADER
access.
- Type:
boolean
- Optional
Make the uploaded file private. (Alias for config.predefinedAcl = 'private'
)
- Type:
boolean
- Optional
Make the uploaded file public. (Alias for config.predefinedAcl = 'publicRead'
)
- Type:
string
- Optional
If you already have a resumable URI from a previously-created resumable upload, just pass it in here and we'll use that.
- Type:
string
- Optional
If the bucket being accessed has requesterPays
functionality enabled, this can be set to control which project is billed for the access of this file.
- Type:
Error
Invoked if the authorization failed, the request failed, or the file wasn't successfully uploaded.
- Type:
Object
The response object from Gaxios.
- Type:
Object
The file's new metadata.
- Type:
Object
- Type:
number
- Type:
number
Progress event provides upload stats like Transferred Bytes and content length.
The file was uploaded successfully.
const {createURI} = require('gcs-resumable-upload')
createURI(config, callback)
- Type:
Error
Invoked if the authorization failed or the request to start a resumable session failed.
- Type:
String
The resumable upload session URI.