public override Task <Object> GetObjectAsync(
     string bucket,
     string objectName,
     GetObjectOptions options            = null,
     CancellationToken cancellationToken = default(CancellationToken))
 {
     return(Task.Run(() => GetObject(bucket, objectName, options)));
 }
 internal LoadDocumentResult(long generation, XDocument document)
 {
     Generation       = generation;
     Document         = document;
     GetObjectOptions = new GetObjectOptions {
         IfGenerationNotMatch = generation
     };
 }
        // *************************************************************************************************************
        // GET OBJECT


        public override Object GetObject(string bucket, string objectName, GetObjectOptions options = null)
        {
            var key = Key(bucket, objectName);

            if (_entries.TryGetValue(key, out var entry))
            {
                return(entry.ToObject(bucket));
            }
            // FIXME: Google compatible exception
            throw new InvalidOperationException($"No object found for {key}.");
        }
        public void ModifyRequest_DefaultOptions()
        {
            var request = new GetRequest(null, "bucket", "object");
            var options = new GetObjectOptions();

            options.ModifyRequest(request);
            Assert.Null(request.Generation);
            Assert.Null(request.IfGenerationMatch);
            Assert.Null(request.IfGenerationNotMatch);
            Assert.Null(request.IfMetagenerationMatch);
            Assert.Null(request.IfMetagenerationNotMatch);
            Assert.Null(request.Projection);
        }
示例#5
0
        public void ModifyRequest_MatchNotMatchConflicts()
        {
            var request = new GetRequest(null, "bucket", "object");

            Assert.Throws <ArgumentException>(() =>
            {
                var options = new GetObjectOptions {
                    IfGenerationMatch = 1L, IfGenerationNotMatch = 2L
                };
                options.ModifyRequest(request);
            });
            Assert.Throws <ArgumentException>(() =>
            {
                var options = new GetObjectOptions {
                    IfMetagenerationMatch = 1L, IfMetagenerationNotMatch = 2L
                };
                options.ModifyRequest(request);
            });
        }
示例#6
0
        public void ModifyRequest_NegativeMatchOptions()
        {
            var request = new GetRequest(null, "bucket", "object");
            var options = new GetObjectOptions
            {
                IfGenerationNotMatch     = 1L,
                IfMetagenerationNotMatch = 2L,
                Generation = 3L,
                Projection = Projection.Full
            };

            options.ModifyRequest(request);
            Assert.Null(request.IfGenerationMatch);
            Assert.Equal(1L, request.IfGenerationNotMatch);
            Assert.Null(request.IfMetagenerationMatch);
            Assert.Equal(2L, request.IfMetagenerationNotMatch);
            Assert.Equal(3L, request.Generation);
            Assert.Equal(ProjectionEnum.Full, request.Projection);
        }
示例#7
0
 public GoogleObject GetObject(string bucket, string objectName, GetObjectOptions options = null)
 {
     return(_wrappedClient.GetObject(bucket, objectName, options));
 }
 /// <summary>
 /// This is only here so it can implement an interface which so it can be mocked.
 /// </summary>
 /// <param name="bucket"></param>
 /// <param name="objectName"></param>
 /// <param name="options"></param>
 /// <returns></returns>
 public override Object GetObject(string bucket, string objectName, GetObjectOptions options = null)
 {
     return(base.GetObject(bucket, objectName, options));
 }
        /// <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);
            }
        }