Beispiel #1
0
        /// <summary>
        ///
        /// <para>DownloadFile:</para>
        ///
        /// <para>Downloads a file from File Service and stores locally/or to stream, caller thread will be blocked before it is done</para>
        ///
        /// <para>Check <seealso cref="IBFileServiceInterface.DownloadFile"/> for detailed documentation</para>
        ///
        /// </summary>
        public bool DownloadFile(string _BucketName, string _KeyInBucket, BStringOrStream _Destination, Action <string> _ErrorMessageAction = null, ulong _StartIndex = 0, ulong _Size = 0)
        {
            BlobContainerClient       ContainerClient = AServiceClient.GetBlobContainerClient(_BucketName);
            BlobClient                Blob            = ContainerClient.GetBlobClient(_KeyInBucket);
            Response <BlobProperties> Response        = Blob.GetProperties();

            if (AServiceClient == null)
            {
                _ErrorMessageAction?.Invoke("BFileServiceAZ->DownloadFile: AServiceClient is null.");
                return(false);
            }

            if (!CheckFileExistence(
                    _BucketName,
                    _KeyInBucket,
                    out bool bExists,
                    _ErrorMessageAction
                    ))
            {
                _ErrorMessageAction?.Invoke("BFileServiceAZ->DownloadFile: CheckFileExistence failed.");
                return(false);
            }

            if (!bExists)
            {
                _ErrorMessageAction?.Invoke("BFileServiceAZ->DownloadFile: File does not exist in the File Service.");
                return(false);
            }

            HttpRange Range = default(HttpRange);

            if (_Size > 0)
            {
                Range = new HttpRange((long)_StartIndex, (long)(_StartIndex + _Size));
            }

            try
            {
                if (_Destination.Type == EBStringOrStreamEnum.String)
                {
                    using (FileStream FS = File.Create(_Destination.String))
                    {
                        BlobDownloadInfo DlInfo = Blob.Download(Range).Value;
                        DlInfo.Content.CopyTo(FS);

                        DlInfo.Dispose();
                    }

                    if (!BUtility.DoesFileExist(
                            _Destination.String,
                            out bool bLocalFileExists,
                            _ErrorMessageAction))
                    {
                        _ErrorMessageAction?.Invoke("BFileServiceAZ->DownloadFile: DoesFileExist failed.");
                        return(false);
                    }

                    if (!bLocalFileExists)
                    {
                        _ErrorMessageAction?.Invoke("BFileServiceAZ->DownloadFile: Download finished, but still file does not locally exist.");
                        return(false);
                    }
                }
                else
                {
                    if (_Destination.Stream == null)
                    {
                        _ErrorMessageAction?.Invoke("BFileServiceAZ->DownloadFile: Destination stream is null.");
                        return(false);
                    }


                    BlobDownloadInfo DlInfo = Blob.Download(Range).Value;
                    DlInfo.Content.CopyTo(_Destination.Stream);
                    DlInfo.Dispose();

                    try
                    {
                        _Destination.Stream.Position = 0;
                    }
                    catch (Exception) { }
                }
            }
            catch (Exception e)
            {
                _ErrorMessageAction?.Invoke("BFileServiceAZ->DownloadFile: " + e.Message + ", Trace: " + e.StackTrace);
                return(false);
            }
            return(true);
        }
Beispiel #2
0
        /// <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);
        }
Beispiel #3
0
        /// <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);
        }
Beispiel #4
0
        /// <summary>
        ///
        /// <para>DownloadFile:</para>
        ///
        /// <para>Downloads a file from File Service and stores locally/or to stream, caller thread will be blocked before it is done</para>
        ///
        /// <para>Check <seealso cref="IBFileServiceInterface.DownloadFile"/> for detailed documentation</para>
        ///
        /// </summary>
        public bool DownloadFile(
            string _BucketName,
            string _KeyInBucket,
            BStringOrStream _Destination,
            Action <string> _ErrorMessageAction = null,
            UInt64 _StartIndex = 0,
            UInt64 _Size       = 0)
        {
            //TODO: StartIndex and Size implementation

            if (_Destination == null)
            {
                _ErrorMessageAction?.Invoke("BFileServiceAWS->DownloadFile: _Destination is null.");
                return(false);
            }

            if (!CheckFileExistence(
                    _BucketName,
                    _KeyInBucket,
                    out bool bExists,
                    _ErrorMessageAction
                    ))
            {
                _ErrorMessageAction?.Invoke("BFileServiceAWS->DownloadFile: CheckFileExistence failed.");
                return(false);
            }

            if (!bExists)
            {
                _ErrorMessageAction?.Invoke("BFileServiceAWS->DownloadFile: File does not exist in the File Service.");
                return(false);
            }

            if (_Destination.Type == EBStringOrStreamEnum.String)
            {
                if (TransferUtil == null)
                {
                    _ErrorMessageAction?.Invoke("BFileServiceAWS->DownloadFile: TransferUtil is null.");
                    return(false);
                }

                try
                {
                    TransferUtil.Download(
                        _Destination.String,
                        _BucketName,
                        _KeyInBucket
                        );
                }
                catch (Exception e)
                {
                    _ErrorMessageAction?.Invoke("BFileServiceAWS->DownloadFile: " + e.Message + ", Trace: " + e.StackTrace);
                    return(false);
                }

                if (!BUtility.DoesFileExist(
                        _Destination.String,
                        out bool bLocalFileExists,
                        _ErrorMessageAction))
                {
                    _ErrorMessageAction?.Invoke("BFileServiceAWS->DownloadFile: DoesFileExist failed.");
                    return(false);
                }

                if (!bLocalFileExists)
                {
                    _ErrorMessageAction?.Invoke("BFileServiceAWS->DownloadFile: Download finished, but still file does not locally exist.");
                    return(false);
                }
            }
            else
            {
                if (S3Client == null)
                {
                    _ErrorMessageAction?.Invoke("BFileServiceAWS->DownloadFile: S3Client is null.");
                    return(false);
                }

                if (_Destination.Stream == null)
                {
                    _ErrorMessageAction?.Invoke("BFileServiceAWS->DownloadFile: Destination stream is null.");
                    return(false);
                }

                GetObjectRequest GetRequest = new GetObjectRequest
                {
                    BucketName = _BucketName,
                    Key        = _KeyInBucket
                };

                try
                {
                    using (var CreatedTask = S3Client.GetObjectAsync(GetRequest))
                    {
                        CreatedTask.Wait();

                        using (GetObjectResponse GetResponse = CreatedTask.Result)
                        {
                            try
                            {
                                GetResponse.ResponseStream.CopyTo(_Destination.Stream);
                            }
                            catch (Exception e)
                            {
                                _ErrorMessageAction?.Invoke("BFileServiceAWS->DownloadFile: " + e.Message + ", Trace: " + e.StackTrace);
                                return(false);
                            }
                        }
                    }

                    _Destination.Stream.Position = 0;
                }
                catch (Exception e)
                {
                    _ErrorMessageAction?.Invoke("BFileServiceAWS->DownloadFile: " + e.Message + ", Trace: " + e.StackTrace);
                }
            }
            return(true);
        }
Beispiel #5
0
        /// <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);
        }
Beispiel #6
0
        /// <summary>
        ///
        /// <para>DownloadFile:</para>
        ///
        /// <para>Downloads a file from File Service and stores locally/or to stream, caller thread will be blocked before it is done</para>
        ///
        /// <para>Check <seealso cref="IBFileServiceInterface.DownloadFile"/> for detailed documentation</para>
        ///
        /// </summary>
        public bool DownloadFile(
            string _BucketName,
            string _KeyInBucket,
            BStringOrStream _Destination,
            Action <string> _ErrorMessageAction = null,
            UInt64 _StartIndex = 0,
            UInt64 _Size       = 0)
        {
            if (GSClient == null)
            {
                _ErrorMessageAction?.Invoke("BFileServiceGC->DownloadFile: GSClient is null.");
                return(false);
            }

            if (!CheckFileExistence(
                    _BucketName,
                    _KeyInBucket,
                    out bool bExists,
                    _ErrorMessageAction
                    ))
            {
                _ErrorMessageAction?.Invoke("BFileServiceGC->DownloadFile: CheckFileExistence failed.");
                return(false);
            }

            if (!bExists)
            {
                _ErrorMessageAction?.Invoke("BFileServiceGC->DownloadFile: File does not exist in the File Service.");
                return(false);
            }

            DownloadObjectOptions DOO = null;

            if (_Size > 0)
            {
                DOO = new DownloadObjectOptions()
                {
                    Range = new System.Net.Http.Headers.RangeHeaderValue((long)_StartIndex, (long)(_StartIndex + _Size))
                };
            }

            try
            {
                if (_Destination.Type == EBStringOrStreamEnum.String)
                {
                    using (FileStream FS = File.Create(_Destination.String))
                    {
                        GSClient.DownloadObject(_BucketName, _KeyInBucket, FS, DOO);
                    }

                    if (!BUtility.DoesFileExist(
                            _Destination.String,
                            out bool bLocalFileExists,
                            _ErrorMessageAction))
                    {
                        _ErrorMessageAction?.Invoke("BFileServiceGC->DownloadFile: DoesFileExist failed.");
                        return(false);
                    }

                    if (!bLocalFileExists)
                    {
                        _ErrorMessageAction?.Invoke("BFileServiceGC->DownloadFile: Download finished, but still file does not locally exist.");
                        return(false);
                    }
                }
                else
                {
                    if (_Destination.Stream == null)
                    {
                        _ErrorMessageAction?.Invoke("BFileServiceGC->DownloadFile: Destination stream is null.");
                        return(false);
                    }

                    GSClient.DownloadObject(_BucketName, _KeyInBucket, _Destination.Stream, DOO);
                    try
                    {
                        _Destination.Stream.Position = 0;
                    }
                    catch (Exception) { }
                }
            }
            catch (Exception e)
            {
                _ErrorMessageAction?.Invoke("BFileServiceGC->DownloadFile: " + e.Message + ", Trace: " + e.StackTrace);
                return(false);
            }
            return(true);
        }
Beispiel #7
0
        public bool Start()
        {
            PrintAction?.Invoke("BDatabaseServicesTest->Info-> Test is starting.");

            if (SelectedFileService == null)
            {
                PrintAction?.Invoke("BFileServicesTest->Error-> Given SelectedFileService is null.");
                return(false);
            }

            if (BucketName == null || BucketName.Length == 0)
            {
                PrintAction?.Invoke("BFileServicesTest->Error-> Given BucketName is null or empty.");
                return(false);
            }

            if (FileKey == null || FileKey.Length == 0)
            {
                PrintAction?.Invoke("BFileServicesTest->Error-> Given FileKey is null or empty.");
                return(false);
            }

            if (FileLocalPath == null ||
                FileLocalPath.Length == 0 ||
                !BUtility.DoesFileExist(FileLocalPath, out bool bFileExists, (string Message) => PrintAction?.Invoke("BFileServicesTest->Error-> " + Message)) ||
                !bFileExists)
            {
                PrintAction?.Invoke("BFileServicesTest->Error-> Given FileLocalPath is null or empty or does not exist.");
                return(false);
            }

            if (!SelectedFileService.HasInitializationSucceed())
            {
                PrintAction?.Invoke("BFileServicesTest->Error-> Initialization failed.");
                return(false);
            }
            PrintAction?.Invoke("BFileServicesTest->Log-> Initialization succeed.");

            PreCleanup();

            if (!TestUploadFile())
            {
                return(false);
            }

            if (!TestCreateSignedDownloadUrl())
            {
                return(false);
            }

            if (!TestGetChecksum())
            {
                return(false);
            }

            if (!TestListFiles(1))
            {
                return(false);
            }

            if (!TestDeleteLocalFile())
            {
                return(false);
            }

            if (!TestDownloadFile())
            {
                return(false);
            }

            if (!TestGetFileSize())
            {
                return(false);
            }

            if (!TestGetFileTags())
            {
                return(false);
            }

            if (!TestSetFileTags())
            {
                return(false);
            }

            if (!TestGetFileTags())
            {
                return(false);
            }

            if (!TestCopyFile())
            {
                return(false);
            }

            if (!TestSetFileAccessibility())
            {
                return(false);
            }

            if (!TestDeleteFile(FileKey))
            {
                return(false);
            }

            if (!TestDeleteFile(FileKey + "_copy"))
            {
                return(false);
            }

            if (!TestCreateSignedUploadUrl())
            {
                return(false);
            }

            return(true);
        }