日期:2014-05-16  浏览次数:20521 次

自己做一个类似mongofiles的命令行工具来操作GridFS

为什么要自己做一个呢?

因为mongodb自带的mongofiles到现在也不能操作和查看某个bucket的下的文件, 只能查看和操作fs下的文件. mongo-java-driver中的那个CLI也不能做这个, 甚至连用户名和密码都不能指定.

?

所以不如我们自己做一个吧. 代码如下, 不多说了:

?

import com.mongodb.DB;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSInputFile;
import com.mongodb.util.Util;

import java.io.File;
import java.security.DigestInputStream;
import java.security.MessageDigest;

/**
 * User: matianyi
 * Date: 13/05/02
 */
public class CLI {
    /**
     * Dumps usage info to stdout
     */
    private static void printUsage() {
        System.out.println("Usage : -h x.x.x.x -u userid -p password -d db -b bucketname action");
        System.out.println("  where  action is one of:");
        System.out.println("      list                      : lists all files in the store");
        System.out.println("      put filename              : puts the file filename into the store");
        System.out.println("      get filename              : gets filename1 from store");
        System.out.println("      md5 filename              : does an md5 hash on a file in the db (for testing)");
        System.out.println("      del filename              : delete filename1 from store");
    }

    private static String host = "127.0.0.1";
    private static String dbName = "test";
    private static String usr = "";
    private static String pwd = "";
    private static String bucket = "";

    private static Mongo _mongo = null;

    private static Mongo getMongo()
            throws Exception {
        if (_mongo == null) {
            _mongo = new Mongo(host);
        }
        return _mongo;
    }

    private static GridFS _gridfs;

    private static GridFS getGridFS()
            throws Exception {
        if (_gridfs == null) {
            DB db1 = getMongo().getDB(dbName);

            if (!usr.equals("")) {
                db1.authenticate(usr, pwd.toCharArray());
            }

            if (!bucket.equals("")) {
                _gridfs = new GridFS(db1, bucket);
            } else {
                _gridfs = new GridFS(db1);
            }
        }
        return _gridfs;
    }

    public static void main(String[] args) throws Exception {

        if (args.length < 1) {
            printUsage();
            return;
        }

        for (int i = 0; i < args.length; i++) {
            String s = args[i];

            if (s.equals("-d")) {
                dbName = args[i + 1];
                i++;
                continue;
            }

            if (s.equals("-u")) {
                usr = args[i + 1];
                i++;
                continue;
            }

            if (s.equals("-p")) {
                pwd = args[i + 1];
                i++;
                continue;
            }

            if (s.equals("-b")) {
                bucket = args[i + 1];
                i++;
                continue;
            }

            if (s.equals("-h")) {
                host = args[i + 1];
                i++;
                continue;
            }

            if (s.equals("help")) {
                printUsage();
                return;
            }

            if (s.equals("list")) {
                GridFS fs = getGridFS();

                System.out.printf("%-60s %-10s\n", "Filename", "Length");

                for (DBObject o : fs.getFileList()) {
                    System.out.printf("%-60s %-10d\n", o.get("filename"), ((Number) o.get("length")).longValue());
                }
                return;
            }

            if (s.equals("get")) {
                GridFS fs = getGridFS();
                String fn = args[i + 1];
                GridFSDBFile f = fs.findOne(fn);
                if (f == null) {
                    System.err.printl