Created
December 8, 2015 22:28
-
-
Save stephenplusplus/5bf537da70df0c9db36c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| var fs = require('fs'); | |
| var gcloud = require('gcloud'); | |
| var gcs = gcloud.storage({ | |
| projectId: 'nth-circlet-705', | |
| keyFilename: '/Users/stephen/dev/keyfile.json' | |
| }); | |
| var bucket = gcs.bucket('stephen-has-a-new-bucket'); | |
| var fileOptions = { | |
| action: 'read', | |
| expires: '01-01-2100' | |
| }; | |
| var readStream = fs.createReadStream('/Users/stephen/dev/dots/.bash_profile'); | |
| uploadStream(readStream, 'directory', '.bash_profile', function(err, url) { | |
| if (err) throw err; | |
| console.log('file uploaded and URL created', url); | |
| move('directory/.bash_profile', 'directory-new/.bash_profile', function(err, url) { | |
| if (err) throw err; | |
| console.log('file moved and URL created', url); | |
| }); | |
| }); | |
| function uploadStream(fileStream, path, fileName, done) { | |
| var destination = path + '/' + fileName; | |
| var file = bucket.file(destination); | |
| var writeStream = file.createWriteStream(); | |
| fileStream.on('error', done); | |
| writeStream | |
| .on('error', done) | |
| .on('finish', function() { | |
| file.getSignedUrl(fileOptions, done); | |
| }); | |
| fileStream.pipe(writeStream); | |
| } | |
| function move(filePath, newFilePath, done) { | |
| var file = bucket.file(filePath); | |
| file.move(newFilePath, function(err, destinationFile) { | |
| if (err) return done(err); | |
| destinationFile.getSignedUrl(fileOptions, done); | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment