Persistent storage with dropbox
See original GitHub issueI’m trying to store files on dropbox for my and have several questions. On production I used just this:
import { Meteor } from 'meteor/meteor';
import { FilesCollection } from 'meteor/ostrio:files';
const Images = new FilesCollection({
collectionName: 'Images',
allowClientCode: false, // Disallow remove files from Client
onBeforeUpload(file) {
// Allow upload files under 10MB, and only in png/jpg/jpeg formats
if (file.size <= 10485760 && /png|jpg|jpeg/i.test(file.extension)) {
return true;
}
return 'Please upload image, with size equal or less than 10MB';
}
});
if (Meteor.isClient) {
Meteor.subscribe('files.images.all');
}
if (Meteor.isServer) {
Meteor.publish('files.images.all', function () {
return Images.find().cursor;
});
}
To integrate dropbox I tried slapping this from https://github.com/VeliovGroup/Meteor-Files/wiki/DropBox-Integration in
var Dropbox, Request, bound, client, fs, Collections = {};
if (Meteor.isServer) {
Dropbox = Npm.require('dropbox');
Request = Npm.require('request');
fs = Npm.require('fs');
bound = Meteor.bindEnvironment(function(callback) {
return callback();
});
client = new Dropbox.Client({
key: 'xxx',
secret: 'xxx',
token: 'xxxxxxxxxxxxxxxxxx'
});
}
Collections.files = new FilesCollection({
debug: false, // Change to `true` for debugging
storagePath: 'assets/app/uploads/uploadedFiles',
collectionName: 'uploadedFiles',
allowClientCode: false,
onAfterUpload: function(fileRef) {
// In onAfterUpload callback we will move file to DropBox
var self = this;
var makeUrl = function(stat, fileRef, version, triesUrl) {
if (triesUrl == null) {
triesUrl = 0;
}
client.makeUrl(stat.path, {
long: true,
downloadHack: true
}, function(error, xml) {
// Store downloadable link in file's meta object
bound(function() {
if (error) {
if (triesUrl < 10) {
Meteor.setTimeout(function() {
makeUrl(stat, fileRef, version, ++triesUrl);
}, 2048);
} else {
console.error(error, {
triesUrl: triesUrl
});
}
} else if (xml) {
var upd = {
$set: {}
};
upd['$set']["versions." + version + ".meta.pipeFrom"] = xml.url;
upd['$set']["versions." + version + ".meta.pipePath"] = stat.path;
self.collection.update({
_id: fileRef._id
}, upd, function(error) {
if (error) {
console.error(error);
} else {
// Unlink original files from FS
// after successful upload to DropBox
self.unlink(self.collection.findOne(fileRef._id), version);
}
});
} else {
if (triesUrl < 10) {
Meteor.setTimeout(function() {
makeUrl(stat, fileRef, version, ++triesUrl);
}, 2048);
} else {
console.error("client.makeUrl doesn't returns xml", {
triesUrl: triesUrl
});
}
}
});
});
};
var writeToDB = function(fileRef, version, data, triesSend) {
// DropBox already uses random URLs
// No need to use random file names
if (triesSend == null) {
triesSend = 0;
}
client.writeFile(fileRef._id + "-" + version + "." + fileRef.extension, data, function(error, stat) {
bound(function() {
if (error) {
if (triesSend < 10) {
Meteor.setTimeout(function() {
writeToDB(fileRef, version, data, ++triesSend);
}, 2048);
} else {
console.error(error, {
triesSend: triesSend
});
}
} else {
// Generate downloadable link
makeUrl(stat, fileRef, version);
}
});
});
};
var readFile = function(fileRef, vRef, version, triesRead) {
if (triesRead == null) {
triesRead = 0;
}
fs.readFile(vRef.path, function(error, data) {
bound(function() {
if (error) {
if (triesRead < 10) {
readFile(fileRef, vRef, version, ++triesRead);
} else {
console.error(error);
}
} else {
writeToDB(fileRef, version, data);
}
});
});
};
var sendToStorage = function(fileRef) {
_.each(fileRef.versions, function(vRef, version) {
readFile(fileRef, vRef, version);
});
};
sendToStorage(fileRef);
},
interceptDownload: function(http, fileRef, version) {
var path, ref, ref1, ref2;
path = (ref = fileRef.versions) != null ? (ref1 = ref[version]) != null ? (ref2 = ref1.meta) != null ? ref2.pipeFrom : void 0 : void 0 : void 0;
if (path) {
// If file is moved to DropBox
// We will pipe request to DropBox
// So, original link will stay always secure
Request({
url: path,
headers: _.pick(http.request.headers, 'range', 'accept-language', 'accept', 'cache-control', 'pragma', 'connection', 'upgrade-insecure-requests', 'user-agent')
}).pipe(http.response);
return true;
} else {
// While file is not yet uploaded to DropBox
// We will serve file from FS
return false;
}
}
});
if (Meteor.isServer) {
// Intercept File's collection remove method
// to remove file from DropBox
var _origRemove = Collections.files.remove;
Collections.files.remove = function(search) {
var cursor = this.collection.find(search);
cursor.forEach(function(fileRef) {
_.each(fileRef.versions, function(vRef) {
var ref;
if (vRef != null ? (ref = vRef.meta) != null ? ref.pipePath : void 0 : void 0) {
client.remove(vRef.meta.pipePath, function(error) {
bound(function() {
if (error) {
console.error(error);
}
});
});
}
});
});
// Call original method
_origRemove.call(this, search);
};
}
I get this error:
W20190905-13:42:46.552(2)? (STDERR) C:\Users\NERV\AppData\Local\.meteor\packages\meteor-tool\1.8.1\mt-os.windows.x86_64\dev_bundle\server-lib\node_modules\fibers\future.js:280
W20190905-13:42:46.554(2)? (STDERR) throw(ex);
W20190905-13:42:46.555(2)? (STDERR) ^
W20190905-13:42:46.556(2)? (STDERR)
W20190905-13:42:46.557(2)? (STDERR) Error: Cannot find module "request"
W20190905-13:42:46.558(2)? (STDERR) at Object.require (C:\Users\NERV\Desktop\Projekte\DDCFull\.meteor\local\build\programs\server\boot.js:303:24)
W20190905-13:42:46.558(2)? (STDERR) at images.js (imports/api/images/images.js:11:17)
W20190905-13:42:46.559(2)? (STDERR) at fileEvaluate (packages\modules-runtime.js:336:7)
W20190905-13:42:46.560(2)? (STDERR) at Module.require (packages\modules-runtime.js:238:14)
W20190905-13:42:46.561(2)? (STDERR) at Module.moduleLink [as link] (C:\Users\NERV\AppData\Local\.meteor\packages\modules\0.13.0\npm\node_modules\reify\lib\runtime\index.js:38:38)
W20190905-13:42:46.562(2)? (STDERR) at methods.js (imports/api/characters/methods.js:1:463)
W20190905-13:42:46.565(2)? (STDERR) at fileEvaluate (packages\modules-runtime.js:336:7)
W20190905-13:42:46.566(2)? (STDERR) at Module.require (packages\modules-runtime.js:238:14)
W20190905-13:42:46.567(2)? (STDERR) at Module.moduleLink [as link] (C:\Users\NERV\AppData\Local\.meteor\packages\modules\0.13.0\npm\node_modules\reify\lib\runtime\index.js:38:38)
W20190905-13:42:46.568(2)? (STDERR) at main.js (server/main.js:1:146)
W20190905-13:42:46.568(2)? (STDERR) at fileEvaluate (packages\modules-runtime.js:336:7)
W20190905-13:42:46.569(2)? (STDERR) at Module.require (packages\modules-runtime.js:238:14)
W20190905-13:42:46.570(2)? (STDERR) at require (packages\modules-runtime.js:258:21)
W20190905-13:42:46.571(2)? (STDERR) at C:\Users\NERV\Desktop\Projekte\DDCFull\.meteor\local\build\programs\server\app\app.js:4673:1
W20190905-13:42:46.571(2)? (STDERR) at C:\Users\NERV\Desktop\Projekte\DDCFull\.meteor\local\build\programs\server\boot.js:419:36
W20190905-13:42:46.573(2)? (STDERR) at Array.forEach (<anonymous>)
So since it cannot find the module “request” I tried downloading this package: https://www.npmjs.com/package/require But I’m not sure if thats what it needs and if it does how to import it. I can’t seem to find it in my node-modules directory.
How can I import this needed module into the file? Thank you for any help!
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Secure, scalable infrastructure - Dropbox Business
Dropbox's key management infrastructure is designed with operational, technical, and procedural security controls with very limited direct access to keys.
Read more >Cloud storage abstraction with Object Store - Dropbox Tech Blog
Object Store doesn't directly implement data storage. Instead, it acts as an abstraction over multiple storage backends, routing PUTs to the ...
Read more >Integrating Dropbox for persistent user data storage in ...
In this post, we show you how to allow seamless access to Dropbox storage from your AppStream 2.0 sessions. This solution uses Active...
Read more >Dropbox's persistent account upgrade prompt - GoodUX
Dropbox uses persistent yet subtle upgrade prompts to convert free users to their premium cloud-based storage service.
Read more >Persistent "Dropbox is full" error despite purging most files
Using Dropbox basic with 5GB storage I am getting continued error message that I'm over the storage limit. Several days ago I deleted...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@TomokoOG no worries, it’s always better to ask, than waste your time 😉 Hope I helped you at least a little
Okay, thanks for the reply. I guess I try something else then.
Is the google cloud storage guide still up to date? https://github.com/VeliovGroup/Meteor-Files/wiki/Google-Cloud-Storage-Integration