Skip to main content
Version: 0.17

Uploading files

Once you logged into your account you can upload files by calling storage.upload():

storage.upload('hello-world.txt', 'Hello world!', (error, file) => {
if (error) return console.error('There was an error:', error)
console.log('The file was uploaded!', file)
})

The upload function is one of the most complex functions in the library because it's quite flexible. In the example above you pass the file name in the first argument, the file contents in the second and wait for a callback.

You can input an options object too:

storage.upload({
name: 'hello-world.txt'
}, 'Hello world!', (error, file) => {
if (error) return console.error('There was an error:', error)
console.log('The file was uploaded!', file)
})

It allows you setting more options, some which will be explained later. The complete list of options are listed in the API reference page.

You can use Buffers to input file content, like this:

storage.upload('hello-world.txt', Buffer.from('SGVsbG8gd29ybGQh', 'base64'), (error, file) => {
if (error) return console.error('There was an error:', error)
console.log('The file was uploaded!', file)
})

Node readable streams are also supported as inputs, but then you also need to specify a file size in the options:

storage.upload({
name: 'hello-world.txt',
size: 12
}, fs.createReadStream('hello-world.txt'), (error, file) => {
if (error) return console.error('There was an error:', error)
console.log('The file was uploaded!', file)
})
info

If you forget to specify the file size the entire stream will be buffered in memory, which will cause issues when uploading large files. Because is quite easy to make this mistake in V1 it is not allowed to upload without setting the size or explicitly enabling buffering.

Finally piping is another way to uploading files:

const fileStream = fs.createReadStream('hello-world.txt')
const uploadStream = storage.upload({
name: 'hello-world.txt',
size: 12
})
fileStream.pipe(uploadStream)
uploadStream.on('complete', file) => {
console.log('The file was uploaded!', file)
})
uploadStream.on('error', error) => {
console.log('The file was uploaded!', file)
})

After uploading you will get a File object either via the callback or the complete event. In the next part of this tutorial we will share this file and do other operations on it.