. NET platform, the implementation of distributed file storage

Source: Internet
Author: User

Problems encountered

For Web applications, when using a single server, the files uploaded by the client are generally stored on this server. But in the cluster environment will not work, if each server to store their own received files, it is a mess, the database clearly has this attachment record, but can not find this file. As a result, the files need to be centrally managed and provide a unified path to the servers in the cluster.

Implementation of distributed file storage based on NFS

The Network file System is referred to as NFS, and a distributed storage file can be implemented by employing words called shared folders. Simply share the folder on the file server, specify the permissions of the account, and set the Web server to access the shared folder's account and password, and the Web server can manipulate files on the file server as if it were a local file. The file access path under NFS has a fixed format, called a UNC (Universal naming convention), beginning with "\ \".

To access files under NFS in a UNC way, you need to use the two Api:wnetaddconnection2 and WNetCancelConnection2 provided by Windows. WNetAddConnection2 can create a UNC connection using the specified account and password, and the program can access the file under the UNC directly.

First create the class fileserverconnection, which is used to manage the connection, with the following code:

public class fileserverconnection{private string UNCName;    private string Username;    private string password; <summary>//constructor///</summary>//<param name= "UNCName" > Full UNC path </param>///&L  T;param name= "username" > Access to Shared connection username </param>//<param name= "password" > Access password for shared connection </param> public        Fileserverconnection (string uncname, string Username, string password) {this.uncname = UNCName;        This.username = Username;    This.password = password;  }///<summary>///Connect file server///</summary> public void Connect () {var netresource = new Netresource {Scope = resourcescope.globalnetwork, Resou Rcetype = resourcetype.disk, DisplayType = Resourcedisplaytype.share, Remote        Name = this.uncName.TrimEnd (' \ \ ')}; var result = Wnetaddconnection2 (netresource, password, username, 0);    if (Result! = 0) throw new win32exception (result); }///<summary>//Release connection///</summary> public void Disconnect () {WNetCancelConnection2    (This.uncname, 0, true);                                                    } [DllImport ("Mpr.dll")] private static extern int WNetAddConnection2 (Netresource netresource,                                                    String password, string username,    int flags); private static extern int WNetCancelConnection2 (string name, int flags, BOOL force);}

Some of the structure codes used in Fileserverconnection are as follows:

[StructLayout (layoutkind.sequential)]public class netresource{public    resourcescope Scope;    Public ResourceType resourcetype;    Public Resourcedisplaytype DisplayType;    public int Usage;    public string localname;    public string remotename;    public string Comment;    public string Provider;} public enum resourcescope{    Connected = 1,    globalnetwork,    remembered,    recent,    Context}; public enum resourcetype{Any    = 0,    Disk = 1,    Print = 2,    Reserved = 8,}public enum resourcedisplaytype{    Generic = 0x0,    Domain = 0x01,    Server = 0x02,    Share = 0x03,    File = 0x04,    Group = 0x05,    Network = 0x06,    Root = 0x07,    shareadmin = 0x08,    Directory = 0x09,    Tree = 0x0a,    ndscontainer = 0x0b}

Then, when the Web program starts, just create an instance of Fileserverconnection and call its Connect method. To prevent duplicate creation of the connection from throwing an exception, you can disconnect before connect. The specific calling code is as follows:

fsconnection= New Fileserverconnection (Storerootpath, username, password); Fsconnection.disconnect (); Fsconnection.connect ();
DFS-based distributed storage scheme a file storage server + A large disk, has been able to cope with most cases. If you want larger storage capacity, greater throughput, and a more secure and reliable file deployment scenario, you can use DFS on Windows Server. DFS is also based on folder sharing (NFS) in nature, but it can be used to organize multi-file servers to provide a unified access path that supports both large capacity and high throughput. Also, you can configure the same file to be stored on a different server, after one hangs, the file will still not be lost; Dfs can also integrate with Active Directory, even if the root server hangs up, still does not affect the server use, thereby guaranteeing the reliability of the file storage. Installation and use of DFS under Windows Server reference: Http://technet.microsoft.com/zh-cn/library/cc731089.aspx#BKMK_UI If you do not want to use Windows-brought DFS, There are also third-party DFS options, such as Fastdfs.

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.