Creating buckets with Google Cloud Storage + Node.js - node.js

I want to be able to dynamically create buckets in Google Cloud Storage. I am using the MEAN.io stack, so I want to do this in Node.js on my backend.
I'm having trouble finding apis to help me, is there a way to do this?
Thanks!
UPDATE: Here I've tried using the Google-Node.js api, but nothing is happening. I tried to then authenticate it in case that's the problem, but I'm not really sure where to go from here.
googleAuth.authenticate(
credentials.jwt,
function(err, token) {
if (err) console.log(err);
console.log(token);
googleapis
.discover('storage', 'v1')
.execute(function(err, client) {
if (err) console.log(err);
client.storage.buckets.insert({'project': credentials.project}, {'name': bucketname}).execute();
})
});

The Google Cloud Storage JSON API is probably the API you want to use. You can code against it directly, or you could use a library like the Google APIs Node.js Client.

Related

How do use transloadit addStream() function in the NodeJS SDK?

Trying out the transloadit api, the template works when I use the testing mode on the transloadit website, but when I try to use it in Node JS with the SDK I'm getting an error:
INVALID_FORM_DATA - https://api2.transloadit.com/assemblies - INVALID_FORM_DATA: The form contained bad data, which cannot be parsed.
The relevant code: (_asset.content) is a Buffer object
async function getThumbnailUrl(_assetkey: string, _asset: I.FormFile): Promise<string> {
let tOptions = {
waitForCompletion: true,
params: {
template_id: process.env.THUMB_TRANSLOADIT_TEMPLATE,
},
};
const stream = new Readable({
read() {
this.push(_asset.content);
this.push(null);
},
});
console.log(_asset.content);
util.transloadit.addStream(_assetkey, stream);
return new Promise((resolve, reject) => {
util.transloadit.createAssembly(tOptions, (err, status) => {
if (err) {
reject(err);
}
console.log(status);
//return status;
resolve(status);
});
});
}
I noticed that you also posted this question on the Transloadit forums - so in the case that anyone else runs into this problem you can find more information on this topic here.
Here's a work-around that the OP found that may be useful:
Just to provide some closure to this topic, I just tested my
workaround (upload to s3, then use import s3 robot to grab the file)
and got it to work with the nodejs sdk so i should be good using that.
I have a suspicion the error I was getting was not to do with the
transloadit api, but rather the form-data library for node js
(https://github.com/form-data/form-data 1) and that’s somehow not
inputting the form data in the way that the transloadit api is
expecting.
But as there aren’t alternatives to that library that I could find, I
wasn’t really able to test that hypothesis.
The Transloadit core team also gave this response regarding the issue:
It may try to set his streams to be Tus streams which would mean that
they’re not uploaded as multipart/form data.
In either case it seems like the error to his callback would be
originating from the error out of _remoteJson
These could be the problem areas
https://github.com/transloadit/node-sdk/blob/master/src/TransloaditClient.js#L146
https://github.com/transloadit/node-sdk/blob/master/src/TransloaditClient.js#L606
https://github.com/transloadit/node-sdk/blob/master/src/TransloaditClient.js#L642
It is also possible that the form-data library could be the source of
the error
To really test this further we’re going to need to try using the
library he was using, make sure the output of it is good, and then
debug the node-sdk to see where the logic failure is in it, or if the
logic failure is on the API side.

Heroku doesn't allow temporary downloads. How can I work around this limitation?

I am trying to download images from MongoDB every time my app starts so it will work fast and as the images are in the app, but Heroku crashes. How can I solve this?
Here is the code I'm trying to use:
dir = "./public/media/"
function getAllImages() {
Image.find({}, function (err, allImages) {
if (err) {
console.log(err);
} else {
allImages.forEach(file => {
fs.writeFile(dir + file.name, file.img.data, function (err) {
if (err) throw err;
console.log('Sucessfully saved!');
});
});
};
});
I currently have 24 images which add up to approximately 10 MB. I will use them as static images in my application. I would like to access them via example.com/media/foo.jpg, etc.
User uploads can't be stored on Heroku's ephemeral filesystem. Any changes made to it will be lost whenever your dyno restarts, which happens frequently (at least once per day). Heroku recommends storing uploaded files on a service like Amazon S3.
You can have your users upload files directly from their browsers to S3 or you could use the AWS SDK to save files from your back-end. A higher-level library like multer-s3 might be helpful too.
It's not usually a good idea to store files in your database, but you can store a pointer to files in your database. For example, you might store https://domain.tld/path/to/some/image.jpg in your database if that's where the file actually lives.
I just learn that the problem was the folder that i use (./public/media) was empty so heroku (even though it is there in the git system) did not create the folder. Because of this the code fs.writeFile(dir + file.name, file.img.data, function (err) didn't work. Thanks for the answers.

Use chrome.storage.sync API in PWA

I am working on creating my first PWA. I created a Chrome Extension in the past, and used the chrome.storage.sync API to store data in the cloud (it's stored using Google Drive TMK).
chrome.storage.sync.set({ [key]: val }, function() {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
} else {
resolve();
}
});
is there a way to use this API when creating PWAs? This API is documented here:
https://developer.chrome.com/extensions/storage
it might be possible to use the Google Drive REST API to store key/value data but I cant figure out how yet:
https://developers.google.com/drive/api/v3/appdata

Cloud Functions for Firebase and third party api

My problem is to make use of an API along with Firebase Functions, the API in question is Coinbase, I use the API with node, if I test in the terminal with the node command it works, however when I use it with Firebase Functions Does not work at all, I've been trying to solve the problem for almost a week now.
The code is as follows>
var functions = require('firebase-functions');
var Client = require('coinbase').Client;
var client = new Client({
"apiKey": "xxxxxxxxxxxx",
"apiSecret": "xxxxxxxxxxxxxxxxxxxxxxx"
});
exports.helloWorld = functions.https.onRequest((request, response) => {
this.client.getAccounts({}, function(err, accounts) {
if(accounts){
response.send(accounts);
}else{
response.send(err);
}
});
});
The error: https://us-central1-investimentos-b7406.cloudfunctions.net/helloWorld
The Coinbase API: https://developers.coinbase.com/docs/wallet/guides/bitcoin-wallet
There's a warning in the docs:
Firebase projects on the Spark plan can make only outbound requests to
Google APIs. Requests to third-party APIs fail with an error. For more
information about upgrading your project, see Pricing.
So you'll need to upgrade to a paid plan to use external APIs.

Creating realtime project with Hapi, MongoDb, NodeJS & AngularJS

I'm creating my first web application using Hapi, MongoDb, NodeJS & Angular. I can already get and save some data from my app.
Now each time I need to refresh the data in the browser, I must restart the server. Because data is only fetched on server start, using the following function:
var getUsers = function(db, callback) {
db.collection("users", function(err, collection) {
if (err) return callback(err, "error getting collection");
collection.find({}).toArray(function(err, users) {
if (err) return callback(err, "error getting find()");
console.log("returning users list: ");
Common.setUsers(users);
//console.log(Common.getUsers());
});
});
};
The users can then be fetched via Common.getUsers()
Now what if I want to create a list that shows me - in realtime - which users register for my application? So without a need for server restart. How can I achieve this?
I've done a bit of searching and found RethinkDb, which has changefeeds. (I wish I found this earlier). MongoDb doesn't have changefeeds the way Rethink implements it.
Is there a way that I can create these changefeeds myself? Or is there another tool that can achieve this for me which works with MongoDb?
A little late but I created a playground application which shows how to build a realtime timeline with hapi and the nes plugin. It also utilizes RethinkDB and makes use of their changefeed feature:
Realtime timeline with hapi.js, nes and RethinkDB
Maybe this also helps you a little bit.

Resources