public static void DeleteObjects()
        {
            const string accessKeyId = "<your access key id>";
            const string accessKeySecret = "<your access key secret>";
            const string endpoint = "<valid host name>";

            const string bucketName = "<your bucket name>";

            var client = new OssClient(endpoint, accessKeyId, accessKeySecret);

            try
            {
                var keys = new List<string>();
                var listResult = client.ListObjects(bucketName);
                foreach (var summary in listResult.ObjectSummaries)
                {
                    Console.WriteLine(summary.Key);
                    keys.Add(summary.Key);
                }
                var request = new DeleteObjectsRequest(bucketName, keys, false);
                client.DeleteObjects(request);
            }
            catch (OssException ex)
            {
                Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
                    ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }
예제 #2
0
        public static void DeleteObjects()
        {
            var client = new OssClient(Endpoint, AccessId, AccessKey);

            try
            {
                var keys = new List<string>();
                var listResult = client.ListObjects(BucketName);
                foreach (var summary in listResult.ObjectSummaries)
                {
                    Console.WriteLine(summary.Key);
                    keys.Add(summary.Key);
                }
                var request = new DeleteObjectsRequest(BucketName, keys, false);
                client.DeleteObjects(request);
            }
            catch (OssException ex)
            {
                Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
                    ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }
        /// <inheritdoc/>
        public async Task DeleteContainer(string containerName)
        {
            try
            {
                //删除目录等于删除该目录下的所有文件
                var blobs = await ListBlobs(containerName);

                await Task.Run(() =>
                {
                    var count = blobs.Count / 1000 + (blobs.Count % 1000 > 0 ? 1 : 0);

                    for (var i = 0; i < count; i++)
                    {
                        var request = new DeleteObjectsRequest(_cfg.BucketName,
                                                               blobs.Skip(i * 1000).Take(1000).Select(p => $"{containerName}/{p.Name}").ToList());

                        _ossClient.DeleteObjects(request).HandlerError("删除对象时出错(删除目录会删除该目录下所有的文件)!");
                    }
                });
            }
            catch (Exception ex)
            {
                throw new StorageException(StorageErrorCode.PostError.ToStorageError(),
                                           new Exception(ex.ToString()));
            }
        }
예제 #4
0
        public static string DelFile(string name)
        {
            //OperationalFilesService.OperationalFiles OperationalFiles = new OperationalFilesService.OperationalFiles();
            //return OperationalFiles.DelFile("SHIANDIANPINGCOM!@#HGHGHG", name);

            OssClient client = new OssClient(endpoint, accessId, accessKey);

            try
            {
                var keys = new List <string>();
                keys.Add(name);
                //var listResult = client.ListObjects(bucketName);
                //foreach (var summary in listResult.ObjectSummaries)
                //{
                //    Console.WriteLine(summary.Key);
                //    keys.Add(summary.Key);
                //}
                var request = new DeleteObjectsRequest(bucketName, keys, false);
                client.DeleteObjects(request);
                return("1");
            }
            catch (OssException ex)
            {
                return(string.Format("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
                                     ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId));
            }
            catch (Exception ex)
            {
                return(string.Format("Failed with error info: {0}", ex.Message));
            }
        }
예제 #5
0
        private static void DeleteObjectsOfBucket(string bucketName)
        {
            var           keys       = new List <string>();
            ObjectListing result     = null;
            string        nextMarker = string.Empty;

            do
            {
                var listObjectsRequest = new ListObjectsRequest(bucketName)
                {
                    Marker  = nextMarker,
                    MaxKeys = 100
                };
                result = client.ListObjects(listObjectsRequest);

                foreach (var summary in result.ObjectSummaries)
                {
                    keys.Add(summary.Key);
                }

                nextMarker = result.NextMarker;

                if (keys.Count != 0)
                {
                    client.DeleteObjects(new DeleteObjectsRequest(bucketName, keys));
                    keys.Clear();
                }
            } while (result.IsTruncated);
        }
예제 #6
0
        public static void DeleteObjects(string bucketName)
        {
            try
            {
                var keys       = new List <string>();
                var listResult = client.ListObjects(bucketName);
                foreach (var summary in listResult.ObjectSummaries)
                {
                    keys.Add(summary.Key);
                    break;
                }
                var request = new DeleteObjectsRequest(bucketName, keys, false);
                client.DeleteObjects(request);

                Console.WriteLine("Delete objects succeeded");
            }
            catch (OssException ex)
            {
                Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
                                  ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }
예제 #7
0
        public static void DeleteObjects()
        {
            var client = new OssClient(Endpoint, AccessId, AccessKey);

            try
            {
                var keys       = new List <string>();
                var listResult = client.ListObjects(BucketName);
                foreach (var summary in listResult.ObjectSummaries)
                {
                    Console.WriteLine(summary.Key);
                    keys.Add(summary.Key);
                }
                var request = new DeleteObjectsRequest(BucketName, keys, false);
                client.DeleteObjects(request);
            }
            catch (OssException ex)
            {
                Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
                                  ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }
예제 #8
0
 /// <summary>
 /// 删除OSS上的文件
 /// </summary>
 /// <param name="filename">文件的完整文件名列表(包含路径)</param>
 /// <returns></returns>
 public static bool DeleteFiles(List <string> filename)
 {
     _logger.Info("开始删除OSS数据...");
     try
     {
         var request = new DeleteObjectsRequest(bucketName, filename.Select(x => basePath + x).ToList(), false);
         var res     = client.DeleteObjects(request);
         _logger.Info("OSS文件删除成功...");
         return(true);
     }
     catch (Exception ex)
     {
         _logger.Error(ex, ex.Message ?? "文件删除失败..." + string.Join(",", filename));
         return(false);
     }
 }
예제 #9
0
        public static void DeleteObjects()
        {
            const string accessKeyId     = "<your access key id>";
            const string accessKeySecret = "<your access key secret>";
            const string endpoint        = "<valid host name>";

            const string bucketName = "<your bucket name>";

            var client = new OssClient(endpoint, accessKeyId, accessKeySecret);

            try
            {
                var keys       = new List <string>();
                var listResult = client.ListObjects(bucketName);
                foreach (var summary in listResult.ObjectSummaries)
                {
                    Console.WriteLine(summary.Key);
                    keys.Add(summary.Key);
                }
                var request = new DeleteObjectsRequest(bucketName, keys, false);
                client.DeleteObjects(request);
            }
            catch (OssException ex)
            {
                Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
                                  ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with error info: {0}", ex.Message);
            }
        }
        public async Task <int> DeleteFilesAsync(string searchPattern = null, CancellationToken cancellation = default)
        {
            var files = await GetFileListAsync(searchPattern, cancellationToken : cancellation).AnyContext();

            var result = _client.DeleteObjects(new DeleteObjectsRequest(_bucket, files.Select(spec => spec.Path).ToList()));

            return(result.Keys?.Length ?? 0);
        }
예제 #11
0
 public void DeleteObjects(IList <string> objKeylist)
 {
     if (objKeylist?.Count > 0)
     {
         var deleteObjectsRequest = new DeleteObjectsRequest(_bucketName, objKeylist);
         var result = _ossClient.DeleteObjects(deleteObjectsRequest);
         // return result;
     }
 }
예제 #12
0
        /// <summary>
        /// 删除指定的文件
        /// </summary>
        /// <param name="bucketName">文件所在存储空间的名称</param>
        /// <param name="key">待删除的文件名称</param>
        public static bool DeleteObject(List <string> keyList)
        {
            var count = 0;
            //简单模式
            const bool quietMode = false;
            // DeleteObjectsRequest的第三个参数指定返回模式。
            var request = new DeleteObjectsRequest(bucketName, keyList, quietMode);
            // 删除多个文件。
            var result = client.DeleteObjects(request);

            if ((!quietMode) && (result.Keys != null))
            {
                count += result.Keys.Length;
            }
            return(keyList.Count == count);
        }
예제 #13
0
 /// <summary>
 /// 删除多个文件
 /// </summary>
 /// <param name="filePathArr"></param>
 public void DeleteObjs(params string[] filePathArr)
 {
     try
     {
         List <string>        filePathList         = filePathArr.ToList();
         DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(Config.Bucket, filePathList);
         client.DeleteObjects(deleteObjectsRequest);
     }
     catch (OssException e)
     {
         Debug.Log("删除多个文件出错:" + e.Message);
     }
     catch (System.Exception e)
     {
         Debug.Log("删除多个文件出错:" + e.Message);
     }
 }
 public bool DeleteBucketObjects()
 {
     try
     {
         var keys       = new List <string>();
         var listResult = client.ListObjects(bucketName);
         foreach (var summary in listResult.ObjectSummaries)
         {
             keys.Add(summary.Key);
         }
         var request = new DeleteObjectsRequest(bucketName, keys, false);
         client.DeleteObjects(request);
         return(true);
     }
     catch (OssException ex)
     {
         lastError = ex;
         return(false);
     }
 }
예제 #15
0
        protected override PurgeResult Purge()
        {
            if (RemoveThreshold == null || RemoveThreshold.Value > DateTime.Now)
            {
                return(new PurgeResult());
            }

            Log.Information($"Purge to aliyun  removeThreshold: {RemoveThreshold}");

            OssClient client        = new OssClient(endpoint, accessKeyId, accessKeySecret);
            var       objectListing = client.ListObjects(bucketName, this.PathPrefix);

            var objectsToDelete = new List <string>();

            foreach (var summary in objectListing.ObjectSummaries)
            {
                if (summary.LastModified.ToUniversalTime() <= RemoveThreshold.Value)
                {
                    objectsToDelete.Add(summary.Key);
                }
            }

            if (objectsToDelete.Count == 0)
            {
                Log.Information("Nothing to purge.");
            }
            else
            {
                objectsToDelete.ForEach(item =>
                {
                    Log.Information($"Prepare to purge: {item}");
                });

                DeleteObjectsRequest request = new DeleteObjectsRequest(bucketName, objectsToDelete);
                client.DeleteObjects(request);

                Log.Information("Aliyun oss purge done.");
            }
            return(new PurgeResult());
        }
        public async Task <bool> RemoveObjectAsync(string bucketName, List <string> objectNames)
        {
            if (string.IsNullOrEmpty(bucketName))
            {
                throw new ArgumentNullException(nameof(bucketName));
            }
            if (objectNames == null || objectNames.Count == 0)
            {
                throw new ArgumentNullException(nameof(objectNames));
            }
            var quietMode = false;
            // DeleteObjectsRequest的第三个参数指定返回模式。
            var request = new DeleteObjectsRequest(bucketName, objectNames, quietMode);
            // 删除多个文件。
            var result = _client.DeleteObjects(request);

            if ((!quietMode) && (result.Keys != null))
            {
                if (result.Keys.Count() == objectNames.Count)
                {
                    return(await Task.FromResult(true));
                }
                else
                {
                    throw new Exception("Some file delete failed.");
                }
            }
            else
            {
                if (result != null)
                {
                    return(await Task.FromResult(true));
                }
                else
                {
                    return(await Task.FromResult(true));
                }
            }
        }
예제 #17
0
        private static void DeleteFile(List <string> keys)
        {
            var client = new OssClient(Endpoint, AccessKeyId, AccessKeySecret);

            try
            {
                var quietMode = false;
                var request   = new DeleteObjectsRequest(BucketName, keys, quietMode);
                var result    = client.DeleteObjects(request);
                if ((result.Keys != null))
                {
                    foreach (var obj in result.Keys)
                    {
                        Console.WriteLine("Delete successfully : {0} ", obj.Key);
                    }
                }
                Console.WriteLine("Delete objects succeeded");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Delete objects failed. {0}", ex.Message);
            }
        }
예제 #18
0
 public void Delete(string[] fileNames)
 {
     _client.DeleteObjects(new DeleteObjectsRequest(_bucketName, fileNames));
 }
예제 #19
0
        public async Task DeleteFilesAsync(string searchPattern = null, CancellationToken cancellation = default(CancellationToken))
        {
            var files = await GetFileListAsync(searchPattern, cancellationToken : cancellation).AnyContext();

            _client.DeleteObjects(new DeleteObjectsRequest(_bucket, files.Select(spec => spec.Path).ToList()));
        }