forked from GoogleCloudPlatform/dotnet-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
samples(storage batch operations): add samples and tests for storage batch operations #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mahendra-google
wants to merge
19
commits into
main
Choose a base branch
from
sbo
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
338ea66
samples(Storage Batch Operations): Add samples and tests for storage…
mahendra-google 17152e6
fix(Storage Batch Operations): Modify retry logic
mahendra-google 5e78e8f
Update StorageBatchOperations.sln
mahendra-google ff806bf
Refactor CancelBatchJobTest for better error handling
mahendra-google 79891d9
Remove testutil project reference from tests
mahendra-google 594ca93
Fix typo in README regarding .NET requirement
mahendra-google ad1a08a
refactor(Storage Batch Operations): Remove unused variables and unuse…
mahendra-google 496d105
Update CreateBatchJob.cs
mahendra-google 1ec77c1
refactor(Storage Batch Operations): Dispose memory stream
mahendra-google 54441e3
refactor(Storage Batch Operations): Convert create job test from fact…
mahendra-google cc65ffd
fix(Storage Batch Operations): Add missing environment variables in f…
mahendra-google 626eb8f
refactor(Storage Batch Operations): Remove unused KMS fields in Creat…
mahendra-google 217f1dd
refactor(Storage Batch Operations): Update CancelBatchJobTest to impr…
mahendra-google 2a3d918
refactor(Storage Batch Operations): Replace Thread.Sleep with loop in…
mahendra-google 2fe069a
style(Storage Batch Operations): Fix formatting in CancelBatchJobTest.cs
mahendra-google c38ef3e
tests(Storage Batch Operations): Stabilize cancelBatchJobTest by incr…
mahendra-google f1ddbbf
chore(Storage Batch Operations): Update dependencies
mahendra-google f7ba2e6
refactor(Storage Batch Operations): Addressing Review Feedback
mahendra-google e583cdf
refactor(Storage Batch Operations): Improve console output messages a…
mahendra-google File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # Cloud Storage Batch Operations Sample | ||
|
|
||
| These samples demonstrate how to interact with the [Google Cloud Storage Batch Operations API][Storage Batch Operations] from C# and | ||
| the .NET client libraries to call the Storage Batch Operations API. | ||
|
|
||
| The samples require [.NET 8][.NET].That means using | ||
| [Visual Studio 2022](https://www.visualstudio.com/), or the command line. | ||
|
|
||
| ## Setup | ||
|
|
||
| 1. Set up a [.NET development environment](https://cloud.google.com/dotnet/docs/setup). | ||
|
|
||
| 2. Enable APIs for your project. | ||
| [Click here][enable-api] | ||
| to visit Cloud Platform Console and enable the Google Cloud Storage Batch Operations API. | ||
|
|
||
| ## Contributing changes | ||
|
|
||
| * See [CONTRIBUTING.md](../../CONTRIBUTING.md) | ||
|
|
||
| ## Licensing | ||
|
|
||
| * See [LICENSE](../../LICENSE) | ||
|
|
||
| ## Testing | ||
|
|
||
| * See [TESTING.md](../../TESTING.md) | ||
|
|
||
| [Storage Batch Operations]: https://cloud.google.com/storage/docs/batch-operations/overview | ||
| [enable-api]: https://console.cloud.google.com/flows/enableapi?apiid=storagebatchoperations_api&showconfirmation=true | ||
| [.NET]: https://dotnet.microsoft.com/en-us/download | ||
141 changes: 141 additions & 0 deletions
141
storagebatchoperations/api/StorageBatchOperations.Samples.Tests/CancelBatchJobTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"). | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // https://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| using Google.Api.Gax.ResourceNames; | ||
| using Google.Cloud.StorageBatchOperations.V1; | ||
| using Google.LongRunning; | ||
| using System; | ||
| using System.IO; | ||
| using System.Text; | ||
| using System.Threading; | ||
| using Xunit; | ||
|
|
||
| [Collection(nameof(StorageFixture))] | ||
| public class CancelBatchJobTest | ||
| { | ||
| private readonly StorageFixture _fixture; | ||
| private readonly BucketList.Types.Bucket _bucket; | ||
| private readonly BucketList _bucketList = new(); | ||
| private readonly PrefixList _prefixListObject = new(); | ||
|
|
||
| public CancelBatchJobTest(StorageFixture fixture) | ||
| { | ||
| int i = 20; | ||
| _fixture = fixture; | ||
| var bucketName = _fixture.GenerateBucketName(); | ||
| _fixture.CreateBucket(bucketName, multiVersion: false, softDelete: false, registerForDeletion: true); | ||
| // Uploading objects to the bucket. | ||
| while (i >= 0) | ||
| { | ||
| var objectName = _fixture.GenerateGuid(); | ||
| var objectContent = _fixture.GenerateGuid(); | ||
| byte[] byteObjectContent = Encoding.UTF8.GetBytes(objectContent); | ||
| MemoryStream streamObjectContent = new MemoryStream(byteObjectContent); | ||
| _fixture.Client.UploadObject(bucketName, objectName, "application/text", streamObjectContent); | ||
| i--; | ||
| } | ||
|
|
||
| _bucket = new BucketList.Types.Bucket | ||
| { | ||
| Bucket_ = bucketName, | ||
| // The prefix list is used to specify the objects to be deleted. To match all objects, use an empty list. | ||
| PrefixList = _prefixListObject | ||
| }; | ||
| // Adding the bucket to the bucket list. | ||
| _bucketList.Buckets.Insert(0, _bucket); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TestCancelBatchJob() | ||
| { | ||
| CancelBatchJobSample cancelBatchJob = new CancelBatchJobSample(); | ||
| ListBatchJobsSample listBatchJobs = new ListBatchJobsSample(); | ||
| GetBatchJobSample getBatchJob = new GetBatchJobSample(); | ||
|
|
||
| string filter = "state:canceled"; | ||
| int pageSize = 10; | ||
| string orderBy = "create_time"; | ||
|
|
||
| var jobId = _fixture.GenerateGuid(); | ||
| var createdJobName = CreateBatchJob(_fixture.LocationName, _bucketList, jobId); | ||
| Assert.NotNull(createdJobName); | ||
|
|
||
| // Poll until the job is in a state that can be cancelled (Running) | ||
| // This prevents attempting to cancel a job that hasn't started or has already finished. | ||
| bool isCancellable = false; | ||
| for (int attempt = 0; attempt < 10; attempt++) | ||
| { | ||
| Job currentJob = getBatchJob.GetBatchJob(createdJobName); | ||
| if (currentJob.State == Job.Types.State.Running) | ||
| { | ||
| isCancellable = true; | ||
| break; | ||
| } | ||
| if (currentJob.State == Job.Types.State.Succeeded) break; | ||
| } | ||
|
|
||
| Assert.True(isCancellable, "Job did not reach a Running state in time to be cancelled."); | ||
| cancelBatchJob.CancelBatchJob(createdJobName); | ||
|
|
||
| Job finalJob = null; | ||
| for (int attempt = 0; attempt < 1000; attempt++) | ||
| { | ||
| finalJob = getBatchJob.GetBatchJob(createdJobName); | ||
| if (finalJob.State == Job.Types.State.Canceled) break; | ||
| } | ||
| Assert.Equal(createdJobName, finalJob.Name.ToString()); | ||
| Assert.Equal(Job.Types.State.Canceled, finalJob.State); | ||
|
|
||
| var batchJobs = listBatchJobs.ListBatchJobs(_fixture.LocationName, filter, pageSize, orderBy); | ||
| Assert.Contains(batchJobs, j => j.Name == createdJobName); | ||
| _fixture.DeleteBatchJob(createdJobName); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Create a batch job with the specified transformation case and bucket list. | ||
| /// </summary> | ||
| public static string CreateBatchJob(LocationName locationName, | ||
| BucketList bucketList, | ||
| string jobId = "12345678910") | ||
| { | ||
| StorageBatchOperationsClient storageBatchClient = StorageBatchOperationsClient.Create(); | ||
|
|
||
| // Creates a batch job with the specified bucket list and delete object settings. | ||
| CreateJobRequest request = new CreateJobRequest | ||
| { | ||
| ParentAsLocationName = locationName, | ||
| JobId = jobId, | ||
| Job = new Job | ||
| { | ||
| BucketList = bucketList, | ||
| DeleteObject = new DeleteObject { PermanentObjectDeletionEnabled = true } | ||
| }, | ||
| RequestId = jobId, | ||
| }; | ||
|
|
||
| Operation<Job, OperationMetadata> response = storageBatchClient.CreateJob(request); | ||
| string jobName = String.Empty; | ||
| for (int attempt = 0; attempt < 10; attempt++) | ||
| { | ||
| Operation<Job, OperationMetadata> retrievedResponse = storageBatchClient.PollOnceCreateJob(response.Name); | ||
| jobName = retrievedResponse.Metadata?.Job?.Name; | ||
|
|
||
| if (!string.IsNullOrEmpty(jobName)) | ||
| { | ||
| break; | ||
| } | ||
| } | ||
| return jobName; | ||
| } | ||
| } |
80 changes: 80 additions & 0 deletions
80
...operations/api/StorageBatchOperations.Samples.Tests/CreateBatchJobWithDeleteObjectTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"). | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // https://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| using Google.Cloud.StorageBatchOperations.V1; | ||
| using System; | ||
| using System.IO; | ||
| using System.Text; | ||
| using Xunit; | ||
|
|
||
| [Collection(nameof(StorageFixture))] | ||
| public class CreateBatchJobWithDeleteObjectTest | ||
| { | ||
| private readonly StorageFixture _fixture; | ||
| private readonly BucketList.Types.Bucket _bucket = new(); | ||
| private readonly BucketList _bucketList = new(); | ||
| private readonly PrefixList _prefixListObject = new(); | ||
|
|
||
| public CreateBatchJobWithDeleteObjectTest(StorageFixture fixture) | ||
| { | ||
| _fixture = fixture; | ||
| var bucketName = _fixture.GenerateBucketName(); | ||
| _fixture.CreateBucket(bucketName, multiVersion: false, softDelete: false, registerForDeletion: true); | ||
|
|
||
| var manifestBucketName = _fixture.GenerateBucketName(); | ||
| _fixture.CreateBucket(manifestBucketName, multiVersion: false, softDelete: false, registerForDeletion: true); | ||
|
|
||
| var objectName = _fixture.GenerateGuid(); | ||
| var manifestObjectName = _fixture.GenerateGuid(); | ||
| var objectContent = _fixture.GenerateGuid(); | ||
| var manifestObjectContent = $"bucket,name,generation{Environment.NewLine}{bucketName},{objectName}"; | ||
|
|
||
| byte[] byteObjectContent = Encoding.UTF8.GetBytes(objectContent); | ||
| using MemoryStream streamObjectContent = new MemoryStream(byteObjectContent); | ||
| // Uploading an object to the bucket | ||
| _fixture.Client.UploadObject(bucketName, objectName, "application/text", streamObjectContent); | ||
|
|
||
| byte[] byteManifestObjectContent = Encoding.UTF8.GetBytes(manifestObjectContent); | ||
| // Uploading a manifest object to the manifest bucket | ||
| using MemoryStream streamManifestObjectContent = new MemoryStream(byteManifestObjectContent); | ||
| _fixture.Client.UploadObject(manifestBucketName, $"{manifestObjectName}.csv", "text/csv", streamManifestObjectContent); | ||
| _bucket = new BucketList.Types.Bucket | ||
| { | ||
| Bucket_ = bucketName, | ||
| // The prefix list is used to specify the objects to be transformed. To match all objects, use an empty list. | ||
| PrefixList = _prefixListObject, | ||
| // Manifest location contains csv file having list of objects to be transformed" | ||
| Manifest = new Manifest { ManifestLocation = $"gs://{manifestBucketName}/{manifestObjectName}.csv" } | ||
| }; | ||
| // Adding the bucket to the bucket list. | ||
| _bucketList.Buckets.Insert(0, _bucket); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TestCreateBatchJobWithDeleteObject() | ||
| { | ||
| CreateBatchJobWithDeleteObjectSample createJob = new CreateBatchJobWithDeleteObjectSample(); | ||
| var jobId = _fixture.GenerateGuid(); | ||
|
|
||
| var createdBatchJob = createJob.CreateBatchJobWithDeleteObject(_fixture.LocationName, _bucketList, jobId); | ||
| Assert.Equal(createdBatchJob.BucketList, _bucketList); | ||
| Assert.Equal("DeleteObject", createdBatchJob.TransformationCase.ToString()); | ||
| Assert.Equal(createdBatchJob.SourceCase.ToString(), _bucketList.GetType().Name); | ||
| Assert.NotNull(createdBatchJob.Name); | ||
| Assert.NotNull(createdBatchJob.JobName); | ||
| Assert.NotNull(createdBatchJob.CreateTime); | ||
| Assert.NotNull(createdBatchJob.CompleteTime); | ||
| _fixture.DeleteBatchJob(createdBatchJob.Name); | ||
| } | ||
| } |
80 changes: 80 additions & 0 deletions
80
...hoperations/api/StorageBatchOperations.Samples.Tests/CreateBatchJobWithPutMetaDataTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"). | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // https://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| using Google.Cloud.StorageBatchOperations.V1; | ||
| using System; | ||
| using System.IO; | ||
| using System.Text; | ||
| using Xunit; | ||
|
|
||
| [Collection(nameof(StorageFixture))] | ||
| public class CreateBatchJobWithPutMetaDataTest | ||
| { | ||
| private readonly StorageFixture _fixture; | ||
| private readonly BucketList.Types.Bucket _bucket = new(); | ||
| private readonly BucketList _bucketList = new(); | ||
| private readonly PrefixList _prefixListObject = new(); | ||
|
|
||
| public CreateBatchJobWithPutMetaDataTest(StorageFixture fixture) | ||
| { | ||
| _fixture = fixture; | ||
| var bucketName = _fixture.GenerateBucketName(); | ||
| _fixture.CreateBucket(bucketName, multiVersion: false, softDelete: false, registerForDeletion: true); | ||
|
|
||
| var manifestBucketName = _fixture.GenerateBucketName(); | ||
| _fixture.CreateBucket(manifestBucketName, multiVersion: false, softDelete: false, registerForDeletion: true); | ||
|
|
||
| var objectName = _fixture.GenerateGuid(); | ||
| var manifestObjectName = _fixture.GenerateGuid(); | ||
| var objectContent = _fixture.GenerateGuid(); | ||
| var manifestObjectContent = $"bucket,name,generation{Environment.NewLine}{bucketName},{objectName}"; | ||
|
|
||
| byte[] byteObjectContent = Encoding.UTF8.GetBytes(objectContent); | ||
| using MemoryStream streamObjectContent = new MemoryStream(byteObjectContent); | ||
| // Uploading an object to the bucket | ||
| _fixture.Client.UploadObject(bucketName, objectName, "application/text", streamObjectContent); | ||
|
|
||
| byte[] byteManifestObjectContent = Encoding.UTF8.GetBytes(manifestObjectContent); | ||
| // Uploading a manifest object to the manifest bucket | ||
| using MemoryStream streamManifestObjectContent = new MemoryStream(byteManifestObjectContent); | ||
| _fixture.Client.UploadObject(manifestBucketName, $"{manifestObjectName}.csv", "text/csv", streamManifestObjectContent); | ||
| _bucket = new BucketList.Types.Bucket | ||
| { | ||
| Bucket_ = bucketName, | ||
| // The prefix list is used to specify the objects to be transformed. To match all objects, use an empty list. | ||
| PrefixList = _prefixListObject, | ||
| // Manifest location contains csv file having list of objects to be transformed" | ||
| Manifest = new Manifest { ManifestLocation = $"gs://{manifestBucketName}/{manifestObjectName}.csv" } | ||
| }; | ||
| // Adding the bucket to the bucket list. | ||
| _bucketList.Buckets.Insert(0, _bucket); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TestCreateBatchJobWithPutMetaData() | ||
| { | ||
| CreateBatchJobWithPutMetadataSample createJob = new CreateBatchJobWithPutMetadataSample(); | ||
| var jobId = _fixture.GenerateGuid(); | ||
|
|
||
| var createdBatchJob = createJob.CreateBatchJobWithPutMetadata(_fixture.LocationName, _bucketList, jobId); | ||
| Assert.Equal(createdBatchJob.BucketList, _bucketList); | ||
| Assert.Equal("PutMetadata", createdBatchJob.TransformationCase.ToString()); | ||
| Assert.Equal(createdBatchJob.SourceCase.ToString(), _bucketList.GetType().Name); | ||
| Assert.NotNull(createdBatchJob.Name); | ||
| Assert.NotNull(createdBatchJob.JobName); | ||
| Assert.NotNull(createdBatchJob.CreateTime); | ||
| Assert.NotNull(createdBatchJob.CompleteTime); | ||
| _fixture.DeleteBatchJob(createdBatchJob.Name); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.