.Net平台下,分布式檔案儲存體的實現

來源:互聯網
上載者:User

標籤:blog   http   使用   os   io   檔案   資料   for   

遇到的問題 

對於Web程式,使用一台伺服器的時候,用戶端上傳的檔案一般也都是儲存在這台伺服器上。但在叢集環境中就行不通了,如果每個伺服器都儲存自己接受到的檔案,就亂套了,資料庫中明明有這個附件的記錄,卻找不到這個檔案。於是,檔案需要進行統一集中管理,並向叢集中的伺服器提供統一的路徑。

基於NFS的分布式檔案儲存體實現

Network File System 簡稱NFS,用人話說叫共用資料夾,可以實現分布式隱藏檔。只需要在檔案伺服器上共用資料夾,並指定相應帳號的許可權,並給Web伺服器設定可以訪問共用資料夾的帳號和密碼,web伺服器就可以像操作本地檔案一樣操作檔案伺服器上的檔案了。NFS下的檔案訪問路徑有固定的格式,稱為UNC(Universal Naming Convention),以“\\”開頭。

要以UNC的方式訪問NFS下的檔案,需要用到windows提供的兩個API:WNetAddConnection2 和 WNetCancelConnection2。WNetAddConnection2可以使用指定的帳號和密碼建立一個UNC的串連,然後程式可以直接存取該UNC下檔案。

首先建立類FileServerConnection,用於管理串連,具體代碼如下:

public class FileServerConnection{    private string uncName;    private string username;    private string password;    /// <summary>    /// 構造器    /// </summary>    /// <param name="uncName">完整的UNC路徑</param>    /// <param name="username">訪問共用串連的使用者名稱</param>    /// <param name="password">訪問共用串連的密碼</param>    public FileServerConnection(string uncName, string username, string password)    {        this.uncName = uncName;        this.username = username;        this.password = password;    }    /// <summary>    /// 串連檔案伺服器    /// </summary>    public void Connect()    {        var netResource = new NetResource                    {                        Scope = ResourceScope.GlobalNetwork,                        ResourceType = ResourceType.Disk,                        DisplayType = ResourceDisplayType.Share,                        RemoteName = this.uncName.TrimEnd(‘\\‘)                    };        var result = WNetAddConnection2(netResource, password, username, 0);        if (result != 0)            throw new Win32Exception(result);    }    /// <summary>    /// 釋放串連    /// </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);}

FileServerConnection中所用到的幾個結構體代碼如下:

[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}

然後在Web程式啟動的時候,只需要建立一個FileServerConnection的執行個體,然後調用它的Connect方法。為了防止重複建立串連引發異常,可以Connect之前先DisConnect。具體調用代碼如下: 

fsConnection= new FileServerConnection (storeRootPath, username, password);fsConnection.Disconnect();fsConnection.Connect();
基於DFS分布式儲存方案一台檔案儲存體伺服器+一塊大磁碟,已經能夠應付大多數情況了。如果想要更大的儲存容量、更大的輸送量、更安全可靠的檔案部署方案,可以使用windows server上的DFS。DFS本質上還是基於檔案夾共用(NFS)的,但是可以通過它來組織多太檔案伺服器提供統一的訪問路徑,從而支援大容量和大輸送量。而且,可以配置把同一份檔案儲存體在不同的伺服器上,一台掛掉之後,檔案仍然不會丟失;DFS還可以和活動目錄集成,即使根伺服器掛掉,仍然不影響伺服器使用,從而 保證檔案儲存體的可靠性。Windows Server下DFS的安裝和使用參考:http://technet.microsoft.com/zh-cn/library/cc731089.aspx#BKMK_UI如果不想使用Windows內建的DFS,還有第三方的DFS可供選擇,比如 FastDFS。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.