public void RemoveFile(string filename, string versionId = null) { var listResponse = _s3Client.ListVersions(new ListVersionsRequest { BucketName = AwsConstants.VERSIONING_BUCKET_NAME, Prefix = filename }); if (string.IsNullOrEmpty(versionId)) { var deleteMarkerVersion = listResponse.Versions.FirstOrDefault(x => x.IsDeleteMarker && x.IsLatest); if (deleteMarkerVersion != null) { versionId = deleteMarkerVersion.VersionId; } } if (!string.IsNullOrEmpty(versionId)) { _s3Client.DeleteObject(new DeleteObjectRequest { BucketName = AwsConstants.VERSIONING_BUCKET_NAME, Key = filename, VersionId = versionId }); } }
public void DeleteDir(string localDirName) { var key = getS3DirKey(localDirName); _s3Client.DeleteObject(new DeleteObjectRequest { BucketName = _bucket, Key = key }); }
public bool DeleteAObject(string bucketname, string keyname) { try { // Create a DeleteObject request DeleteObjectRequest request = new DeleteObjectRequest { BucketName = bucketname, Key = keyname }; // Issue request client.DeleteObject(request); return(true); } catch (AmazonS3Exception amazonS3Exception) { string filename = ConfigurationManager.AppSettings["LogFile"]; using (StreamWriter writer = File.AppendText(filename)) { if (amazonS3Exception.ErrorCode != null && (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity"))) { log.WriteLog("Please check the provided AWS Credentials.", writer); log.WriteLog("If you haven't signed up for Amazon S3, please visit http://aws.amazon.com/s3", writer); return(false); } else { log.WriteLog("An Error, number " + amazonS3Exception.ErrorCode + ", occurred when deleting a object with the message " + amazonS3Exception.Message, writer); return(false); } } } }
/// <summary> /// Moves an image (well, any object, really) from one key to another /// </summary> /// <param name="szSrc">Source path (key)</param> /// <param name="szDst">Destination path (key)</param> public static void MoveImageOnS3(string szSrc, string szDst) { try { using (IAmazonS3 s3 = AWSConfiguration.S3Client()) { CopyObjectRequest cor = new CopyObjectRequest(); DeleteObjectRequest dor = new DeleteObjectRequest(); cor.SourceBucket = cor.DestinationBucket = dor.BucketName = AWSConfiguration.CurrentS3Bucket; cor.DestinationKey = szDst; cor.SourceKey = dor.Key = szSrc; cor.CannedACL = S3CannedACL.PublicRead; cor.StorageClass = S3StorageClass.Standard; // vs. reduced s3.CopyObject(cor); s3.DeleteObject(dor); } } catch (AmazonS3Exception ex) { util.NotifyAdminEvent("Error moving image on S3", String.Format(CultureInfo.InvariantCulture, "Error moving from key\r\n{0}to\r\n{1}\r\n\r\n{2}", szSrc, szDst, WrapAmazonS3Exception(ex)), ProfileRoles.maskSiteAdminOnly); throw new MyFlightbookException(String.Format(CultureInfo.InvariantCulture, "Error moving file on S3: Request address:{0}, Message:{1}", WrapAmazonS3Exception(ex), ex.Message)); } catch (Exception ex) { throw new MyFlightbookException("Unknown error moving image on S3: " + ex.Message); } }
private Amazon.S3.Model.DeleteObjectResponse CallAWSServiceOperation(IAmazonS3 client, Amazon.S3.Model.DeleteObjectRequest request) { Utils.Common.WriteVerboseEndpointMessage(this, client.Config, "Amazon S3", "DeleteObject"); try { #if DESKTOP return(client.DeleteObject(request)); #elif CORECLR return(client.DeleteObjectAsync(request).GetAwaiter().GetResult()); #else #error "Unknown build edition" #endif } catch (AmazonServiceException exc) { var webException = exc.InnerException as System.Net.WebException; if (webException != null) { throw new Exception(Utils.Common.FormatNameResolutionFailureMessage(client.Config, webException.Message), webException); } throw; } }
///<sumary> /// Deletes current file and uplods a new /// /// @param bucketName /// @param objectKey /// @param contentType /// @param fileToReplace /// @return ///</sumary> public static bool ReplaceS3Object(string bucketName, string objectKey, string contentType, byte[] fileToReplace) { bool result = true; if (fileToReplace != null) { IAmazonS3 s3Client = S3Helper.S3Client; var current = GetS3ObjectStream(bucketName, objectKey); if (current != null) { DeleteObjectRequest delReq = new DeleteObjectRequest() { Key = objectKey, BucketName = bucketName }; s3Client.DeleteObject(bucketName, objectKey); } using (MemoryStream ms = new MemoryStream(fileToReplace)) { PutObjectToS3(bucketName, objectKey, contentType, ms); } } else { result = false; } return(result); }
public static ActionResult Delete(IAmazonS3 S3Manager, string bucketName, string fileName) { ActionResult ar = new ActionResult() { IsSuccess = false, Msg = "Empty" }; if (S3Manager != null) { ListObjectsRequest request = new ListObjectsRequest(); request.BucketName = bucketName; request.Prefix = fileName; ListObjectsResponse response = S3Manager.ListObjects(request); if (response.S3Objects.Count == 1) { S3Manager.DeleteObject(bucketName, fileName); ar.IsSuccess = true; ar.Msg = "删除成功!"; } else { ar.IsSuccess = false; ar.Msg = "文件不存在!"; } } return(ar); }
static void DeletingAnObject() { try { DeleteObjectRequest request = new DeleteObjectRequest() { BucketName = bucketName, Key = keyName }; client.DeleteObject(request); } catch (AmazonS3Exception amazonS3Exception) { if (amazonS3Exception.ErrorCode != null && (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity"))) { Console.WriteLine("Please check the provided AWS Credentials."); Console.WriteLine("If you haven't signed up for Amazon S3, please visit http://aws.amazon.com/s3"); } else { Console.WriteLine("An error occurred with the message '{0}' when deleting an object", amazonS3Exception.Message); } } }
public static void DeleteObject(string fileKey) { using (_client = new AmazonS3Client(AwsAccessKeyId, AwsSecretAccessKey, EndPoint)) { try { var deleteObjectRequest = new DeleteObjectRequest() { BucketName = BucketName, Key = fileKey }; var deleteObjectResponse = _client.DeleteObject(deleteObjectRequest); } catch (AmazonS3Exception amazonS3Exception) { if (amazonS3Exception.ErrorCode != null && (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity"))) { Console.WriteLine("Check the provided AWS Credentials."); Console.WriteLine( "For service sign up go to http://aws.amazon.com/s3"); } else { Console.WriteLine( "Error occurred. Message:'{0}' when writing an object" , amazonS3Exception.Message); } } } }
/// <summary> /// /// </summary> /// <param name="bucketName"></param> /// <param name="fileKey"></param> /// <returns></returns> public string DeleteFileS3(string bucketName, string fileKey) { string result = ""; try { AmazonS3Config config = new AmazonS3Config { ServiceURL = endPoint, ForcePathStyle = true }; IAmazonS3 client = null; client = Amazon.AWSClientFactory.CreateAmazonS3Client(AWSAccessKey, AWSSecretKey, config); if (Exists(client, fileKey, bucketName)) { client.DeleteObject(new Amazon.S3.Model.DeleteObjectRequest() { BucketName = bucketName, Key = fileKey }); result = "delete success " + bucketName + "/" + fileKey; } else { result = "file not exists " + bucketName + "/" + fileKey; } } catch (Exception ex) { result = "error!"; } return(result); }
private bool MoveToArchive(string fileName, string archiveLocation, string bucketName) { CopyObjectRequest copyRequest = new CopyObjectRequest() { SourceBucket = bucketName, SourceKey = fileName, DestinationBucket = bucketName, DestinationKey = archiveLocation + fileName }; CopyObjectResponse copyResponse = _client.CopyObject(copyRequest); if (copyResponse.HttpStatusCode == System.Net.HttpStatusCode.OK) { DeleteObjectRequest deleteRequest = new DeleteObjectRequest() { BucketName = bucketName, Key = fileName }; DeleteObjectResponse deleteResponse = _client.DeleteObject(deleteRequest); if (deleteResponse.HttpStatusCode == System.Net.HttpStatusCode.NoContent) { return(true); } } return(false); }
/// <summary> /// ADMIN ONLY - Removes images from the debug bucket /// </summary> public static void ADMINCleanUpDebug() { using (IAmazonS3 s3 = AWSConfiguration.S3Client()) { ListObjectsRequest request = new ListObjectsRequest() { BucketName = AWSConfiguration.S3BucketNameDebug }; do { ListObjectsResponse response = s3.ListObjects(request); foreach (S3Object o in response.S3Objects) { DeleteObjectRequest dor = new DeleteObjectRequest() { BucketName = AWSConfiguration.S3BucketNameDebug, Key = o.Key }; s3.DeleteObject(dor); } // If response is truncated, set the marker to get the next // set of keys. if (response.IsTruncated) { request.Marker = response.NextMarker; } else { request = null; } } while (request != null); } }
private void DeleteFolder(string path, IAmazonS3 client) { EnsureInitialized(); if (_client == null) { return; } //TODO: Refractor to use async deletion? foreach (var folder in ListFolders(path)) { DeleteFolder(folder.GetPath(), client); } foreach (var file in ListFiles(path)) { DeleteFile(file.GetPath(), client); } var request = new DeleteObjectRequest() { BucketName = BucketName, Key = path }; DeleteObjectResponse response = client.DeleteObject(request); }
public static void DeletingAnObject(string bucketName, string keyName) { try { using (IAmazonS3 s3Client = GetAmazonS3ClientInstance()) { DeleteObjectRequest request = new DeleteObjectRequest { BucketName = bucketName, Key = keyName }; s3Client.DeleteObject(request); } } catch (AmazonS3Exception amazonS3Exception) { if (amazonS3Exception.ErrorCode != null && (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity"))) { throw new Exception("Please check the provided AWS Credentials."); } throw new Exception(String.Format("An error occurred with the message '{0}' when deleting an object", amazonS3Exception.Message)); } }
public void delete() { using (client = new AmazonS3Client(_accessKey, _secretKey, Amazon.RegionEndpoint.USEast1)) { try { // Make the bucket version-enabled. EnableVersioningOnBucket(bucketName); // Add a sample object. string versionID = PutAnObject(keyName); // Delete the object by specifying an object key and a version ID. DeleteObjectRequest request = new DeleteObjectRequest { BucketName = bucketName, Key = keyName, VersionId = versionID }; Console.WriteLine("Deleting an object"); client.DeleteObject(request); } catch (AmazonS3Exception s3Exception) { Console.WriteLine(s3Exception.Message, s3Exception.InnerException); } } Console.WriteLine("Press any key to continue..."); Console.ReadKey(); }
/// <summary> /// Deletes folder together with its content /// </summary> public void DeleteFolder(string folderName) { if (!FolderExists(folderName)) { return; } DeleteFolderContent(folderName); var deleteObjectRequest = new DeleteObjectRequest() { BucketName = _bucketName, Key = GetFolderPrefix(folderName) }; _client.DeleteObject(deleteObjectRequest); }
/// <summary> /// Deletes the image from S3. The actual operation happens asynchronously; the result is not captured. /// </summary> /// <param name="mfbii">The image to delete</param> public static void DeleteImageOnS3(MFBImageInfo mfbii) { if (mfbii == null) { return; } try { DeleteObjectRequest dor = new DeleteObjectRequest() { BucketName = AWSConfiguration.CurrentS3Bucket, Key = mfbii.S3Key }; new Thread(new ThreadStart(() => { try { using (IAmazonS3 s3 = AWSConfiguration.S3Client()) { s3.DeleteObject(dor); if (mfbii.ImageType == MFBImageInfo.ImageFileType.S3VideoMP4) { // Delete the thumbnail too. string szs3Key = mfbii.S3Key; dor.Key = szs3Key.Replace(Path.GetFileName(szs3Key), MFBImageInfo.ThumbnailPrefixVideo + Path.GetFileNameWithoutExtension(szs3Key) + "00001" + FileExtensions.JPG); s3.DeleteObject(dor); } } } catch (Exception ex) when(ex is AmazonS3Exception) { } } )).Start(); } catch (AmazonS3Exception ex) { throw new MyFlightbookException(String.Format(CultureInfo.InvariantCulture, "Error deleting file on S3: {0}", WrapAmazonS3Exception(ex)), ex.InnerException); } catch (Exception ex) { throw new MyFlightbookException("Unknown error deleting image on S3: " + ex.Message); } }
public static void DeleteFile(IAmazonS3 client, string bucketName, string fileName) { client.DeleteObject(new DeleteObjectRequest { BucketName = bucketName, Key = fileName }); }
private void DeleteFile(string path, IAmazonS3 client) { var request = new DeleteObjectRequest() { BucketName = BucketName, Key = path }; DeleteObjectResponse response = client.DeleteObject(request); }
public void remove() { DeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest() { BucketName = BucketName, Key = Filename }; client.DeleteObject(deleteObjectRequest); }
public void DeleteFile(string relativeFileName) { string rootKey = string.Format("{0}/{1}", _workingDir, relativeFileName); List <string> allObjects = _client.ListObjects(new ListObjectsRequest { BucketName = _config.RootBucketName, Prefix = rootKey }).CommonPrefixes; allObjects.AsParallel().ForAll(x => _client.DeleteObject(_config.RootBucketName, x)); }
public string DeleteObjectFromAmazon(string fileId) { var request = new DeleteObjectRequest(); request.BucketName = bucket; request.Key = fileId; var response = _client.DeleteObject(request); return("Removed"); }
public override bool Delete(NonSecureFileModel fileModel) { DeleteObjectRequest request = new DeleteObjectRequest() { BucketName = bucketName, Key = fileModel.FileFullPath + fileModel.FileName }; s3Client.DeleteObject(request); return(true); }
//delete related private bool DeleteObject(int id) { Log.Debug("Entered delete object"); //delete from s3 using (TuneWorldDBEntities entities = new TuneWorldDBEntities()) { var entity = entities.Objects.Where(e => e.ObjectId.Equals(id)); if (entity.Count() == 1) { using (client = new AmazonS3Client(awskey, awssecret, new AmazonS3Config { RegionEndpoint = regionep, UseAccelerateEndpoint = true })) { try { // Delete the object DeleteObjectRequest request = new DeleteObjectRequest { BucketName = bucketName, Key = entity.First().ObjectUploadLink, //VersionId = versionID }; Console.WriteLine("Deleting an object"); DeleteObjectResponse resp = client.DeleteObject(request); } catch (AmazonS3Exception s3Exception) { Log.Debug("Delete S3 Exception exception " + s3Exception.Message + s3Exception.StackTrace); Console.WriteLine(s3Exception.Message, s3Exception.InnerException); return(false); } //delete from rds try { Log.Debug("Delete S3 success updating RDS "); ObjectModel obj = new ObjectModel(id); string cmdstr = obj.ObjectDeleteDB("Objects", id); DBAccess.DBAccess.InsertUpdateDeleteIntoDB(cmdstr); } catch (Exception e) { Log.Debug("Delete updating RDS excepton" + e.Message + e.StackTrace); return(false); } return(true); } } return(false); } }
/// <summary> /// Deletes a file /// </summary> /// <param name="path">Web path to file's folder</param> /// <param name="fileName">File name</param> public void DeleteFile(string path, string fileName) { // Prepare delete request var request = new DeleteObjectRequest(); request.BucketName = (_bucketName); request.Key = (GetKey(path, fileName)); // Delete file var response = _client.DeleteObject(request); //response.Dispose(); }
private static void DeleteS3Object(string bucketName, string bucketObject) { Console.WriteLine("Delete object " + bucketObject); DeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest { BucketName = bucketName, Key = bucketObject }; s3Client.DeleteObject(deleteObjectRequest); Console.WriteLine("Object deleted"); }
public override void Delete(string container, string fileName) { client = AWSClientFactory.CreateAmazonS3Client(this.ExtendedProperties["accessKey"], this.ExtendedProperties["secretKey"], RegionEndpoint.USEast1); String S3_KEY = fileName; DeleteObjectRequest request = new DeleteObjectRequest() { BucketName = container, Key = S3_KEY, }; DeleteObjectResponse response = client.DeleteObject(request); }
public Exception Delete(string token) { Exception ret = null; VerifyConfiguration(); try { DeleteObjectRequest request = new DeleteObjectRequest() { BucketName = Configuration.BucketName, Key = token }; AmazonClient.DeleteObject(request); } catch (Exception ex) { ret = ex; } return(ret); }
/// <summary> /// Deletes a file /// </summary> /// <param name="path">Web path to file's folder</param> /// <param name="fileName">File name</param> public void DeleteFile(string path, string fileName) { // Prepare delete request var request = new DeleteObjectRequest() { BucketName = _bucketName, Key = GetKey(path, fileName) }; // Delete file var response = _client.DeleteObject(request); response.DisposeIfDisposable(); }
/// <summary> /// Delete image from particular folder with specific image name /// </summary> /// <param name="mainFolder"></param> /// <param name="keyName"></param> /// <returns></returns> public static bool DeleteFile(string mainFolder, string keyName) { using (_client = new AmazonS3Client(AwsAccessKeyId, AwsSecretAccessKey, EndPoint)) { const string delimiter = "/"; var deleteFolderRequest = new DeleteObjectRequest { BucketName = BucketName, Key = string.Concat(mainFolder, delimiter, keyName) }; var deleteObjectResponse = _client.DeleteObject(deleteFolderRequest); return(deleteObjectResponse.HttpStatusCode == System.Net.HttpStatusCode.NoContent); } }
/// <summary> /// Deletes the from S3. /// </summary> /// <exception cref="T:System.Net.WebException"></exception> /// <exception cref="T:Amazon.S3.AmazonS3Exception"></exception> public void Delete() { if (Exists) { var deleteObjectRequest = new DeleteObjectRequest { BucketName = bucket, Key = S3Helper.EncodeKey(key) }; ((Amazon.Runtime.Internal.IAmazonWebServiceRequest)deleteObjectRequest).AddBeforeRequestHandler(S3Helper.FileIORequestEventHandler); s3Client.DeleteObject(deleteObjectRequest); Directory.Create(); } }
public static DeleteObjectResponse DeleteFile(IAmazonS3 s3Client, string s3path, string BucketName = "ulp-learning") { DeleteObjectRequest request = new DeleteObjectRequest() { BucketName=BucketName,Key=s3path}; var result=s3Client.DeleteObject(request); return result; }
private void DeleteFolder(string path, IAmazonS3 client) { EnsureInitialized(); if (_client == null) return; //TODO: Refractor to use async deletion? foreach (var folder in ListFolders(path)) { DeleteFolder(folder.GetPath(), client); } foreach (var file in ListFiles(path)) { DeleteFile(file.GetPath(), client); } var request = new DeleteObjectRequest() { BucketName = BucketName, Key = path }; DeleteObjectResponse response = client.DeleteObject(request); }