C # Use MD5 to determine the existence of files in Gridfs

Source: Internet
Author: User
Tags hash md5 md5 hash mongodb

Background

Assuming that using MongoDB's Gridfs to do a distributed file system, the same file is stored in a file system, then the file will need to determine whether the file already exists, in Gridfs each file has a unique MD5 hash value, Only need to use the MD5 value of the file to determine whether in the Gridfs already exist on it, the so-called second-pass function is to use this principle.

Technology stack

C #, VS2013, MongoDB, Gridfs

Realize

1. First get the MD5 value according to the file stream, the code is as follows:

The code is as follows Copy Code

MD5 code for the computed file
public static string Getmd5hash (Stream stream)
{
string result = "";
String hashdata = "";
Byte[] Arrbythashvalue;
System.Security.Cryptography.MD5CryptoServiceProvider Md5hasher =
New System.Security.Cryptography.MD5CryptoServiceProvider ();

Try
{
Arrbythashvalue = Md5hasher.computehash (stream);//Compute the hash value of the specified Stream object
A string of hexadecimal pairs separated by hyphens in which each pair represents the corresponding element in value; for example, "f-2c-4a"
Hashdata = System.BitConverter.ToString (Arrbythashvalue);
Replace
Hashdata = Hashdata.replace ("-", "");
result = Hashdata;
}
catch (System.Exception ex)
{
Record log
}

return result;
}

2. The uniqueness checksum in the Gridfs is based on the MD5 value, as follows:

The code is as follows Copy Code
         public string isexist (string tag)
        {
            String id = "";
            var files = _gridfs.find (Query.eq ("MD5"), Bsonvalue.create (tag));
            if (files. Count () > 0)
            {
                 Id=files. FirstOrDefault (). Id.tostring ();
           }
            return ID;
       }

3. The complete code reference for the Gridfs action class

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.