/// <summary> /// Removes object from storage /// </summary> /// <param name="objectId">Object Id</param> /// <param name="bucketName">Bucket name - Optional if passed throuhg constructor</param> /// <returns></returns> /// <exception cref="EndpointUnreachableException">Thrown when S3 endpoint is unreachable.</exception> /// <exception cref="S3BaseException">Thrown when exception is not handled.</exception> public async Task RemoveObject(string objectId) { var s3Obj = BucketHelper.ExtractObjectInfo(objectId); var bucket = s3Obj.bucketName?.ToLower() ?? _bucketName; var objectName = s3Obj.objectName; try { await _requestRetryPolicy.ExecuteAsync(async() => { await _objectOperationsClient.RemoveObjectAsync(bucket, objectName); }); } catch (MinioException ex) when(ex is ConnectionException || ex is InternalServerException || ex is InternalClientException || ex is InvalidEndpointException ) { throw new EndpointUnreachableException(_endpoint, ex.ToString()); } catch (S3BaseException) { throw; } catch (Exception ex) { throw new S3BaseException(ex.Message, ex.ToString()); } }
/// <summary> /// Get object - With data /// </summary> /// <param name="objectId">Object Id</param> /// <param name="bucketName">Bucket name - Optional if passed throuhg constructor</param> /// <returns>Returns actual object data - bytes</returns> /// <exception cref="EndpointUnreachableException">Thrown when S3 endpoint is unreachable.</exception> /// <exception cref="ObjectNotFoundException">Thrown when object is not found.</exception> /// <exception cref="S3BaseException">Thrown when exception is not handled.</exception> public async Task <S3Object> GetObject(string objectId) { var s3Obj = BucketHelper.ExtractObjectInfo(objectId); var bucket = s3Obj.bucketName?.ToLower() ?? _bucketName; var objectName = s3Obj.objectName; var result = await GetObjectInfo(objectId); try { result.Data = await _requestRetryPolicy.ExecuteAsync(async() => { byte[] data = null; await _objectOperationsClient.GetObjectAsync(bucket, objectName, (s) => { using (var ms = new MemoryStream()) { byte[] buffer = new byte[result.Size]; int read; while ((read = s.Read(buffer, 0, buffer.Length)) > 0) { ms.Write(buffer, 0, read); } ms.Seek(0, SeekOrigin.Begin); data = ms.ToArray(); } }); return(data); }); } catch (MinioException ex) when(ex is ConnectionException || ex is InternalServerException || ex is InternalClientException || ex is InvalidEndpointException ) { throw new EndpointUnreachableException(_endpoint, ex.ToString()); } catch (S3BaseException) { throw; } catch (Exception ex) { throw new S3BaseException(ex.Message, ex.ToString()); } return(result); }
/// <summary> /// Gets object information - Without file data /// </summary> /// <param name="objectId">Object Id</param> /// <param name="bucketName">Bucket name - Optional if passed throuhg constructor</param> /// <returns>Returns object information and metadata</returns> /// <exception cref="EndpointUnreachableException">Thrown when S3 endpoint is unreachable.</exception> /// <exception cref="ObjectNotFoundException">Thrown when object is not found.</exception> /// <exception cref="S3BaseException">Thrown when exception is not handled.</exception> public async Task <S3Object> GetObjectInfo(string objectId) { var s3Obj = BucketHelper.ExtractObjectInfo(objectId); var bucket = s3Obj.bucketName?.ToLower() ?? _bucketName; var objectName = s3Obj.objectName; S3Object result = null; try { result = await _requestRetryPolicy.ExecuteAsync(async() => { var response = await _objectOperationsClient.StatObjectAsync(bucket, objectName); if (response == null) { throw new Exceptions.ObjectNotFoundException(objectName, bucket); } return(new S3Object(response.ObjectName, bucket, response.Size, response.ETag, response.ContentType, response.MetaData, null)); }); } catch (MinioException ex) when(ex is ConnectionException || ex is InternalServerException || ex is InternalClientException || ex is InvalidEndpointException ) { throw new EndpointUnreachableException(_endpoint, ex.ToString()); } catch (S3BaseException) { throw; } catch (Exception ex) { throw new S3BaseException(ex.Message, ex.ToString()); } return(result); }
/// <summary> /// Gets object URL for downloading from storage (Temporary URL support varies by implementation) /// </summary> /// <param name="objectId">Object Id</param> /// <param name="expiresInSeconds">Temporary link expiration time in seconds. Defaults to 12 hours</param> /// <param name="bucketName">Bucket name - Optional if passed throuhg constructor</param> /// <returns>Returns temporary URL of object for download</returns> /// <exception cref="EndpointUnreachableException">Thrown when S3 endpoint is unreachable.</exception> /// <exception cref="ObjectNotFoundException">Thrown when object is not found.</exception> /// <exception cref="S3BaseException">Thrown when exception is not handled.</exception> public async Task <string> GetObjectURL(string objectId, int expiresInSeconds = 600) { var s3Obj = BucketHelper.ExtractObjectInfo(objectId); var bucket = s3Obj.bucketName?.ToLower() ?? _bucketName; var objectName = s3Obj.objectName; string result = null; try { result = await _requestRetryPolicy.ExecuteAsync(async() => { return(await _objectOperationsClient.PresignedGetObjectAsync(bucket, objectName, expiresInSeconds)); }); if (result == null) { throw new Exceptions.ObjectNotFoundException(objectId, bucket); } } catch (MinioException ex) when(ex is ConnectionException || ex is InternalServerException || ex is InternalClientException || ex is InvalidEndpointException ) { throw new EndpointUnreachableException(_endpoint, ex.ToString()); } catch (S3BaseException) { throw; } catch (Exception ex) { throw new S3BaseException(ex.Message, ex.ToString()); } return(result); }