-
Notifications
You must be signed in to change notification settings - Fork 7
/
gcs.js
42 lines (32 loc) · 1.24 KB
/
gcs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
var request = require('request');
var GCS = function(gapi) {
this.gapi = gapi;
};
GCS.prototype.putStream = function(stream, bucket, filepath, headers, callback) {
stream.pause();
this.gapi.getToken(function(err, token) {
if (err) { return callback(err); }
headers.Authorization = 'Bearer ' + token;
headers.Date = new Date().toString();
headers.Host = 'storage.googleapis.com';
headers['x-goog-api-version'] = 2;
var url = 'https://storage.googleapis.com/' + bucket + filepath;
stream.resume();
stream.pipe(request.put(url, {headers: headers}, callback));
});
};
GCS.prototype.deleteFile = function(bucket, filepath, callback) {
this.gapi.getToken(function(err, token) {
if (err) { return callback(err); }
var headers = {};
headers.Authorization = 'Bearer ' + token;
headers.Date = new Date().toString();
headers.Host = 'storage.googleapis.com';
headers['Content-Length'] = 0;
headers['x-goog-api-version'] = 2;
var url = 'https://storage.googleapis.com/' + bucket + filepath;
request.del(url, {headers: headers}, callback);
});
};
module.exports = GCS;
module.exports.gapitoken = require('gapitoken');