Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

gcs-resumable-upload

googleapis1.1mMITdeprecated6.0.0TypeScript support: included

gcs-resumable-upload is deprecated. Support will end on 11/01/2023

Upload a file to Google Cloud Storage with built-in resumable behavior

google, gcloud, storage, gcs, upload, resumable

readme

[DEPRECATED] gcs-resumable-upload

This repository has been deprecated. Support will end on November 1, 2023.

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.

How it works

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.

Authentication

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.

API

const {gcsResumableUpload} = require('gcs-resumable-upload')
const upload = gcsResumableUpload(config)

upload is an instance of Duplexify.


Methods

upload.createURI(callback)

callback(err, resumableURI)
callback.err
  • Type: Error

Invoked if the authorization failed or the request to start a resumable session failed.

callback.resumableURI
  • Type: String

The resumable upload session URI.

upload.deleteConfig()

This will remove the config data associated with the provided file.


Configuration

config

  • Type: object

Configuration object.

config.authClient

If you want to re-use an auth client from google-auth-library, pass an instance here.

config.authConfig
  • Type: object
  • Optional

See authConfig.

config.bucket
  • Type: string
  • Required

The name of the destination bucket.

config.configPath
  • 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.

config.customRequestOptions
  • 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',
    },
  },
})
config.file
  • Type: string
  • Required

The name of the destination file.

config.generation
  • 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.

config.key
  • Type: string|buffer
  • Optional

A customer-supplied encryption key.

config.kmsKeyName
  • 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.

config.metadata
  • Type: object
  • Optional

Any metadata you wish to set on the object.

config.metadata.contentLength

Set the length of the file being uploaded.

config.metadata.contentType

Set the content type of the incoming data.

config.offset
  • Type: number
  • Optional

The starting byte of the upload stream, for resuming an interrupted upload.

config.origin
  • Type: string
  • Optional

Set an Origin header when creating the resumable upload URI.

config.predefinedAcl
  • Type: string
  • Optional

Apply a predefined set of access controls to the created file.

Acceptable values are:

  • authenticatedRead - Object owner gets OWNER access, and allAuthenticatedUsers get READER access.
  • bucketOwnerFullControl - Object owner gets OWNER access, and project team owners get OWNER access.
  • bucketOwnerRead - Object owner gets OWNER access, and project team owners get READER access.
  • private - Object owner gets OWNER access.
  • projectPrivate - Object owner gets OWNER access, and project team members get access according to their roles.
  • publicRead - Object owner gets OWNER access, and allUsers get READER access.
config.private
  • Type: boolean
  • Optional

Make the uploaded file private. (Alias for config.predefinedAcl = 'private')

config.public
  • Type: boolean
  • Optional

Make the uploaded file public. (Alias for config.predefinedAcl = 'publicRead')

config.uri
  • 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.

config.userProject
  • 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.

config.retryOptions
  • Type: object
  • Optional

Parameters used to control retrying operations.

interface RetryOptions {
  retryDelayMultiplier?: number;
  totalTimeout?: number;
  maxRetryDelay?: number;
  autoRetry?: boolean;
  maxRetries?: number;
  retryableErrorFn?: (err: ApiError) => boolean;
}
config.retryOptions.retryDelayMultiplier
  • Type: number
  • Optional

Base number used for exponential backoff. Default 2.

config.retryOptions.totalTimeout
  • Type: number
  • Optional

Upper bound on the total amount of time to attempt retrying, in seconds. Default: 600.

config.retryOptions.maxRetryDelay
  • Type: number
  • Optional

The maximum time to delay between retries, in seconds. Default: 64.

config.retryOptions.autoRetry
  • Type: boolean
  • Optional

Whether or not errors should be retried. Default: true.

config.retryOptions.maxRetries
  • Type: number
  • Optional

The maximum number of retries to attempt. Default: 5.

config.retryOptions.retryableErrorFn
  • Type: function
  • Optional

Custom function returning a boolean indicating whether or not to retry an error.

config.chunkSize
  • Type: number
  • Optional

Enables Multiple chunk upload mode and sets each request size to this amount.

This only makes sense to use for larger files. The chunk size should be a multiple of 256 KiB (256 x 1024 bytes). Larger chunk sizes typically make uploads more efficient. We recommend using at least 8 MiB for the chunk size.

Review documentation for guidance and best practices.


Events

.on('error', function (err) {})

err
  • Type: Error

Invoked if the authorization failed, the request failed, or the file wasn't successfully uploaded.

.on('response', function (response) {})

resp
  • Type: Object

The response object from Gaxios.

metadata
  • Type: Object

The file's new metadata.

.on('progress', function (progress) {})

progress

  • Type: Object
progress.bytesWritten
  • Type: number
progress.contentLength
  • Type: number

Progress event provides upload stats like Transferred Bytes and content length.

.on('finish', function () {})

The file was uploaded successfully.


Static Methods

const {createURI} = require('gcs-resumable-upload')
`

createURI(config, callback)

callback(err, resumableURI)
callback.err
  • Type: Error

Invoked if the authorization failed or the request to start a resumable session failed.

callback.resumableURI
  • Type: String

The resumable upload session URI.

changelog

Changelog

npm history

6.0.0 (2023-08-10)

⚠ BREAKING CHANGES

  • update to Node 14 (#549)

Miscellaneous Chores

5.0.1 (2022-08-23)

Bug Fixes

5.0.0 (2022-05-20)

⚠ BREAKING CHANGES

  • update library to use Node 12 (#520)

Build System

4.0.2 (2022-01-26)

Bug Fixes

  • Stop Duplicate Response Handlers on Retries (#502) (c5b3059)

4.0.1 (2022-01-06)

Bug Fixes

  • Fix support for streams without content-length property (#491) (ac2f73b)

4.0.0 (2021-12-30)

⚠ BREAKING CHANGES

  • Multiple Chunk Upload Support (#486)

Features

3.6.0 (2021-11-09)

Features

  • add error code to error in startUploading (#474) (1ae6987)

3.5.1 (2021-11-04)

Bug Fixes

3.5.0 (2021-11-04)

Features

3.4.0 (2021-11-03)

Features

Bug Fixes

  • throw informative error in the case that retries run out (#477) (4b3db66)

3.3.1 (2021-09-02)

Bug Fixes

  • build: switch primary branch to main (#451) (c180f66)

3.3.0 (2021-07-19)

Features

3.2.1 (2021-06-28)

Bug Fixes

3.2.0 (2021-06-10)

Features

  • add gcf-owl-bot[bot] to ignoreAuthors (#421) (b842b41)

3.1.4 (2021-05-03)

Bug Fixes

3.1.3 (2021-02-12)

Bug Fixes

  • deps: update dependency google-auth-library to v7 (#404) (8d4dd15)

3.1.2 (2020-12-22)

Bug Fixes

  • deps: update dependency gaxios to v4 (#389) (05f3af2)

3.1.1 (2020-07-09)

Bug Fixes

3.1.0 (2020-06-24)

Features

3.0.0 (2020-05-12)

⚠ BREAKING CHANGES

Features

  • allow user-specified protocol for options.apiEndpoint (#349) (47f76d0)

Bug Fixes

  • deps: update dependency gaxios to v3 (#325) (584ea71)
  • deps: update dependency google-auth-library to v6 (#327) (609bf9a)

Miscellaneous Chores

Build System

2.3.3 (2020-03-06)

Bug Fixes

2.3.2 (2019-12-05)

Bug Fixes

  • deps: TypeScript 3.7.0 causes breaking change in typings (#285) (3e671b2)
  • typescript: add return type for base uri getter (#286) (7121624)

2.3.1 (2019-11-14)

Bug Fixes

2.3.0 (2019-10-09)

Features

  • support all query parameters during URI creation (#275) (383a490)

2.2.5 (2019-09-07)

Bug Fixes

  • typecast metadata.size from string to number (#263) (64ea7a1)

2.2.4 (2019-08-15)

Bug Fixes

2.2.3 (2019-07-26)

Bug Fixes

  • deps: update dependency google-auth-library to v5 (#250) (8bc4798)

2.2.2 (2019-07-17)

Bug Fixes

  • deps: update dependency pumpify to v2 (#237) (a2a2636)

2.2.1 (2019-07-17)

Bug Fixes

2.2.0 (2019-07-14)

Bug Fixes

  • docs: make anchors work in jsdoc (#238) (86e4433)
  • expose 'Retry limit exceeded' server error message (#240) (40a1306)
  • make cache key unique by including generation (#243) (85f80ab)

Features

2.1.1 (2019-06-20)

Bug Fixes

  • deps: update dependency configstore to v5 (#234) (9b957c6)

2.1.0 (2019-06-19)

Features

2.0.0 (2019-05-09)

Bug Fixes

  • deps: update dependency abort-controller to v3 (0c4f6c0)
  • deps: update dependency gaxios to v2 (#210) (d5a1a5c)
  • deps: update dependency google-auth-library to v4 (#219) (1e60178)

Build System

BREAKING CHANGES

  • upgrade engines field to >=8.10.0 (#213)

v1.1.0

03-26-2019 07:13 PDT

New Features

  • feat: support ConfigStore configPath option (#194)

Internal / Testing Changes

  • chore: publish to npm using wombat (#197)
  • build: use per-repo npm publish token (#195)
  • build: Add docuploader credentials to node publish jobs (#192)
  • build: use node10 to run samples-test, system-test etc (#190)
  • build: update release configuration

v1.0.0

02-28-2019 06:27 PST

This release has breaking changes. The underlying transport library was changed from request to gaxios. Any response objects returned via the API will now return a GaxiosResponse object.

Old Code

.on('response', function (resp, metadata) {
  console.log(resp.statusCode);
})

New Code

.on('response', function (resp) {
  console.log(resp.status);
});

Implementation Changes

  • fix: replace request with gaxios (#174)

Documentation

  • docs: update links in contrib guide (#184)
  • docs: add lint/fix example to contributing guide (#177)

Internal / Testing Changes

  • chore(deps): update dependency mocha to v6 (#185)
  • build: use linkinator for docs test (#183)
  • build: create docs test npm scripts (#182)
  • build: test using @grpc/grpc-js in CI (#181)
  • chore: move CONTRIBUTING.md to root (#179)
  • chore(deps): update dependency typescript to ~3.3.0 (#176)

v0.14.1

01-25-2019 10:39 PST

Implementation Changes

  • fix: return GaxiosError directly (#171)

Documentation

  • build: exclude googleapis in 404 check. (#172)
  • build: exclude googleapis.com checks in 404 checker (#170)

v0.14.0

01-23-2019 17:57 PST

New Features

  • feat: support async functions (#164)
  • fix: use the reject handler for promises (#144)
  • feat: add progress events (#135)

Dependencies

  • fix(deps): update dependency google-auth-library to v3 (#165)
  • refactor: use teeny-request (part 1) (#141)
  • chore(deps): update dependency @types/configstore to v4 (#145)
  • chore(deps): update dependency typescript to ~3.2.0 (#140)
  • chore(deps): update dependency gts to ^0.9.0 (#137)
  • chore(deps): update dependency through2 to v3 (#131)
  • refactor: move from axios back to request (#123)
  • chore(deps): update dependency nock to v10 (#113)
  • chore: update the version of typescript (#106)

Documentation

  • build: ignore googleapis.com in doc link checker (#166)
  • build: check broken links in generated docs (#162)

Internal / Testing Changes

  • fix: fix the unit tests (#161)
  • chore(build): inject yoshi automation key (#160)
  • chore: update nyc and eslint configs (#159)
  • chore: fix publish.sh permission +x (#156)
  • fix(build): fix Kokoro release script (#155)
  • build: add Kokoro configs for autorelease (#154)
  • chore: always nyc report before calling codecov (#153)
  • chore: nyc ignore build/test by default (#152)
  • chore: update synth and common config (#150)
  • fix(build): fix system key decryption (#142)
  • chore: add synth.metadata
  • chore: update eslintignore config (#136)
  • chore: use latest npm on Windows (#134)
  • chore: update CircleCI config (#129)
  • chore: include build in eslintignore (#126)
  • chore: update issue templates (#121)
  • chore: remove old issue template (#119)
  • build: run tests on node11 (#118)
  • chores(build): run codecov on continuous builds (#112)
  • chores(build): do not collect sponge.xml from windows builds (#114)
  • chore: update new issue template (#111)
  • build: fix codecov uploading on Kokoro (#108)
  • Update kokoro config (#105)
  • Update CI config (#103)
  • Update kokoro config (#101)
  • test: remove appveyor config (#100)
  • Update kokoro config (#99)
  • Enable prefer-const in the eslint config (#98)
  • Enable no-var in eslint (#97)
  • Update to new repo location (#96)
  • Update CI config (#95)

v0.13.0

Dependencies

  • fix(deps): update dependency google-auth-library to v2 (#89)
  • chore(deps): update dependency nyc to v13 (#86)

Docs

  • docs: update the README (#79)

Internal / Testing Changes

  • Retry npm install in CI (#92)
  • Update CI config (#90)
  • Update CI config (#88)
  • Update the CI config (#85)
  • chore: update CircleCI config
  • chore: ignore package-lock.json (#83)
  • chore: update renovate config (#81)
  • chore: enable noImplicitThis (#82)
  • chore: enable CI and synth script (#77)

v0.12.0

Implemenation Changes

BREAKING CHANGE:

  • chore: drop support for node.js 4 (#75)

Dependencies

  • chore(deps): update dependency gts to ^0.8.0 (#71)
  • fix(deps): update dependency configstore to v4 (#72)
  • chore(deps): update dependency typescript to v3 (#74)

Internal / Testing Changes

  • chore: make it OSPO compliant (#73)
  • fix: quarantine axios types (#70)