如何将上传的文件存储在文件系统中?
我试图找出将用户上传的文件存储在文件系统中的最佳方法。这些文件的范围从个人文件到wiki文件。当然,DB会指向那些我还没弄明白的文件。如何将上传的文件存储在文件系统中?
基本要求:
- 童话体面的安全,使人们无法猜测文件名 (Picture001.jpg,Picture002.jpg, Music001.mp3是一个很大的不)
- 方便地备份& Mirrorable(我更喜欢这种方式,所以我不需要每一次都需要备份整个硬盘,我喜欢只备份最新产品的想法,但我可以灵活选择这里的选项。)
- 如果需要,可扩展到多台服务器上的数百万个文件
一种技术是将数据存储在以其内容的散列(SHA1)命名的文件中。这是不容易猜测的,任何备份程序都应该能够处理它,并且它很容易被分割(通过在一台机器上存储从0开始的散列,在下一个从1开始的散列等)。
数据库将包含用户分配的名称和内容的SHA1哈希之间的映射。
SHA1散列的文件名+盐(或者,如果需要的话,可以使文件内容更容易检测到重复文件,但也会给服务器带来更多压力)。这可能需要进行一些调整才是唯一的(即添加上传的用户ID或时间戳),而盐则使其不可猜测。
文件夹结构然后是散列的部分。
例如,如果哈希是“2fd4e1c67a2d28fced849ee1bb76e7391b93eb12”,那么文件夹可以是:
/2
/2/2f/
/2/2f/2fd/
/2/2f/2fd/2fd4e1c67a2d28fced849ee1bb76e7391b93eb12
这是为了防止大的文件夹(某些操作系统有麻烦enumarating文件夹与文件的一百万,因此制作几个子文件夹的部分散列,有多少层次?这取决于您期望的文件数量,但2或3通常是合理的。
仅就您的问题(安全性)的一个方面而言:将上传的文件安全地存储在文件系统中是为了确保上传的文件不在webroot中(即,您可以'通过URL直接访问它们 - 你必须通过一个脚本)。
这使您可以完全控制人们可以下载(安全)的内容并允许诸如日志记录之类的内容。当然,你必须确保脚本本身是安全的,但这意味着只有你允许的人才能够下载某些文件。
文件名指南,每个文件夹中不超过几千个文件/文件夹自动扩展文件夹层次结构。备份新文件是通过备份新文件夹完成的。
你还没有注明你使用的是什么环境和/或编程语言,但是这是一个C#/。净/ Windows示例:
using System;
using System.IO;
using System.Xml.Serialization;
/// <summary>
/// Class for generating storage structure and file names for document storage.
/// Copyright (c) 2008, Huagati Systems Co.,Ltd.
/// </summary>
public class DocumentStorage
{
private static StorageDirectory _StorageDirectory = null;
public static string GetNewUNCPath()
{
string storageDirectory = GetStorageDirectory();
if (!storageDirectory.EndsWith("\\"))
{
storageDirectory += "\\";
}
return storageDirectory + GuidEx.NewSeqGuid().ToString() + ".data";
}
public static void SaveDocumentInfo(string documentPath, Document documentInfo)
{
//the filestream object don't like NTFS streams so this is disabled for now...
return;
//stores a document object in a separate "docinfo" stream attached to the file it belongs to
//XmlSerializer ser = new XmlSerializer(typeof(Document));
//string infoStream = documentPath + ":docinfo";
//FileStream fs = new FileStream(infoStream, FileMode.Create);
//ser.Serialize(fs, documentInfo);
//fs.Flush();
//fs.Close();
}
private static string GetStorageDirectory()
{
string storageRoot = ConfigSettings.DocumentStorageRoot;
if (!storageRoot.EndsWith("\\"))
{
storageRoot += "\\";
}
//get storage directory if not set
if (_StorageDirectory == null)
{
_StorageDirectory = new StorageDirectory();
lock (_StorageDirectory)
{
string path = ConfigSettings.ReadSettingString("CurrentDocumentStoragePath");
if (path == null)
{
//no storage tree created yet, create first set of subfolders
path = CreateStorageDirectory(storageRoot, 1);
_StorageDirectory.FullPath = path.Substring(storageRoot.Length);
ConfigSettings.WriteSettingString("CurrentDocumentStoragePath", _StorageDirectory.FullPath);
}
else
{
_StorageDirectory.FullPath = path;
}
}
}
int fileCount = (new DirectoryInfo(storageRoot + _StorageDirectory.FullPath)).GetFiles().Length;
if (fileCount > ConfigSettings.FolderContentLimitFiles)
{
//if the directory has exceeded number of files per directory, create a new one...
lock (_StorageDirectory)
{
string path = GetNewStorageFolder(storageRoot + _StorageDirectory.FullPath, ConfigSettings.DocumentStorageDepth);
_StorageDirectory.FullPath = path.Substring(storageRoot.Length);
ConfigSettings.WriteSettingString("CurrentDocumentStoragePath", _StorageDirectory.FullPath);
}
}
return storageRoot + _StorageDirectory.FullPath;
}
private static string GetNewStorageFolder(string currentPath, int currentDepth)
{
string parentFolder = currentPath.Substring(0, currentPath.LastIndexOf("\\"));
int parentFolderFolderCount = (new DirectoryInfo(parentFolder)).GetDirectories().Length;
if (parentFolderFolderCount < ConfigSettings.FolderContentLimitFolders)
{
return CreateStorageDirectory(parentFolder, currentDepth);
}
else
{
return GetNewStorageFolder(parentFolder, currentDepth - 1);
}
}
private static string CreateStorageDirectory(string currentDir, int currentDepth)
{
string storageDirectory = null;
string directoryName = GuidEx.NewSeqGuid().ToString();
if (!currentDir.EndsWith("\\"))
{
currentDir += "\\";
}
Directory.CreateDirectory(currentDir + directoryName);
if (currentDepth < ConfigSettings.DocumentStorageDepth)
{
storageDirectory = CreateStorageDirectory(currentDir + directoryName, currentDepth + 1);
}
else
{
storageDirectory = currentDir + directoryName;
}
return storageDirectory;
}
private class StorageDirectory
{
public string DirectoryName { get; set; }
public StorageDirectory ParentDirectory { get; set; }
public string FullPath
{
get
{
if (ParentDirectory != null)
{
return ParentDirectory.FullPath + "\\" + DirectoryName;
}
else
{
return DirectoryName;
}
}
set
{
if (value.Contains("\\"))
{
DirectoryName = value.Substring(value.LastIndexOf("\\") + 1);
ParentDirectory = new StorageDirectory { FullPath = value.Substring(0, value.LastIndexOf("\\")) };
}
else
{
DirectoryName = value;
}
}
}
}
}
扩展在菲尔圣的回答,安全的另一个方面是使用一个单独的域名上传的文件(instante,维基百科使用upload.wikimedia.org),并确保该网域无法读取任何网站的Cookie。这样可以防止用户通过脚本上传HTML文件来窃取用户的会话cookie(只需设置Content-Type标头是不够的,因为some browsers已知可以忽略它并根据文件内容进行猜测;它也可以是嵌入到其他类型的文件中,因此检查HTML并禁止它不是微不足道的)。
哈希内容?不想那样做,太耗费资源... – KristoferA 2008-10-22 10:25:34