Exemplo n.º 1
0
        public void ModifyRequest_AllOptions_NegativeMatch()
        {
            var request = new RewriteRequest(null, null, "sourceBucket", "sourceObject", "destBucket", "destObject");
            var options = new CopyObjectOptions
            {
                DestinationPredefinedAcl       = PredefinedObjectAcl.Private,
                IfGenerationNotMatch           = 1L,
                IfMetagenerationNotMatch       = 2L,
                IfSourceGenerationNotMatch     = 3L,
                IfSourceMetagenerationNotMatch = 4L,
                Projection       = Projection.Full,
                SourceGeneration = 5L
            };

            options.ModifyRequest(request);
            Assert.Equal(RewriteRequest.DestinationPredefinedAclEnum.Private__, request.DestinationPredefinedAcl);
            Assert.Null(request.IfGenerationMatch);
            Assert.Equal(1L, request.IfGenerationNotMatch);
            Assert.Null(request.IfMetagenerationMatch);
            Assert.Equal(2L, request.IfMetagenerationNotMatch);
            Assert.Null(request.IfSourceGenerationMatch);
            Assert.Equal(3L, request.IfSourceGenerationNotMatch);
            Assert.Null(request.IfSourceMetagenerationMatch);
            Assert.Equal(4L, request.IfSourceMetagenerationNotMatch);
            Assert.Equal(5L, request.SourceGeneration);
        }
Exemplo n.º 2
0
        public void ModifyRequest_MatchNotMatchConflicts()
        {
            var request = new RewriteRequest(null, null, "sourceBucket", "sourceObject", "destBucket", "destObject");

            Assert.Throws <ArgumentException>(() =>
            {
                var options = new CopyObjectOptions {
                    IfGenerationMatch = 1L, IfGenerationNotMatch = 2L
                };
                options.ModifyRequest(request);
            });
            Assert.Throws <ArgumentException>(() =>
            {
                var options = new CopyObjectOptions {
                    IfMetagenerationMatch = 1L, IfMetagenerationNotMatch = 2L
                };
                options.ModifyRequest(request);
            });
            Assert.Throws <ArgumentException>(() =>
            {
                var options = new CopyObjectOptions {
                    IfSourceGenerationMatch = 1L, IfSourceGenerationNotMatch = 2L
                };
                options.ModifyRequest(request);
            });
            Assert.Throws <ArgumentException>(() =>
            {
                var options = new CopyObjectOptions {
                    IfSourceMetagenerationMatch = 1L, IfSourceMetagenerationNotMatch = 2L
                };
                options.ModifyRequest(request);
            });
        }
 public override Task <Object> CopyObjectAsync(
     string sourceBucket,
     string sourceObjectName,
     string destinationBucket            = null,
     string destinationObjectName        = null,
     CopyObjectOptions options           = null,
     CancellationToken cancellationToken = default(CancellationToken))
 {
     return(Task.Run(() => CopyObject(sourceBucket, sourceObjectName, destinationBucket, destinationObjectName, options)));
 }
Exemplo n.º 4
0
        public void CreateBucketAndObjects()
        {
            // Note: this test may fail if the project location prevents multi-regional storage.
            string initialBucketStorageClass = StorageClasses.Standard;
            string updatedObjectStorageClass = StorageClasses.Coldline;
            string updatedBucketStorageClass = StorageClasses.Nearline;

            string bucketName = _fixture.BucketPrefix + "storage-classes";
            string objectName = IdGenerator.FromGuid();
            var    client     = _fixture.Client;

            client.CreateBucket(_fixture.ProjectId, new Bucket {
                Name = bucketName, StorageClass = initialBucketStorageClass
            });
            StorageFixture.SleepAfterBucketCreateDelete();
            _fixture.RegisterBucketToDelete(bucketName);

            var bucket = client.GetBucket(bucketName);

            Assert.Equal(initialBucketStorageClass, bucket.StorageClass);

            var obj = client.UploadObject(bucketName, objectName, "application/octet-stream", TestHelpers.GenerateData(100));

            Assert.Equal(initialBucketStorageClass, obj.StorageClass);

            // Change storage class via a rewrite
            var options = new CopyObjectOptions
            {
                ExtraMetadata = new Object {
                    StorageClass = updatedObjectStorageClass
                }
            };

            client.CopyObject(bucketName, objectName, bucketName, objectName, options);

            // Fetch separately rather than trusting the return value of CopyObject...
            obj = client.GetObject(bucketName, objectName);
            Assert.Equal(updatedObjectStorageClass, obj.StorageClass);

            bucket.StorageClass = updatedBucketStorageClass;
            client.UpdateBucket(bucket);
            // Wait for 10 seconds to allow the bucket changes to take effect. While a lot of the time
            // the change is effective immediately, this pause just makes things more reliably - and is pretty reasonable
            // given the distributed nature of the system.
            Thread.Sleep(TimeSpan.FromSeconds(10));

            string objectName2 = IdGenerator.FromGuid();

            var obj2 = client.UploadObject(bucketName, objectName2, "application/octet-stream", TestHelpers.GenerateData(100));

            Assert.Equal(updatedBucketStorageClass, obj2.StorageClass);
        }
Exemplo n.º 5
0
        private static CopyObjectOptions GetCopyOptions(GxFileType fileType)
        {
            CopyObjectOptions options = new CopyObjectOptions();

            if (fileType.HasFlag(GxFileType.Private))
            {
                options.DestinationPredefinedAcl = PredefinedObjectAcl.ProjectPrivate;
            }
            else
            {
                options.DestinationPredefinedAcl = PredefinedObjectAcl.PublicRead;
            }
            return(options);
        }
        public void CreateBucketAndObjects()
        {
            // Note: this test may fail if the project location prevents multi-regional storage.
            string initialBucketStorageClass = StorageClasses.MultiRegional;
            string updatedObjectStorageClass = StorageClasses.Coldline;
            string updatedBucketStorageClass = StorageClasses.Nearline;

            string bucketName = _fixture.BucketPrefix + "storage-classes";
            string objectName = IdGenerator.FromGuid();
            var    client     = _fixture.Client;

            client.CreateBucket(_fixture.ProjectId, new Bucket {
                Name = bucketName, StorageClass = initialBucketStorageClass
            });
            StorageFixture.SleepAfterBucketCreateDelete();
            _fixture.RegisterBucketToDelete(bucketName);

            var bucket = client.GetBucket(bucketName);

            Assert.Equal(initialBucketStorageClass, bucket.StorageClass);

            var obj = client.UploadObject(bucketName, objectName, "application/octet-stream", TestHelpers.GenerateData(100));

            Assert.Equal(initialBucketStorageClass, obj.StorageClass);

            // Change storage class via a rewrite
            var options = new CopyObjectOptions
            {
                ExtraMetadata = new Object {
                    StorageClass = updatedObjectStorageClass
                }
            };

            client.CopyObject(bucketName, objectName, bucketName, objectName, options);

            // Fetch separately rather than trusting the return value of CopyObject...
            obj = client.GetObject(bucketName, objectName);
            Assert.Equal(updatedObjectStorageClass, obj.StorageClass);

            bucket.StorageClass = updatedBucketStorageClass;
            client.UpdateBucket(bucket);
            string objectName2 = IdGenerator.FromGuid();

            var obj2 = client.UploadObject(bucketName, objectName2, "application/octet-stream", TestHelpers.GenerateData(100));

            Assert.Equal(updatedBucketStorageClass, obj2.StorageClass);
        }
Exemplo n.º 7
0
        public void ModifyRequest_DefaultOptions()
        {
            var request = new RewriteRequest(null, null, "sourceBucket", "sourceObject", "destBucket", "destObject");
            var options = new CopyObjectOptions();

            options.ModifyRequest(request);
            Assert.Null(request.DestinationPredefinedAcl);
            Assert.Null(request.IfGenerationMatch);
            Assert.Null(request.IfGenerationNotMatch);
            Assert.Null(request.IfMetagenerationMatch);
            Assert.Null(request.IfMetagenerationNotMatch);
            Assert.Null(request.IfSourceGenerationMatch);
            Assert.Null(request.IfSourceGenerationNotMatch);
            Assert.Null(request.IfSourceMetagenerationMatch);
            Assert.Null(request.IfSourceMetagenerationNotMatch);
            Assert.Null(request.Projection);
            Assert.Null(request.SourceGeneration);
        }
Exemplo n.º 8
0
        public override Uri Copy(string srcdomain, string srcpath, string newdomain, string newpath)
        {
            var storage = GetStorage();

            var srcKey = MakePath(srcdomain, srcpath);
            var dstKey = MakePath(newdomain, newpath);

            var size = GetFileSize(srcdomain, srcpath);

            CopyObjectOptions options = new CopyObjectOptions();

            options.DestinationPredefinedAcl = GetDomainACL(newdomain);

            storage.CopyObject(_bucket, MakePath(srcdomain, srcpath), _bucket, MakePath(newdomain, newpath), options);

            QuotaUsedAdd(newdomain, size);

            return(GetUri(newdomain, newpath));
        }
    public Google.Apis.Storage.v1.Data.Object CopyFileArchivedGeneration(
        string sourceBucketName = "source-bucket-name",
        string sourceObjectName = "source-file",
        string destBucketName   = "destination-bucket-name",
        string destObjectName   = "destination-file-name",
        long?generation         = 1579287380533984)
    {
        var storage     = StorageClient.Create();
        var copyOptions = new CopyObjectOptions
        {
            SourceGeneration = generation
        };

        var copiedFile = storage.CopyObject(sourceBucketName, sourceObjectName,
                                            destBucketName, destObjectName, copyOptions);

        Console.WriteLine($"Generation {generation} of the object {sourceBucketName}/{sourceObjectName} " +
                          $"was copied to to {destBucketName}/{destObjectName}.");

        return(copiedFile);
    }
        // *************************************************************************************************************
        // GET OBJECT

        public override Object CopyObject(
            string sourceBucket,
            string sourceObjectName,
            string destinationBucket,
            string destinationObjectName,
            CopyObjectOptions options = null)
        {
            var sourceKey = Key(sourceBucket, sourceObjectName);
            var targetkey = Key(destinationBucket, destinationObjectName);

            if (_entries.TryGetValue(sourceKey, out var source))
            {
                var entry = new Entry(destinationObjectName, source.MediaType, source.Data);
                _entries.AddOrUpdate(targetkey, entry, (_, __) => entry);
                return(entry.ToObject(destinationBucket));
            }
            else
            {
                // FIXME: Google compatible exception
                throw new InvalidOperationException($"No object found for {sourceKey}.");
            }
        }