. NET simple picture system-local storage and distributed storage

Source: Internet
Author: User

Local storage The so-called local storage is to save the uploaded image on the local disk of the picture server.
if(Confighelper.getconfigstring ("Savemode") =="Local")//indicates that the store is in local mode            {                varhostname = Request. querystring["Host"] ??""; if(hostname. StartsWith ("www.") ) {hostname= hostname. Remove (0,4); }                stringSavepath =string. Join ("\\", hostname. Split ('.').                Reverse ()); Savepath+="\\Upload\\"+Getpathrule (); stringLocalPath =Path.Combine (Httpruntime.appdomainapppath, Savepath); if(!directory.exists (LocalPath))                {directory.createdirectory (LocalPath); }                 stringex =path.getextension (file.                 FileName); if(Confighelper.getconfigstring ("Savefilerule") =="Guid") {filename= Guid.NewGuid (). ToString ("N") +ex; }                Else{filename= DateTime.Now.ToString ("yyyymmddhhmmssfff") +ex; } file.                SaveAs (Path.Combine (localPath, filename)); FileName= Request. Url.scheme +"://"+ Request. Url.authority +"/"+ Savepath.replace ("\\","/") +"/"+filename; }  
View Code 1. In the local storage mode, in order to distinguish between different business sites uploaded images, where the domain name is used as a part of the image storage path, such as the domain name is www.yourdomain.com&nbsp, the upload image storage path should be such \com\yourdomain (www omitted ), if it is passport.yourdomain.com, the storage path is \com\yourdomain\passport. 2. At the same time, in order to facilitate the collation of the file grouping in a temporal manner, support YYYY, YYYYYMM,YYYYYMMDD three modes, if the choice is yyyy, the whole year of the picture will be saved in the folder 2015.  3. For file names, there are two ways to name them, GUIDs and time, and time is accurate to milliseconds.   finally returns the picture path.   Distributed storage distributed storage is focused on Fastdfs and requires the use of a Linux system, where the CentOS6.5 version is used to install the virtual machine.   Install file point here to download the installation package   installation steps
Software Environment: VMware Workstation One, CentO 6.5 package: Because it is a virtual machine environment, it is recommended to log in directly to root, because many operations under root can be done directly under the interface, for those who are not familiar with Linux easy to operate, of course, the formal environment, but also is to use the terminal to operate under the ordinary user. 1. Copy the fastdfs_v5.05.tar.gz, Libfastcommon.master.zip, nginx-1.8.0.tar.gz to the root folder and unzip 2. Copy the pcre-8.34.tar.gz, zlib-1.2.8.tar.gz, fastdfs-nginx-module_v1.16.tar.gz to the/usr/local/src folder and Unzip 3. Create a new folder for Fastdfs users to store logs and data, and Nginx folder 1. Create a new Tracker folder under the/opt folder 2. Create a new Storage_info folder under the/opt folder 3. Create a new Storage_data folder under the/opt folder 4. Create a new Client folder under the/opt folder 5. Create a new Nginx folder under/usr/local 4. Perform yum install-y gcc yum install-y gcc-c++ yum install-y pcre pcre-devel y in the terminal Um install-y zlib zlib-devel yum install-y OpenSSL openssl-devel5. Go to the/root folder under the extracted Libfastcommon-master folder, execute./make.sh, execute after successful execution./make.sh install, if there is no error, Libfastcommon installation successful note, The path of the installation above is/usr/lib64/, but the Lib directory fastdfs the main program settings is/usr/local/lib, so a soft connection needs to be created as follows: Ln-s/usr/lib64/libfastcommon.so/usr/local/ lib/libfastcommon.so ln-s/usr/lib64/libfastcommon.so/usr/lib/libfastcommon.so ln-s/usr/lib64/libfdfsclient.so/usr/local/lib/libfdfsclient.so ln-s/usr/lib64/libfdfsclient.so/usr /lib/libfdfsclient.so6. Go to the root folder after extracting the Fastdfs folder, execute./make.sh, execute after successful execution./make.sh install, if there is no error and the configuration file can be found under the/etc/fast folder ( Client.conf.simple,storage.conf.simple,tracker.conf.simple) indicates that the installation was successful. 7. Go to the/etc/fast folder 1. Rename Tracker.conf.simple to tracker.conf, which is actually to remove the. Simple, open the file and modify the Base_path=/opt/tracker #即刚才在/ Tracker folder path created under the OPT folder 2. Rename Storage.conf.simple to storage.conf, open the file and modify the Base_path=/opt/storage_info #即刚才在/ The Storage_info folder created under the OPT folder Store_path0=/opt/storage_data #即刚才在 storage_data folder tracker_server=192.168 created under the/opt folder. 1.104:22122 #IP地址就是tracker地址, if it is a distributed deployment, fill in the tracker IP address, if it is a single-machine deployment, fill in the native address 3.client.conf.simple renamed to Client.conf, Open the file and modify it (this step is not required, primarily to test whether the software was installed successfully) Base_path=/fdfs/client #即刚才在 the Client folder path created under the/opt folder tracker_server=192.168.1.10 4:22122 #IP地址就是tracker地址, if it is a distributed deployment, fill in the tracker IP address, and if it is a single-machine deployment, fill in the native address http.tracker_server_port=8080 8. Enter the/user/bin folder, right-start the terminal, execute the following command in the terminal fdfs_trackerd/etc/fdfs/tracker.conf start #可以在/opt/tracker folder to see if the log output is normal Fdfs_sto raged/etc/fdfs/storage.conf Start #可以在/opt/storage_info folder to see if the log output is normal, and under the/opt/storage_data folder to see if a folder is created Fdfs_test/ etc/fdfs/client.conf upload/root/1.jpg #上传文件 This 1.jpg requires pre-existing execution of the third step above, and normally returns a http://192.168.1.104:8080/group1/ M00.....jpg address, which means that the upload is successful, Fastdfs also installed normally, but this address is not accessible, want to access, also need to install Nginx and Fastdfs-nginx-module 9. About Ginx, in http://www.cnblogs.com/PurpleDream/p/4510279.html this blog post has been mentioned storage and Tracker are required to install Ngxin, storage installed Nginx, The main purpose is to provide HTTP access to the service, while resolving the storage server synchronization delay problem in group.     and tracker installed Nginx, mainly in order to provide HTTP access to reverse proxy, load balancing and caching services, if it is a single-machine environment, it should start two nginx instances, but here in order to simplify, I only installed nginx for storage. Go to the Fastdfs-nginx-modul folder under the/usr/local/src folder, go to src, open and edit config to find the line containing Core_incs, remove all the local in the path, and change to core_incs= "$ core_incs/usr/include/fastdfs/usr/include/fastcommon/"into the/root folder under the extracted nginx-1.8.0 folder, right-click on the terminal, in the terminal to execute./configure --prefix=/usr/local/nginx--ADD-MODULE=/USR/LOCAL/SRC/FASTDFS-NGINX-MODULE/SRC--with-pcre=/usr/local/src/pcre-8.34/--with-zlib=/usr/local/src/zlib-1.2.8 After success, execute make, Install10. Go to/root/fastdfs/config, copy the http.conf and mime.types under the directory to/etc/fdfs/and go to/usr/local/src/   folder, locate astdfs-nginx-module/src under mod_fastdfs.conf This file is copied to/etc/fdfs under 11. Open the mod_fastdfs.conf that you just copied to/etc/fdfs, and modify the following: Base_path=/opt/storage_info the Storage_info folder created under the/opt folder tracker_server=192.168.1.104:22122 # #即刚才在 Tracker server IP address and port number url_have_group_name= true #文件url中是否有group名 store_path_count=1 #存储路径个数, need and STORE_PA The th number matches the Storage_data folder created under Store_path0=/opt/storage_data #即刚才在/opt folder http.need_find_content_type=true #从文件扩展名查 Find file type (true when nginx) Note: This configuration is generally listed on the web, but it is not available in the fastdfs5.05 version [group1] #取消注释 group_name=group1 #取消注释 storage_server _port=23000 #取消注释 store_path_count=1 #取消注释并修改 storage_path0=/opt/storage_data #取消注释并修改 There are two group configurations at the bottom of the file because we Recognize that there is only one group, so uncomment the first group and modify it, and the others remain the same. 12. Enter/usr/local/nginx/coNF, modified nginx.conf listen 8080;     Added in the server segment: Location ~/group1/m00{root/opt/storage_data/data;    Ngx_fastdfs_module; A soft connection ln-s/opt/storage_data/data/opt/storage_data/data/m0013 is established in the terminal. After performing the above steps, the software, even if the installation is complete, executes the following command in the terminal,/USR/BIN/FDF s_trackerd/etc/fdfs/tracker.conf Restart #重启tracker/usr/bin/fdfs_storaged/etc/fdfs/storage.conf Restart #重启storag E/usr/local/nginx/sbin/nginx-c/usr/local/nginx/conf/nginx.conf #重启nginx 14. Open the CentOS browser and enter 182.168 .1.104:8,080 should be able to see the Nginx page, enter the address of the http://192.168.1.104:8080/group1/M00.....jpg we just got, if the window is closed, you can upload it again, Copy the returned address to the browser and you should see the uploaded image 15. If you want to access the above address on the physical machine and be able to perform the upload operation, you need to open the port iptables-i input-p tcp-m State--state new-m TCP--dport 22122-j ACCEPT IPTA Bles-i input-p tcp-m State--state new-m TCP--dport 23000-j ACCEPT iptables-i input-p tcp-m State--state NEW       -M TCP--dport 8080-j accept/etc/init.d/iptables save #保存 after execution, you should be able to access the image address in the browser of the physical machine.
View CodeAfter performing the above steps, we have successfully installed the FASTDFS, the formal environment installation can refer to http://www.cnblogs.com/PurpleDream/p/4510279.html. NET driver Fastdfs has. NET driver, can be downloaded on NuGet, the source is hosted on the Gtihub, there is demo, you can refer to the demo to verify that the above environment installation is normal, this time the use of this is the driver. The next thing is more simple, is to save the uploaded images to Fastdfs. Modify the configuration: Upload the image, directly call the corresponding method can be.
byte[] fileData; using(varBinaryReader =NewBinaryReader (file. InputStream)) {FileData=binaryreader.readbytes (file.                ContentLength); }                 varStoragenode =Fastdfsclient.getstoragenode (FastDfsGlobalConfig.Config.GroupName); varFileName = Fastdfsclient.uploadfile (Storagenode, FileData, path.getextension (file). FileName). TrimStart ('.')); returnConfighelper.getconfigstring ("Trackerhost") + FastDfsGlobalConfig.Config.GroupName +"/"+ FileName;
View Code

. NET simple picture system-local storage and distributed storage

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.