Recently, you need to upload images of your website to cloud storage. so I found qiniu. see the above SDK... I found that there are few comments in it. at that time, I looked at the top. there are basically no good examples for searching on the Internet. great inconvenience for beginners! Finally, I made some basic examples here, hoping to help beginners.
Step 1. assign a value first. The Code is as follows (you should be able to understand it)
Config. access_key = ""; // AK config. secret_key = ""; // SK bucket = ""; // space Name Domain = ""; // Domain Name
Step 2. Upload the File Code as follows:
public static bool PutFile(string path) { bool flag = false; Settings putSetting = new Settings(); ResumablePutExtra extra = new ResumablePutExtra(); NameValueCollection nc = new NameValueCollection(); nc.Add("x:username", "qiniu"); extra.CallbackParams = nc; ResumablePut target = new ResumablePut(putSetting, extra); string upToken = new PutPolicy(Bucket).Token(new Qiniu.Auth.digest.Mac()); string key = path.Substring(path.LastIndexOf(‘/‘) + 1, path.Length - path.LastIndexOf(‘/‘) - 1); target.PutFinished += new EventHandler<CallRet>((o, e) => { if (e.OK) { flag = true; } }); CallRet ret = target.PutFile(upToken, path, key); return flag; }
String key = path. substring (path. lastindexof ('/') + 1, path. length-path. lastindexof ('/')-1); // I processed the image. I keep the original image name. The other key is the resource name.
Put so many generations on the previous page in a class. Then you can call it. This is super simple!
The call method is as follows:
Class Name. putfile (savepath) // If static is added, savepath can be directly called, which is the path of the file to be uploaded (local path)