예제 #1
0
    public void ComposeObject()
    {
        UploadFileSample    uploadFileSample    = new UploadFileSample();
        ComposeObjectSample composeObjectSample = new ComposeObjectSample();
        DownloadFileSample  downloadFileSample  = new DownloadFileSample();

        var firstObject  = "HelloComposeObject.txt";
        var secondObject = "HelloComposeObjectAdditional.txt";
        var targetObject = "HelloComposedDownload.txt";

        uploadFileSample.UploadFile(_fixture.BucketNameGeneric, "Resources/Hello.txt", _fixture.Collect(firstObject));

        uploadFileSample.UploadFile(_fixture.BucketNameGeneric, "Resources/HelloDownloadCompleteByteRange.txt", _fixture.Collect(secondObject));

        composeObjectSample.ComposeObject(_fixture.BucketNameGeneric, firstObject, secondObject, _fixture.Collect(targetObject));

        // Download the composed file
        downloadFileSample.DownloadFile(_fixture.BucketNameGeneric, targetObject, targetObject);

        // Content from both file should exists in the downloaded file
        var targetContent = File.ReadAllText(targetObject);

        Assert.Contains(File.ReadAllText("Resources/Hello.txt"), targetContent);
        Assert.Contains(File.ReadAllText("Resources/HelloDownloadCompleteByteRange.txt"), targetContent);

        File.Delete(targetObject);
    }
    public void CopyFileArchivedGeneration()
    {
        UploadFileSample                   uploadFileSample                   = new UploadFileSample();
        BucketEnableVersioningSample       bucketEnableVersioningSample       = new BucketEnableVersioningSample();
        GetMetadataSample                  getMetadataSample                  = new GetMetadataSample();
        DownloadFileSample                 downloadFileSample                 = new DownloadFileSample();
        CopyFileArchivedGenerationSample   copyFileArchivedGenerationSample   = new CopyFileArchivedGenerationSample();
        DeleteFileArchivedGenerationSample deleteFileArchivedGenerationSample = new DeleteFileArchivedGenerationSample();
        BucketDisableVersioningSample      bucketDisableVersioningSample      = new BucketDisableVersioningSample();

        var objectName       = "HelloCopyArchive.txt";
        var copiedObjectName = "ByeCopy.txt";

        // Enable bucket versioning
        bucketEnableVersioningSample.BucketEnableVersioning(_fixture.BucketNameGeneric);

        // Uploaded for the first time
        uploadFileSample.UploadFile(_fixture.BucketNameGeneric, _fixture.FilePath, objectName);

        // Get generation of first version of the file
        var obj = getMetadataSample.GetMetadata(_fixture.BucketNameGeneric, objectName);
        var fileArchivedGeneration = obj.Generation;

        // Upload again to archive previous generation.
        uploadFileSample.UploadFile(_fixture.BucketNameGeneric, "Resources/HelloDownloadCompleteByteRange.txt", objectName);

        // Get generation of second version of the file
        obj = getMetadataSample.GetMetadata(_fixture.BucketNameGeneric, objectName);
        var fileCurrentGeneration = obj.Generation;


        _fixture.CollectArchivedFiles(_fixture.BucketNameGeneric, objectName, fileArchivedGeneration);
        _fixture.CollectArchivedFiles(_fixture.BucketNameGeneric, objectName, fileCurrentGeneration);

        try
        {
            // Copy first version of the file to new bucket.
            copyFileArchivedGenerationSample.CopyFileArchivedGeneration(_fixture.BucketNameGeneric, objectName,
                                                                        _fixture.BucketNameRegional, _fixture.CollectRegionalObject(copiedObjectName), fileArchivedGeneration);

            // Download copied file
            downloadFileSample.DownloadFile(_fixture.BucketNameRegional, copiedObjectName, copiedObjectName);

            // Match file contents with first version of the file
            Assert.Equal(File.ReadAllText(_fixture.FilePath), File.ReadAllText(copiedObjectName));
        }
        finally
        {
            File.Delete(copiedObjectName);

            // Disable bucket versioning
            bucketDisableVersioningSample.BucketDisableVersioning(_fixture.BucketNameGeneric);
        }
    }
예제 #3
0
    public void DeleteFileArchivedGeneration()
    {
        UploadFileSample                   uploadFileSample                   = new UploadFileSample();
        ListFilesSample                    listFilesSample                    = new ListFilesSample();
        BucketEnableVersioningSample       bucketEnableVersioningSample       = new BucketEnableVersioningSample();
        GetMetadataSample                  getMetadataSample                  = new GetMetadataSample();
        DownloadFileSample                 downloadFileSample                 = new DownloadFileSample();
        ListFileArchivedGenerationSample   listFileArchivedGenerationSample   = new ListFileArchivedGenerationSample();
        DeleteFileArchivedGenerationSample deleteFileArchivedGenerationSample = new DeleteFileArchivedGenerationSample();
        BucketDisableVersioningSample      bucketDisableVersioningSample      = new BucketDisableVersioningSample();

        var objectName = "HelloDeleteFileArchivedGeneration.txt";

        // Enable bucket versioning
        bucketEnableVersioningSample.BucketEnableVersioning(_fixture.BucketNameGeneric);

        // Uploaded for the first time
        uploadFileSample.UploadFile(_fixture.BucketNameGeneric, _fixture.FilePath, objectName);

        // Upload again to archive previous generation.
        uploadFileSample.UploadFile(_fixture.BucketNameGeneric, "Resources/HelloDownloadCompleteByteRange.txt", objectName);


        try
        {
            var objects = listFileArchivedGenerationSample.ListFileArchivedGeneration(_fixture.BucketNameGeneric);

            Assert.Equal(2, objects.Count(a => a.Name == objectName));

            // Get Generations
            var  testFiles = objects.Where(a => a.Name == objectName).ToList();
            long?fileArchivedGeneration = testFiles[0].Generation;
            long?fileCurrentGeneration  = testFiles[1].Generation;

            // Delete first generation of the file
            deleteFileArchivedGenerationSample.DeleteFileArchivedGeneration(_fixture.BucketNameGeneric, objectName, fileArchivedGeneration);

            objects = listFileArchivedGenerationSample.ListFileArchivedGeneration(_fixture.BucketNameGeneric);
            Assert.Equal(1, objects.Count(a => a.Name == objectName));

            // Delete second generation of the file
            deleteFileArchivedGenerationSample.DeleteFileArchivedGeneration(_fixture.BucketNameGeneric, objectName, fileCurrentGeneration);

            objects = listFileArchivedGenerationSample.ListFileArchivedGeneration(_fixture.BucketNameGeneric);
            Assert.Equal(0, objects.Count(a => a.Name == objectName));
        }
        finally
        {
            // Disable bucket versioning
            bucketDisableVersioningSample.BucketDisableVersioning(_fixture.BucketNameGeneric);
        }
    }
예제 #4
0
    public void ListFilesWithPrefix()
    {
        ListFilesWithPrefixSample listFilesWithPrefixSample = new ListFilesWithPrefixSample();
        UploadFileSample          uploadFileSample          = new UploadFileSample();

        // Upload 4 files.
        uploadFileSample.UploadFile(_fixture.BucketNameGeneric, _fixture.FilePath, _fixture.Collect("a/1.txt"));
        uploadFileSample.UploadFile(_fixture.BucketNameGeneric, _fixture.FilePath, _fixture.Collect("a/2.txt"));
        uploadFileSample.UploadFile(_fixture.BucketNameGeneric, _fixture.FilePath, _fixture.Collect("b/2.txt"));
        uploadFileSample.UploadFile(_fixture.BucketNameGeneric, _fixture.FilePath, _fixture.Collect("a/b/3.txt"));

        var files = listFilesWithPrefixSample.ListFilesWithPrefix(_fixture.BucketNameGeneric, "a/", null).ToList();

        Assert.Contains(files, c => c.Name == "a/1.txt");
        Assert.Contains(files, c => c.Name == "a/2.txt");
        Assert.Contains(files, c => c.Name == "a/b/3.txt");
    }
예제 #5
0
    public void ListFiles()
    {
        ListFilesSample  listFilesSample  = new ListFilesSample();
        UploadFileSample uploadFileSample = new UploadFileSample();

        uploadFileSample.UploadFile(_bucketFixture.BucketNameGeneric, _bucketFixture.FilePath, _bucketFixture.Collect("HelloListObjectsTest.txt"));

        var files = listFilesSample.ListFiles(_bucketFixture.BucketNameGeneric);

        Assert.Contains(files, c => c.Name == "HelloListObjectsTest.txt");
    }
예제 #6
0
    public void UploadFile()
    {
        UploadFileSample uploadFileSample = new UploadFileSample();
        ListFilesSample  listFilesSample  = new ListFilesSample();

        // upload file
        uploadFileSample.UploadFile(_bucketFixture.BucketNameGeneric, _bucketFixture.FilePath, _bucketFixture.Collect("UploadTest.txt"));

        var files = listFilesSample.ListFiles(_bucketFixture.BucketNameGeneric);

        Assert.Contains(files, c => c.Name == "UploadTest.txt");
    }
예제 #7
0
    public void DeleteFile()
    {
        UploadFileSample uploadFileSample = new UploadFileSample();
        DeleteFileSample deleteFileSample = new DeleteFileSample();
        ListFilesSample  listFilesSample  = new ListFilesSample();

        uploadFileSample.UploadFile(_bucketFixture.BucketNameGeneric, _bucketFixture.FilePath, "DeleteTest.txt");
        deleteFileSample.DeleteFile(_bucketFixture.BucketNameGeneric, "DeleteTest.txt");
        var files = listFilesSample.ListFiles(_bucketFixture.BucketNameGeneric).ToList();

        Assert.DoesNotContain(files, c => c.Name == "DeleteTest.txt");
    }
    public void MoveFile()
    {
        MoveFileSample    moveFileSample    = new MoveFileSample();
        UploadFileSample  uploadFileSample  = new UploadFileSample();
        GetMetadataSample getMetadataSample = new GetMetadataSample();

        uploadFileSample.UploadFile(_fixture.BucketNameGeneric, _fixture.FilePath, "HelloMove.txt");
        // Make sure the file doesn't exist until we move it there.
        Assert.Throws <GoogleApiException>(() => getMetadataSample.GetMetadata(_fixture.BucketNameGeneric, "ByeMove.txt"));

        moveFileSample.MoveFile(_fixture.BucketNameGeneric, "HelloMove.txt", _fixture.BucketNameGeneric, _fixture.Collect("ByeMove.txt"));

        var exception = Assert.Throws <GoogleApiException>(() => getMetadataSample.GetMetadata(_fixture.BucketNameGeneric, "HelloMove.txt"));

        Assert.Equal(System.Net.HttpStatusCode.NotFound, exception.HttpStatusCode);
    }
    public void TestSetTemporaryHold()
    {
        SetTemporaryHoldSample     setTemporaryHoldSample     = new SetTemporaryHoldSample();
        ReleaseTemporaryHoldSample releaseTemporaryHoldSample = new ReleaseTemporaryHoldSample();
        UploadFileSample           uploadFileSample           = new UploadFileSample();
        GetMetadataSample          getMetadataSample          = new GetMetadataSample();

        uploadFileSample.UploadFile(_bucketFixture.BucketNameRegional, _bucketFixture.FilePath, _bucketFixture.CollectRegionalObject("SetTemporaryHold.txt"));

        setTemporaryHoldSample.SetTemporaryHold(_bucketFixture.BucketNameRegional, "SetTemporaryHold.txt");

        var metadata = getMetadataSample.GetMetadata(_bucketFixture.BucketNameRegional, "SetTemporaryHold.txt");

        Assert.True(metadata.TemporaryHold);

        releaseTemporaryHoldSample.ReleaseTemporaryHold(_bucketFixture.BucketNameRegional, "SetTemporaryHold.txt");
    }
예제 #10
0
    public async Task DownloadByteRangeAsync()
    {
        UploadFileSample             uploadFileSample        = new UploadFileSample();
        DownloadByteRangeAsyncSample downloadByteRangeSample = new DownloadByteRangeAsyncSample();

        uploadFileSample.UploadFile(
            _bucketFixture.BucketNameGeneric, "Resources/HelloDownloadCompleteByteRange.txt",
            _bucketFixture.Collect("HelloDownloadCompleteByteRange.txt"));

        await downloadByteRangeSample.DownloadByteRangeAsync(
            _bucketFixture.BucketNameGeneric, "HelloDownloadCompleteByteRange.txt", 0, 20,
            "HelloDownloadCompleteByteRange.txt_0-20");

        var downloadedString = Encoding.UTF8.GetString(File.ReadAllBytes("HelloDownloadCompleteByteRange.txt_0-20"));

        Assert.Equal("\uFEFFHello Download Com", downloadedString);
        File.Delete("HelloDownloadCompleteByteRange.txt_0-20");
    }
    public void SetObjectMetadata()
    {
        GetMetadataSample       getMetadataSample       = new GetMetadataSample();
        UploadFileSample        uploadFileSample        = new UploadFileSample();
        SetObjectMetadataSample setObjectMetadataSample = new SetObjectMetadataSample();

        var key        = "key-to-add";
        var value      = "value-to-add";
        var objectName = "HelloSetObjectMetadata.txt";

        uploadFileSample.UploadFile(_fixture.BucketNameGeneric, _fixture.FilePath, _fixture.Collect(objectName));

        setObjectMetadataSample.SetObjectMetadata(_fixture.BucketNameGeneric, objectName, key, value);

        var file = getMetadataSample.GetMetadata(_fixture.BucketNameGeneric, objectName);

        Assert.Contains(file.Metadata, m => m.Key == key && m.Value == value);
    }
예제 #12
0
    public void TestPrintFileAclForUser()
    {
        PrintFileAclForUserSample printFileAclForUserSample = new PrintFileAclForUserSample();
        UploadFileSample          uploadFileSample          = new UploadFileSample();
        AddFileOwnerSample        addFileOwnerSample        = new AddFileOwnerSample();

        string userEmail = _bucketFixture.ServiceAccountEmail;

        uploadFileSample.UploadFile(_bucketFixture.BucketNameGeneric, _bucketFixture.FilePath, _bucketFixture.Collect("HelloAddObjectOwnerForUser.txt"));

        // add file owner
        addFileOwnerSample.AddFileOwner(_bucketFixture.BucketNameGeneric, "HelloAddObjectOwnerForUser.txt", userEmail);

        // Make sure we print-acl-for-user shows us the user, but not all the ACLs.
        var fileAclForUser = printFileAclForUserSample.PrintFileAclForUser(_bucketFixture.BucketNameGeneric, "HelloAddObjectOwnerForUser.txt", userEmail);

        Assert.All(fileAclForUser, c => Assert.Equal(c.Email, userEmail));
    }
    public void TestSetEventBasedHold()
    {
        ReleaseEventBasedHoldSample releaseEventBasedHoldSample = new ReleaseEventBasedHoldSample();
        SetEventBasedHoldSample     setEventBasedHoldSample     = new SetEventBasedHoldSample();
        UploadFileSample            uploadFileSample            = new UploadFileSample();
        GetMetadataSample           getMetadataSample           = new GetMetadataSample();

        uploadFileSample.UploadFile(_bucketFixture.BucketNameGeneric, _bucketFixture.FilePath, _bucketFixture.Collect("SetEventBasedHold.txt"));

        // Set event based hold.
        setEventBasedHoldSample.SetEventBasedHold(_bucketFixture.BucketNameGeneric, "SetEventBasedHold.txt");
        var metadata = getMetadataSample.GetMetadata(_bucketFixture.BucketNameGeneric, "SetEventBasedHold.txt");

        Assert.True(metadata.EventBasedHold);

        // Release event based hold.
        releaseEventBasedHoldSample.ReleaseEventBasedHold(_bucketFixture.BucketNameGeneric, "SetEventBasedHold.txt");
    }
    public void PrintFileAcl()
    {
        PrintFileAclSample printFileAclSample = new PrintFileAclSample();
        UploadFileSample   uploadFileSample   = new UploadFileSample();
        AddFileOwnerSample addFileOwnerSample = new AddFileOwnerSample();

        string userEmail = _fixture.ServiceAccountEmail;

        uploadFileSample.UploadFile(_fixture.BucketNameGeneric, _fixture.FilePath, _fixture.Collect("HelloAddObjectOwner.txt"));

        // print file acl
        var fileAcl = printFileAclSample.PrintObjectAcl(_fixture.BucketNameGeneric, "HelloAddObjectOwner.txt");

        Assert.DoesNotContain(fileAcl, c => c.Email == userEmail);

        // add file owner
        addFileOwnerSample.AddFileOwner(_fixture.BucketNameGeneric, "HelloAddObjectOwner.txt", userEmail);

        // print file acl
        fileAcl = printFileAclSample.PrintObjectAcl(_fixture.BucketNameGeneric, "HelloAddObjectOwner.txt");
        Assert.Contains(fileAcl, c => c.Email == userEmail);
    }
    public void DownloadPublicFile()
    {
        MakePublicSample         makePublicSample         = new MakePublicSample();
        UploadFileSample         uploadFileSample         = new UploadFileSample();
        DownloadPublicFileSample downloadPublicFileSample = new DownloadPublicFileSample();

        uploadFileSample.UploadFile(_fixture.BucketNameGeneric, _fixture.FilePath, _fixture.Collect("HelloDownloadPublic.txt"));

        // Make it public
        makePublicSample.MakePublic(_fixture.BucketNameGeneric, "HelloDownloadPublic.txt");

        // Try downloading without creds
        try
        {
            downloadPublicFileSample.DownloadPublicFile(_fixture.BucketNameGeneric, "HelloDownloadPublic.txt", "HelloDownloadPublic.txt");
            Assert.Equal(File.ReadAllText(_fixture.FilePath), File.ReadAllText("HelloDownloadPublic.txt"));
        }
        finally
        {
            File.Delete("HelloDownloadPublic.txt");
        }
    }
예제 #16
0
    public void MakePublic()
    {
        MakePublicSample  makePublicSample  = new MakePublicSample();
        UploadFileSample  uploadFileSample  = new UploadFileSample();
        GetMetadataSample getMetadataSample = new GetMetadataSample();

        uploadFileSample.UploadFile(_bucketFixture.BucketNameGeneric, _bucketFixture.FilePath, _bucketFixture.Collect("HelloMakePublic.txt"));

        var metadata = getMetadataSample.GetMetadata(_bucketFixture.BucketNameGeneric, "HelloMakePublic.txt");

        Assert.NotNull(metadata.MediaLink);

        // Before making the file public, fetching the medialink should throw an exception.
        WebClient webClient = new WebClient();

        Assert.Throws <WebException>(() => webClient.DownloadString(metadata.MediaLink));

        // Make it public and try fetching again.
        var medialink = makePublicSample.MakePublic(_bucketFixture.BucketNameGeneric, "HelloMakePublic.txt");
        var text      = webClient.DownloadString(medialink);

        Assert.Equal(File.ReadAllText(_bucketFixture.FilePath), text);
    }
예제 #17
0
    public BucketFixture()
    {
        ProjectId = Environment.GetEnvironmentVariable("GOOGLE_PROJECT_ID");
        if (string.IsNullOrWhiteSpace(ProjectId))
        {
            throw new Exception("You need to set the Environment variable 'GOOGLE_PROJECT_ID' with your Google Cloud Project's project id.");
        }
        // create simple bucket
        CreateBucket(BucketNameGeneric);

        // create regional bucket
        CreateRegionalBucketSample createRegionalBucketSample = new CreateRegionalBucketSample();

        createRegionalBucketSample.CreateRegionalBucket(ProjectId, BucketNameRegional, TestLocation, StorageClasses.Regional);
        SleepAfterBucketCreateUpdateDelete();
        TempBucketNames.Add(BucketNameRegional);

        //upload file to BucketName
        UploadFileSample uploadFileSample = new UploadFileSample();

        uploadFileSample.UploadFile(BucketNameGeneric, FilePath, FileName);

        Collect(FileName);
    }