One or seven KN custom configuration node
<configSections> <section name ="Qiniuconfig"Type="Amy.Toolkit.QiniuStorage.SectionHandler"/> </configSections> <QiniuConfig> <add key="AccessKey"Value="their own accesskey."></add> <add key="Secretkey"Value="their own secretkey."></add> <add key="Buckets"Value="Space name"></add> <add key="Domain"Value="seven KN domain name/own domain name"></add> </QiniuConfig>
II. implementation of custom configuration functions
Public classConfig {/// <summary> ///Seven bull public key/// </summary> Public stringAccessKey {Get;Set; } /// <summary> ///Seven Bull key/// </summary> Public stringSecretkey {Get;Set; } /// <summary> ///Resource Space/// </summary> Public stringBucket {Get;Set; } /// <summary> ///Space Domain name/// </summary> Public stringDomain {Get;Set; } } Public classSectionhandler:iconfigurationsectionhandler { Public ObjectCreate (ObjectParentObjectconfigcontext, XmlNode section) {Config config=NewConfig (); //parsing profile information, returning objects if(Section! =NULL) { foreach(XmlNode IteminchSection . ChildNodes) {Switch(item. attributes["Key"]. InnerText) { Case "AccessKey": CONFIG. AccessKey= Item. attributes["value"]. InnerText; Break; Case "Secretkey": CONFIG. Secretkey= Item. attributes["value"]. InnerText; Break; Case "Buckets": CONFIG. Buckets= Item. attributes["value"]. InnerText; Break; Case "Domain": CONFIG. Domain= Item. attributes["value"]. InnerText; Break; default: Break; } } } returnconfig; } }
View Code
Third, the realization of seven of cattle upload image function
Public classQiniufactory {/// <summary> ///Upload Image/// </summary> /// <param name= "Stream" >Picture Stream</param> /// <returns>Seven cow picture path</returns> Public Static stringuploadimage (System.IO.Stream Stream) {config config= (Config) configurationmanager.getsection ("Qiniuconfig"); //set up an account for AK and SKQiniu.Conf.Config.ACCESS_KEY =CONFIG. AccessKey; Qiniu.Conf.Config.SECRET_KEY=CONFIG. Secretkey; Ioclient Target=Newioclient (); PutExtra Extra=NewPutExtra (); //set the upload spaceString bucket =CONFIG. Buckets; //Set the key value (and file name) of the uploaded fileString key = Idhelper.id32 +". jpg"; //normal upload, only need to set the upload space name on it, the second parameter can be set token expiration timePutpolicy put =NewPutpolicy (Bucket,3600); //call the token () method to generate the uploaded token stringUptoken =put. Token (); //the path to the uploaded file//string filePath = @ "F:\130326213588cbde762953a234.jpg"; //Call the Putfile () method to upload//putret ret = target. Putfile (Uptoken, Key, FilePath, extra); varRET =Target. Put (Uptoken, key, Stream, extra); return string. IsNullOrEmpty (Ret.key)?string. Empty:string. Format ("{0}/{1}", CONFIG. Domain, Ret.key); } /// <summary> ///Upload Image/// </summary> /// <param name= "Stream" >Picture Stream</param> /// <returns>Seven cow picture path</returns> Public Static stringUploadimage (byte[] bytes) {Config config= (Config) configurationmanager.getsection ("Qiniuconfig"); //set up an account for AK and SKQiniu.Conf.Config.ACCESS_KEY =CONFIG. AccessKey; Qiniu.Conf.Config.SECRET_KEY=CONFIG. Secretkey; Ioclient Target=Newioclient (); PutExtra Extra=NewPutExtra (); //set the upload spaceString bucket =CONFIG. Buckets; //Set the key value (and file name) of the uploaded fileString key = Idhelper.id32 +". jpg"; //normal upload, only need to set the upload space name on it, the second parameter can be set token expiration timePutpolicy put =NewPutpolicy (Bucket,3600); //call the token () method to generate the uploaded token stringUptoken =put. Token (); //the path to the uploaded file//string filePath = @ "F:\130326213588cbde762953a234.jpg"; //Call the Putfile () method to upload//putret ret = target. Putfile (Uptoken, Key, FilePath, extra); varRET = target. Put (Uptoken, Key,NewSystem.IO.MemoryStream (bytes), extra); return string. IsNullOrEmpty (Ret.key)?string. Empty:string. Format ("{0}/{1}", CONFIG. Domain, Ret.key); } }
View Code
Iv. client calls seven KN
/// <summary> ///Seven cow upload file/// </summary> Public stringQiniuuploadfile () {varURL ="Seven cow picture access path"; varFiles =Request.Files; if(Files. Count >0) {URL= Amy.Toolkit.QiniuStorage.QiniuFactory.UploadImage (files[0]. InputStream); } returnURL; }
Seven Bull pictures Cloud storage configuration and examples