Пример #1
0
        /// <summary>
        /// Download a file object from GCS
        /// </summary>
        private void DownloadObject(StorageClient client, Google.Apis.Storage.v1.Data.Object gcsObject,
                                    FileObject fileObject, ObjectVersion version)
        {
            try
            {
                using (MemoryStream memStm = new MemoryStream())
                {
                    Console.ForegroundColor = ConsoleColor.Gray;
                    Helpers.WriteProgress(0);

                    _fileSize = (long)gcsObject.Size.GetValueOrDefault();
                    client.DownloadObject(gcsObject, memStm, new DownloadObjectOptions()
                    {
                        Generation = version.Generation
                    }, this);

                    memStm.Position        = 0;
                    fileObject.DataContent = memStm.ToArray();
                }
            }
            catch (Exception ex)
            {
                throw new SmkException(string.Format(ErrorResources.GcsBucketSource_DownloadingError, gcsObject.Name), ex);
            }
        }
Пример #2
0
        /// <summary>
        /// Restore the object for a specific date time
        /// </summary>
        public bool Restore(ObjectVersion version, ref string destination)
        {
            try
            {
                using (StorageClient _client = StorageClient.Create(GoogleCredential.FromFile(_apiKey)))
                {
                    var gen = new GetObjectOptions()
                    {
                        Generation = version.Generation
                    };

                    var obj = _client.GetObject(_bucketName, version.Name, gen);

                    string originalMD5, metadataMD5, metadataEncrypted;

                    if (!obj.Metadata.TryGetValue(Constants.OriginalMD5Key, out originalMD5) ||
                        !obj.Metadata.TryGetValue(Constants.MetadataMD5Key, out metadataMD5) ||
                        !obj.Metadata.TryGetValue(Constants.MetadataEncryptedKey, out metadataEncrypted))
                    {
                        _logger.WriteLog(ErrorCodes.GcsRestore_MissingMetadata,
                                         string.Format(ErrorResources.GcsRestore_MissingMetadata, obj.Name),
                                         Severity.Error, VerboseLevel.User);
                        return(false);
                    }

                    FileObject fo = new FileObject()
                    {
                        Metadata = new FileMetadata()
                        {
                            OriginalMD5 = originalMD5
                        },
                        DataContent     = null,
                        IsSecured       = true,
                        MetadataMD5     = metadataMD5,
                        MetadataContent = metadataEncrypted
                    };

                    DownloadObject(_client, obj, fo, version);

                    Helpers.WriteProgress("Decrypting...");
                    fo = new UnsecureTransform(_crypto_key, _crypto_iv, _logger).Process(fo);

                    Helpers.WriteProgress("Saving...");
                    Helpers.WriteProgress("");

                    // Force a destination name if not given by the user
                    if (string.IsNullOrEmpty(destination))
                    {
                        string destName = version.Name.Replace(Constants.EncryptedExt, "");
                        destination = Path.GetFileNameWithoutExtension(destName);
                        destination = $"{destination}.{version.Generation}.restore{Path.GetExtension(destName)}";
                    }

                    File.WriteAllBytes(destination, fo.DataContent);
                    File.SetAttributes(destination, fo.Metadata.Attributes);
                    File.SetLastWriteTime(destination, fo.Metadata.LastWriteTime);

                    return(true);
                }
            }
            catch (Exception ex)
            {
                _logger.WriteLog(ErrorCodes.GcsRestore_RestoreObjectException,
                                 ErrorResources.GcsRestore_RestoreObjectException + Environment.NewLine + ex.Message,
                                 Severity.Error, VerboseLevel.User);
                return(false);
            }
        }
        /// <summary>
        /// Restore the object for a specific date time
        /// </summary>
        public bool Restore(ObjectVersion version, ref string destination)
        {
            try
            {
                CloudBlockBlob blob = (CloudBlockBlob)version.ObjectData;

                if (blob.IsDeleted)
                {
                    blob.UndeleteAsync().Wait();
                }

                // Extracts the metadata
                string originalMD5, metadataMD5, metadataEncrypted;

                if (!blob.Metadata.TryGetValue(Constants.OriginalMD5Key, out originalMD5) ||
                    !blob.Metadata.TryGetValue(Constants.MetadataMD5Key, out metadataMD5) ||
                    !blob.Metadata.TryGetValue(Constants.MetadataEncryptedKey.Replace(".", ""), out metadataEncrypted))
                {
                    _logger.WriteLog(ErrorCodes.AbsObjectRestore_MissingMetadata,
                                     string.Format(ErrorResources.AbsObjectRestore_MissingMetadata, blob.Name),
                                     Severity.Error, VerboseLevel.User);
                    return(false);
                }

                FileObject fo = new FileObject()
                {
                    Metadata = new FileMetadata()
                    {
                        OriginalMD5 = originalMD5
                    },
                    DataContent     = null,
                    IsSecured       = true,
                    MetadataMD5     = metadataMD5,
                    MetadataContent = metadataEncrypted
                };

                DownloadObject(blob, fo);

                Helpers.WriteProgress("Decrypting...");
                fo = new UnsecureTransform(_crypto_key, _crypto_iv, _logger).Process(fo);

                Helpers.WriteProgress("Saving...");
                Helpers.WriteProgress("");

                // Force a destination name if not given by the user
                if (string.IsNullOrEmpty(destination))
                {
                    string destName = version.Name.Replace(Constants.EncryptedExt, "");
                    destination = Path.GetFileNameWithoutExtension(destName);
                    destination = $"{destination}.{version.VersionId}.restore{Path.GetExtension(destName)}";
                }

                File.WriteAllBytes(destination, fo.DataContent);
                File.SetAttributes(destination, fo.Metadata.Attributes);
                File.SetLastWriteTime(destination, fo.Metadata.LastWriteTime);

                return(true);
            }
            catch (Exception ex)
            {
                _logger.WriteLog(ErrorCodes.AbsBlobRestore_RestoreObjectException,
                                 ErrorResources.AbsBlobRestore_RestoreObjectException + Environment.NewLine + ex.Message,
                                 Severity.Error, VerboseLevel.User);
                return(false);
            }
        }
Пример #4
0
        /// <summary>
        /// Restore the object for a specific date time
        /// </summary>
        public bool Restore(ObjectVersion version, ref string destination)
        {
            try
            {
                GetObjectRequest request = new GetObjectRequest()
                {
                    BucketName = _bucketName,
                    Key        = version.Name,
                    VersionId  = (string)version.VersionId
                };

                using (GetObjectResponse response = _client.GetObjectAsync(request).Result)
                {
                    response.WriteObjectProgressEvent += ResponseWriteObjectProgressEvent;

                    // Extracts the metadata values
                    string originalMD5 = null, metadataMD5 = null, metadataEncrypted = null;
                    if (!ExtractMetadata(version.Name, response.Metadata, out originalMD5, out metadataMD5, out metadataEncrypted))
                    {
                        return(false);
                    }

                    FileObject fo = new FileObject()
                    {
                        Metadata = new FileMetadata()
                        {
                            OriginalMD5 = originalMD5
                        },
                        DataContent     = null,
                        IsSecured       = true,
                        MetadataMD5     = metadataMD5,
                        MetadataContent = metadataEncrypted
                    };

                    // Extracts the file content
                    using (MemoryStream stm = new MemoryStream())
                    {
                        response.ResponseStream.CopyTo(stm);
                        stm.Position   = 0;
                        fo.DataContent = stm.ToArray();
                    }

                    Helpers.WriteProgress("Decrypting...");
                    fo = new UnsecureTransform(_crypto_key, _crypto_iv, _logger).Process(fo);

                    Helpers.WriteProgress("Saving...");
                    Helpers.WriteProgress("");

                    // Force a destination name if not given by the user
                    if (string.IsNullOrEmpty(destination))
                    {
                        string destName = version.Name.Replace(Constants.EncryptedExt, "");
                        destination = Path.GetFileNameWithoutExtension(destName);
                        destination = $"{destination}.{version.VersionId}.restore{Path.GetExtension(destName)}";
                    }

                    File.WriteAllBytes(destination, fo.DataContent);
                    File.SetAttributes(destination, fo.Metadata.Attributes);
                    File.SetLastWriteTime(destination, fo.Metadata.LastWriteTime);

                    return(true);
                }
            }
            catch (Exception ex)
            {
                _logger.WriteLog(ErrorCodes.S3ObjectRestore_RestoreObjectException,
                                 ErrorResources.S3ObjectRestore_RestoreObjectException + Environment.NewLine + ex.Message,
                                 Severity.Error, VerboseLevel.User);
                return(false);
            }
        }