/// <summary> /// /// <para>SetFileAccessibility:</para> /// /// <para>Changes accessibility of a file in the File Service</para> /// /// <para>Check <seealso cref="IBFileServiceInterface.SetFileAccessibility"/> for detailed documentation</para> /// /// </summary> public bool SetFileAccessibility( string _BucketName, string _KeyInBucket, EBRemoteFileReadPublicity _RemoteFileReadAccess = EBRemoteFileReadPublicity.AuthenticatedRead, Action <string> _ErrorMessageAction = null) { if (GSClient == null) { _ErrorMessageAction?.Invoke("BFileServiceGC->SetFileAccessibility: GSClient is null."); return(false); } PredefinedObjectAcl CloudTypePublicity; if (_RemoteFileReadAccess == EBRemoteFileReadPublicity.PublicRead) { CloudTypePublicity = PredefinedObjectAcl.PublicRead; } else if (_RemoteFileReadAccess == EBRemoteFileReadPublicity.ProjectWideProtectedRead) { CloudTypePublicity = PredefinedObjectAcl.ProjectPrivate; } else { CloudTypePublicity = PredefinedObjectAcl.AuthenticatedRead; } try { var ExistingObject = GSClient.GetObject(_BucketName, _KeyInBucket); if (ExistingObject == null) { _ErrorMessageAction?.Invoke("BFileServiceGC->SetFileAccessibility: Response of GetObject for existing object is null."); return(false); } ExistingObject.CacheControl = CloudTypePublicity == PredefinedObjectAcl.PublicRead ? "public" : "private"; var UpdatedObject = GSClient.UpdateObject(ExistingObject, new UpdateObjectOptions { PredefinedAcl = CloudTypePublicity }); if (UpdatedObject == null) { _ErrorMessageAction?.Invoke("BFileServiceGC->SetFileAccessibility: Response of GetObject for updated object is null."); return(false); } } catch (Exception e) { _ErrorMessageAction?.Invoke("BFileServiceGC->SetFileAccessibility: " + e.Message + ", Trace: " + e.StackTrace); return(false); } return(true); }
/// <summary> /// /// <para>CopyFile:</para> /// /// <para>Copy a file from a bucket and relative location to another in File Service, caller thread will be blocked before it is done</para> /// /// <para>Check <seealso cref="IBFileServiceInterface.CopyFile"/> for detailed documentation</para> /// /// </summary> public bool CopyFile( string _SourceBucketName, string _SourceKeyInBucket, string _DestinationBucketName, string _DestinationKeyInBucket, EBRemoteFileReadPublicity _RemoteFileReadAccess = EBRemoteFileReadPublicity.AuthenticatedRead, Action <string> _ErrorMessageAction = null) { if (GSClient == null) { _ErrorMessageAction?.Invoke("BFileServiceGC->CopyFile: GSClient is null."); return(false); } PredefinedObjectAcl CloudTypePublicity; if (_RemoteFileReadAccess == EBRemoteFileReadPublicity.PublicRead) { CloudTypePublicity = PredefinedObjectAcl.PublicRead; } else if (_RemoteFileReadAccess == EBRemoteFileReadPublicity.ProjectWideProtectedRead) { CloudTypePublicity = PredefinedObjectAcl.ProjectPrivate; } else { CloudTypePublicity = PredefinedObjectAcl.AuthenticatedRead; } try { var CopiedObject = GSClient.CopyObject(_SourceBucketName, _SourceKeyInBucket, _DestinationBucketName, _DestinationKeyInBucket, new CopyObjectOptions { DestinationPredefinedAcl = CloudTypePublicity }); if (CopiedObject == null) { _ErrorMessageAction?.Invoke("BFileServiceGC->CopyFile: Operation has failed."); return(false); } CopiedObject.CacheControl = CloudTypePublicity == PredefinedObjectAcl.PublicRead ? "public" : "private"; GSClient.PatchObject(CopiedObject, null); } catch (Exception e) { _ErrorMessageAction?.Invoke("BFileServiceGC->CopyFile: " + e.Message + ", Trace: " + e.StackTrace); return(false); } return(true); }
/// <summary> /// /// <para>CopyFile:</para> /// /// <para>Copy a file from a bucket and relative location to another in File Service, caller thread will be blocked before it is done</para> /// /// <para>Check <seealso cref="IBFileServiceInterface.CopyFile"/> for detailed documentation</para> /// /// </summary> public bool CopyFile( string _SourceBucketName, string _SourceKeyInBucket, string _DestinationBucketName, string _DestinationKeyInBucket, EBRemoteFileReadPublicity _RemoteFileReadAccess = EBRemoteFileReadPublicity.AuthenticatedRead, Action <string> _ErrorMessageAction = null) { if (S3Client == null) { _ErrorMessageAction?.Invoke("BFileServiceAWS->CopyFile: S3Client is null."); return(false); } CopyObjectRequest Request = new CopyObjectRequest { SourceBucket = _SourceBucketName, SourceKey = _SourceKeyInBucket, DestinationBucket = _DestinationBucketName, DestinationKey = _DestinationKeyInBucket }; if (_RemoteFileReadAccess == EBRemoteFileReadPublicity.PublicRead) { Request.CannedACL = S3CannedACL.PublicRead; } else if (_RemoteFileReadAccess == EBRemoteFileReadPublicity.ProjectWideProtectedRead) { Request.CannedACL = S3CannedACL.AuthenticatedRead; } else { Request.CannedACL = S3CannedACL.AuthenticatedRead; } try { using (var CreatedTask = S3Client.CopyObjectAsync(Request)) { CreatedTask.Wait(); } } catch (Exception e) { _ErrorMessageAction?.Invoke("BFileServiceAWS->CopyFile: " + e.Message + ", Trace: " + e.StackTrace); return(false); } return(true); }
/// <summary> /// /// <para>SetFileAccessibility:</para> /// /// <para>Changes accessibility of a file in the File Service</para> /// /// <para>Check <seealso cref="IBFileServiceInterface.SetFileAccessibility"/> for detailed documentation</para> /// /// </summary> public bool SetFileAccessibility( string _BucketName, string _KeyInBucket, EBRemoteFileReadPublicity _RemoteFileReadAccess = EBRemoteFileReadPublicity.AuthenticatedRead, Action <string> _ErrorMessageAction = null) { if (S3Client == null) { _ErrorMessageAction?.Invoke("BFileServiceAWS->SetFileAccessibility: S3Client is null."); return(false); } PutACLRequest Request = new PutACLRequest { BucketName = _BucketName, Key = _KeyInBucket }; if (_RemoteFileReadAccess == EBRemoteFileReadPublicity.PublicRead) { Request.CannedACL = S3CannedACL.PublicRead; } else if (_RemoteFileReadAccess == EBRemoteFileReadPublicity.ProjectWideProtectedRead) { Request.CannedACL = S3CannedACL.AuthenticatedRead; } else { Request.CannedACL = S3CannedACL.AuthenticatedRead; } try { using (var CreatedTask = S3Client.PutACLAsync(Request)) { CreatedTask.Wait(); } } catch (Exception e) { _ErrorMessageAction?.Invoke("BFileServiceAWS->SetFileAccessibility: " + e.Message + ", Trace: " + e.StackTrace); return(false); } return(true); }
/// <summary> /// Azure blob storage does not support per object permissions. /// Calling this throws NotSupportedException /// </summary> public bool SetFileAccessibility(string _BucketName, string _KeyInBucket, EBRemoteFileReadPublicity _RemoteFileReadAccess = EBRemoteFileReadPublicity.AuthenticatedRead, Action <string> _ErrorMessageAction = null) { // Azure blob storage does not support per object permissions. throw new NotSupportedException(); }
/// <summary> /// /// <para>UploadFile:</para> /// /// <para>Uploads a local file to File Service, caller thread will be blocked before it is done</para> /// /// <para>EBRemoteFileReadPublicity does nothing as Azure does not support per object authentication, only per container</para> /// /// <para>Check <seealso cref="IBFileServiceInterface.UploadFile"/> for detailed documentation</para> /// /// </summary> public bool UploadFile(BStringOrStream _LocalFileOrStream, string _BucketName, string _KeyInBucket, EBRemoteFileReadPublicity _RemoteFileReadAccess = EBRemoteFileReadPublicity.AuthenticatedRead, Tuple <string, string>[] _FileTags = null, Action <string> _ErrorMessageAction = null) { if (AServiceClient == null) { _ErrorMessageAction?.Invoke("BFileServiceAZ->UploadFile: GSClient is null."); return(false); } BlobContainerClient ContainerClient = AServiceClient.GetBlobContainerClient(_BucketName); BlobClient Blob = ContainerClient.GetBlobClient(_KeyInBucket); Response <BlobContentInfo> Response = null; if (_LocalFileOrStream.Type == EBStringOrStreamEnum.String) { if (!BUtility.DoesFileExist( _LocalFileOrStream.String, out bool bLocalFileExists, _ErrorMessageAction)) { _ErrorMessageAction?.Invoke("BFileServiceAZ->UploadFile: DoesFileExist failed."); return(false); } if (!bLocalFileExists) { _ErrorMessageAction?.Invoke("BFileServiceAZ->UploadFile: Local file does not exist."); return(false); } using (FileStream FS = new FileStream(_LocalFileOrStream.String, FileMode.Open, FileAccess.Read)) { try { Response = Blob.Upload(FS); if (Response.Value == null) { _ErrorMessageAction?.Invoke("BFileServiceAZ->UploadFile: Operation has failed."); return(false); } } catch (Exception e) { _ErrorMessageAction?.Invoke("BFileServiceAZ->UploadFile: " + e.Message + ", Trace: " + e.StackTrace); return(false); } } } else { try { Response = Blob.Upload(_LocalFileOrStream.Stream); if (Response.Value == null) { _ErrorMessageAction?.Invoke("BFileServiceAZ->UploadFile: Operation has failed."); return(false); } } catch (Exception e) { _ErrorMessageAction?.Invoke("BFileServiceAZ->UploadFile: " + e.Message + ", Trace: " + e.StackTrace); return(false); } } if (Response != null) { if (_FileTags != null && _FileTags.Length > 0) { Dictionary <string, string> NewMetadata = new Dictionary <string, string>(); foreach (Tuple <string, string> CurrentTag in _FileTags) { NewMetadata.Add(CurrentTag.Item1, CurrentTag.Item2); } try { Blob.SetMetadata(NewMetadata); } catch (Exception ex) { _ErrorMessageAction?.Invoke($"BFileServiceAZ->UploadFile: {ex.Message}, Trace: {ex.StackTrace}"); return(false); } } var FileName = _KeyInBucket; var FileNameIndex = _KeyInBucket.LastIndexOf("/") + 1; if (FileNameIndex > 0) { FileName = _KeyInBucket.Substring(FileNameIndex, _KeyInBucket.Length - FileNameIndex); } BlobHttpHeaders Header = new BlobHttpHeaders() { ContentDisposition = $"inline; filename={FileName}" }; try { Blob.SetHttpHeaders(Header); } catch (Exception ex) { _ErrorMessageAction?.Invoke($"BFileServiceAZ->UploadFile: {ex.Message}, Trace: {ex.StackTrace}"); return(false); } } return(true); }
/// <summary> /// /// <para>CopyFile:</para> /// /// <para>Copy a file from a bucket and relative location to another in File Service, caller thread will be blocked before it is done</para> /// /// <para>EBRemoteFileReadPublicity does nothing as Azure does not support per object authentication, only per container</para> /// /// <para>Check <seealso cref="IBFileServiceInterface.CopyFile"/> for detailed documentation</para> /// /// </summary> public bool CopyFile(string _SourceBucketName, string _SourceKeyInBucket, string _DestinationBucketName, string _DestinationKeyInBucket, EBRemoteFileReadPublicity _RemoteFileReadAccess = EBRemoteFileReadPublicity.AuthenticatedRead, Action <string> _ErrorMessageAction = null) { try { BlobContainerClient SourceClient = AServiceClient.GetBlobContainerClient(_SourceBucketName); BlobClient SourceBlob = SourceClient.GetBlobClient(_SourceKeyInBucket); //Will throw exception on failure Response <bool> ExistsResponse = SourceBlob.Exists(); if (ExistsResponse.Value) { BlobContainerClient DestClient = AServiceClient.GetBlobContainerClient(_DestinationBucketName); BlobClient DestBlob = SourceClient.GetBlobClient(_DestinationKeyInBucket); CopyFromUriOperation CopyOp = DestBlob.StartCopyFromUri(SourceBlob.Uri, new BlobCopyFromUriOptions()); CopyOp.WaitForCompletionAsync(); CopyOp.UpdateStatus(); return(CopyOp.HasCompleted); } else { _ErrorMessageAction?.Invoke($"BFileServiceAZ -> CopyFile : Source file does not exist"); return(false); } } catch (Exception ex) { _ErrorMessageAction?.Invoke($"BFileServiceAZ -> CopyFile : {ex.Message}\n{ex.StackTrace}"); return(false); } }
/// <summary> /// /// <para>UploadFile:</para> /// /// <para>Uploads a local file to File Service, caller thread will be blocked before it is done</para> /// /// <para>Check <seealso cref="IBFileServiceInterface.UploadFile"/> for detailed documentation</para> /// /// </summary> public bool UploadFile( BStringOrStream _LocalFileOrStream, string _BucketName, string _KeyInBucket, EBRemoteFileReadPublicity _RemoteFileReadAccess = EBRemoteFileReadPublicity.AuthenticatedRead, Tuple <string, string>[] _FileTags = null, Action <string> _ErrorMessageAction = null) { if (TransferUtil == null) { _ErrorMessageAction?.Invoke("BFileServiceAWS->UploadFile: TransferUtil is null."); return(false); } TransferUtilityUploadRequest UploadRequest = new TransferUtilityUploadRequest { BucketName = _BucketName, PartSize = 16, Key = _KeyInBucket }; if (_LocalFileOrStream.Type == EBStringOrStreamEnum.String) { if (!BUtility.DoesFileExist( _LocalFileOrStream.String, out bool bLocalFileExists, _ErrorMessageAction)) { _ErrorMessageAction?.Invoke("BFileServiceAWS->UploadFile: DoesFileExist failed."); return(false); } if (!bLocalFileExists) { _ErrorMessageAction?.Invoke("BFileServiceAWS->UploadFile: Local file does not exist."); return(false); } UploadRequest.FilePath = _LocalFileOrStream.String; } else { UploadRequest.InputStream = _LocalFileOrStream.Stream; } if (_RemoteFileReadAccess == EBRemoteFileReadPublicity.PublicRead) { UploadRequest.CannedACL = S3CannedACL.PublicRead; } else if (_RemoteFileReadAccess == EBRemoteFileReadPublicity.ProjectWideProtectedRead) { UploadRequest.CannedACL = S3CannedACL.AuthenticatedRead; } else { UploadRequest.CannedACL = S3CannedACL.AuthenticatedRead; } if (_FileTags != null && _FileTags.Length > 0) { List <Tag> FileTags = new List <Tag>(); foreach (Tuple <string, string> CurrentTag in _FileTags) { Tag NewTag = new Tag { Key = CurrentTag.Item1, Value = CurrentTag.Item2 }; FileTags.Add(NewTag); } UploadRequest.TagSet = FileTags; } try { TransferUtil.Upload(UploadRequest); } catch (Exception e) { _ErrorMessageAction?.Invoke("BFileServiceAWS->UploadFile: " + e.Message + ", Trace: " + e.StackTrace); return(false); } return(true); }
/// <summary> /// /// <para>UploadFile:</para> /// /// <para>Uploads a local file to File Service, caller thread will be blocked before it is done</para> /// /// <para>Check <seealso cref="IBFileServiceInterface.UploadFile"/> for detailed documentation</para> /// /// </summary> public bool UploadFile( BStringOrStream _LocalFileOrStream, string _BucketName, string _KeyInBucket, EBRemoteFileReadPublicity _RemoteFileReadAccess = EBRemoteFileReadPublicity.AuthenticatedRead, Tuple <string, string>[] _FileTags = null, Action <string> _ErrorMessageAction = null) { if (GSClient == null) { _ErrorMessageAction?.Invoke("BFileServiceGC->UploadFile: GSClient is null."); return(false); } PredefinedObjectAcl CloudTypePublicity; if (_RemoteFileReadAccess == EBRemoteFileReadPublicity.PublicRead) { CloudTypePublicity = PredefinedObjectAcl.PublicRead; } else if (_RemoteFileReadAccess == EBRemoteFileReadPublicity.ProjectWideProtectedRead) { CloudTypePublicity = PredefinedObjectAcl.ProjectPrivate; } else { CloudTypePublicity = PredefinedObjectAcl.AuthenticatedRead; } Google.Apis.Storage.v1.Data.Object UploadedObject = null; if (_LocalFileOrStream.Type == EBStringOrStreamEnum.String) { if (!BUtility.DoesFileExist( _LocalFileOrStream.String, out bool bLocalFileExists, _ErrorMessageAction)) { _ErrorMessageAction?.Invoke("BFileServiceGC->UploadFile: DoesFileExist failed."); return(false); } if (!bLocalFileExists) { _ErrorMessageAction?.Invoke("BFileServiceGC->UploadFile: Local file does not exist."); return(false); } using (FileStream FS = new FileStream(_LocalFileOrStream.String, FileMode.Open, FileAccess.Read)) { try { UploadedObject = GSClient.UploadObject(_BucketName, _KeyInBucket, null, FS, new UploadObjectOptions { PredefinedAcl = CloudTypePublicity }); if (UploadedObject == null) { _ErrorMessageAction?.Invoke("BFileServiceGC->UploadFile: Operation has failed."); return(false); } } catch (Exception e) { _ErrorMessageAction?.Invoke("BFileServiceGC->UploadFile: " + e.Message + ", Trace: " + e.StackTrace); return(false); } } } else { try { UploadedObject = GSClient.UploadObject(_BucketName, _KeyInBucket, null, _LocalFileOrStream.Stream, new UploadObjectOptions { PredefinedAcl = CloudTypePublicity }); if (UploadedObject == null) { _ErrorMessageAction?.Invoke("BFileServiceGC->UploadFile: Operation has failed."); return(false); } } catch (Exception e) { _ErrorMessageAction?.Invoke("BFileServiceGC->UploadFile: " + e.Message + ", Trace: " + e.StackTrace); return(false); } } if (UploadedObject != null) { UploadedObject.CacheControl = CloudTypePublicity == PredefinedObjectAcl.PublicRead ? "public" : "private"; if (_FileTags != null && _FileTags.Length > 0) { Dictionary <string, string> NewMetadata = new Dictionary <string, string>(); foreach (Tuple <string, string> CurrentTag in _FileTags) { NewMetadata.Add(CurrentTag.Item1, CurrentTag.Item2); } UploadedObject.Metadata = NewMetadata; } var FileName = _KeyInBucket; var FileNameIndex = _KeyInBucket.LastIndexOf("/") + 1; if (FileNameIndex > 0) { FileName = _KeyInBucket.Substring(FileNameIndex, _KeyInBucket.Length - FileNameIndex); } UploadedObject.ContentDisposition = "inline; filename=" + FileName; GSClient.PatchObject(UploadedObject, null); } return(true); }