示例#1
0
 public static void DeleteFile(string fileName, string BucketName = null)
 {
     try
     {
         var  nosClient = new NosClient(NosConfig.EndPoint, NosConfig.AccessKey, NosConfig.SecretKey);
         bool exist     = nosClient.DoesObjectExist(NosConfig.BucketName, fileName);
         if (exist)
         {
             nosClient.DeleteObject(NosConfig.BucketName, fileName);
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
示例#2
0
        public string NEAPI()
        {
            string cmd    = RouteData.Values["id"]?.ToString().ToLower();
            string bucket = Request.Form["bucket"].ToString();
            string result = "fail";

            var nosClient = new NosClient(AccessNOS.EndPoint, AccessNOS.AccessKeyId, AccessNOS.AccessKeySecret, new ClientConfiguration()
            {
                MaxErrorRetry     = 2,
                MaxConnections    = 200,
                ConnectionTimeout = 50000
            });

            switch (cmd)
            {
                #region 列举文件
            case "list":
                var listObjectsRequest = new ListObjectsRequest(bucket)
                {
                    MaxKeys = 1000,
                    Prefix  = Request.Form["keywords"].ToString()
                };
                try
                {
                    ObjectListing objList = nosClient.ListObjects(listObjectsRequest);
                    result = objList.ObjectSummarise.ToJson();
                }
                catch (Exception ex)
                {
                    FilterConfigs.WriteLog(HttpContext, ex);
                    result = "[]";
                }
                break;
                #endregion

                #region 存在文件
            case "exists":
                try
                {
                    string key = Request.Form["key"].ToString();
                    bool   b   = nosClient.DoesObjectExist(bucket, key);
                    result = b ? "1" : "0";
                }
                catch (Exception)
                {
                    result = "error";
                }
                break;
                #endregion

                #region  除文件
            case "del":
                try
                {
                    var keys = Request.Form["key"].ToString().Split(',').ToList();
                    if (keys.Count == 1)
                    {
                        nosClient.DeleteObject(bucket, keys[0]);
                    }
                    else if (keys.Count > 1)
                    {
                        nosClient.DeleteObjects(bucket, keys, false);
                    }
                    result = "success";
                }
                catch (Exception)
                {
                    result = "fail";
                }
                break;
                #endregion
            }

            return(result);
        }