Created
September 22, 2013 18:31
-
-
Save siygle/6662541 to your computer and use it in GitHub Desktop.
[Sample] Upload image to google cloud storage
This file contains 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
var GAPI = require('node-gcs').gapitoken; | |
var GCS = require('node-gcs'); | |
var fs = require('fs'); | |
var path = require('path'); | |
var format = require('util').format; | |
var config = require('../res/config.js').GCS; | |
const STATIC_URL = "https://storage.googleapis.com"; | |
const BUCKET = config.BUCKET_NAME; | |
const FILENAME = "rock-cloud.png"; | |
const KEY_PATH = "../res/key.pem"; | |
const FILE_PATH = "/tmp/test.png"; | |
var gapi = new GAPI({ | |
iss: config.ISS, | |
scope: config.SCOPE, | |
keyFile: path.join(__dirname, KEY_PATH) | |
}, function(err) { | |
if (err) { | |
console.error(err); | |
} else { | |
var gcs = new GCS(gapi); | |
fs.stat(FILE_PATH, function(err, stats) { | |
if (err) { | |
console.error(err); | |
} else { | |
var file = fs.createReadStream(FILE_PATH); | |
var headers = { | |
'Content-Length': stats.size, | |
'Content-Type': 'image/png', | |
'x-goog-acl': 'public-read' | |
}; | |
gcs.putStream(file, config.BUCKET_NAME, '/' + FILENAME, headers, function(err, res, body) { | |
if (err) { | |
console.error(err); | |
} else { | |
console.log(res); | |
console.log(format("Your file is here: %s/%s/%s", STATIC_URL, BUCKET, FILENAME)); | |
} | |
}); | |
} | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment