-
Notifications
You must be signed in to change notification settings - Fork 68
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
docs: Copy all storage samples from java-docs-samples #1258
Merged
Merged
Changes from all commits
Commits
Show all changes
79 commits
Select commit
Hold shift + click to select a range
94947d9
Copy all storage samples from java-docs-samples
JesseLovelace 144edbe
🦉 Updates from OwlBot
gcf-owl-bot[bot] d0c8ddc
use gcloud-devel testing account
JesseLovelace cbddfac
Merge branch 'movesamples' of github.com:googleapis/java-storage into…
JesseLovelace 08619fb
🦉 Updates from OwlBot
gcf-owl-bot[bot] 121249c
fix cleanup config
JesseLovelace 9e565e1
Merge branch 'movesamples' of github.com:googleapis/java-storage into…
JesseLovelace 05b8c05
🦉 Updates from OwlBot
gcf-owl-bot[bot] 750be2c
fix cfg
JesseLovelace 4d7b06b
Merge branch 'movesamples' of github.com:googleapis/java-storage into…
JesseLovelace f11841f
🦉 Updates from OwlBot
gcf-owl-bot[bot] 0dcf62b
fix cfg and owlbot
JesseLovelace 5c10df3
🦉 Updates from OwlBot
gcf-owl-bot[bot] 5f537ac
fix csek and request pays tests
JesseLovelace 2648875
Merge branch 'movesamples' of github.com:googleapis/java-storage into…
JesseLovelace 0ae066d
fix cfg and owlbot ignore
JesseLovelace dc6d5ff
Merge branch 'main' into movesamples
JesseLovelace 8522d61
🦉 Updates from OwlBot
gcf-owl-bot[bot] 89bf36a
fix requester pays samples
JesseLovelace 926791a
Merge branch 'movesamples' of github.com:googleapis/java-storage into…
JesseLovelace 15a9735
🦉 Updates from OwlBot
gcf-owl-bot[bot] b7481e3
fix download requester pays sample
JesseLovelace afd9a9e
Merge branch 'movesamples' of github.com:googleapis/java-storage into…
JesseLovelace 95b8e37
🦉 Updates from OwlBot
gcf-owl-bot[bot] 2c32586
fix system out capture
JesseLovelace 054e962
Merge branch 'movesamples' of github.com:googleapis/java-storage into…
JesseLovelace a65926a
🦉 Updates from OwlBot
gcf-owl-bot[bot] 9120eb4
fix disable requester pays sample
JesseLovelace 2849557
Merge branch 'movesamples' of github.com:googleapis/java-storage into…
JesseLovelace 7b38d19
🦉 Updates from OwlBot
gcf-owl-bot[bot] 6b94c36
fix label and kms tests
JesseLovelace b0fc440
Merge branch 'movesamples' of github.com:googleapis/java-storage into…
JesseLovelace 08701db
add remaining samples and tests
JesseLovelace 9d346e7
Merge branch 'main' into movesamples
JesseLovelace 2e9cec7
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 2287376
fix hmac cleanup
JesseLovelace 2ef06db
Merge branch 'movesamples' of github.com:googleapis/java-storage into…
JesseLovelace 0bad428
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] dd10893
fix snippet output capture
JesseLovelace cc942e4
Merge branch 'movesamples' of github.com:googleapis/java-storage into…
JesseLovelace 75fc930
fix standardOut reset
JesseLovelace e1c2e58
Fix standard out capture
JesseLovelace 917ba15
increase retry backoff multiplier
JesseLovelace df7ab1c
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 5eacbe9
fix UBLA sample and HMAC service account
JesseLovelace d044b70
Merge branch 'movesamples' of github.com:googleapis/java-storage into…
JesseLovelace 09095cc
fix copyright header formatting
JesseLovelace bd6797e
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 1c26ba9
fix checkstyle
JesseLovelace 7728349
Merge branch 'movesamples' of github.com:googleapis/java-storage into…
JesseLovelace ce8ba9f
add sleeps for hmac tests
JesseLovelace 29bd318
use java docs samples testing account
JesseLovelace ce857ee
Merge branch 'main' into movesamples
JesseLovelace 7ddb4c6
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] a396dc8
Merge branch 'main' into movesamples
JesseLovelace d39aff9
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 4939c36
fix snapshot dependency
JesseLovelace f1d9a09
fix kms resource
JesseLovelace a3bfbb8
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] ee37dc5
fix hmac list test
JesseLovelace dd933da
Merge branch 'movesamples' of github.com:googleapis/java-storage into…
JesseLovelace eb85d91
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 4f5df3d
add sleeps to fix 429 errors
JesseLovelace 5699e6f
Merge branch 'movesamples' of github.com:googleapis/java-storage into…
JesseLovelace 4098e6d
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 8f194b4
fix hmac service account
JesseLovelace 992e5ba
Merge branch 'movesamples' of github.com:googleapis/java-storage into…
JesseLovelace 4b8a352
Fix hmac service account
JesseLovelace 983c953
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 793c562
fix standard out
JesseLovelace bf5fc12
Merge branch 'movesamples' of github.com:googleapis/java-storage into…
JesseLovelace 8810c2a
fix bucket retention test
JesseLovelace abe158b
fix checkstyle
JesseLovelace 366fd81
Add comment about setout
JesseLovelace 0431002
Merge branch 'main' into movesamples
JesseLovelace 2e5730b
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 41ba1b9
fix acl sample comments
JesseLovelace 132cd35
Merge branch 'movesamples' of github.com:googleapis/java-storage into…
JesseLovelace a202432
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
83 changes: 83 additions & 0 deletions
83
samples/snippets/src/main/java/com/example/storage/GenerateSignedPostPolicyV4.java
This file contains 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,83 @@ | ||
/* | ||
* Copyright 2020 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 | ||
* | ||
* http://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. | ||
*/ | ||
|
||
package com.example.storage; | ||
|
||
// [START storage_generate_signed_post_policy_v4] | ||
|
||
import com.google.cloud.storage.BlobInfo; | ||
import com.google.cloud.storage.PostPolicyV4; | ||
import com.google.cloud.storage.Storage; | ||
import com.google.cloud.storage.StorageOptions; | ||
import java.util.Map; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class GenerateSignedPostPolicyV4 { | ||
/** | ||
* Generating a signed POST policy requires Credentials which implement ServiceAccountSigner. | ||
* These can be set explicitly using the Storage.PostPolicyV4Option.signWith(ServiceAccountSigner) | ||
* option. If you don't, you could also pass a service account signer to StorageOptions, i.e. | ||
* StorageOptions().newBuilder().setCredentials(ServiceAccountSignerCredentials). In this example, | ||
* neither of these options are used, which means the following code only works when the | ||
* credentials are defined via the environment variable GOOGLE_APPLICATION_CREDENTIALS, and those | ||
* credentials are authorized to sign a policy. See the documentation for | ||
* Storage.generateSignedPostPolicyV4 for more details. | ||
*/ | ||
public static void generateSignedPostPolicyV4( | ||
String projectId, String bucketName, String blobName) { | ||
// The ID of your GCP project | ||
// String projectId = "your-project-id"; | ||
|
||
// The ID of the GCS bucket to upload to | ||
// String bucketName = "your-bucket-name" | ||
|
||
// The name to give the object uploaded to GCS | ||
// String blobName = "your-object-name" | ||
|
||
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService(); | ||
|
||
PostPolicyV4.PostFieldsV4 fields = | ||
PostPolicyV4.PostFieldsV4.newBuilder().setCustomMetadataField("test", "data").build(); | ||
|
||
PostPolicyV4 policy = | ||
storage.generateSignedPostPolicyV4( | ||
BlobInfo.newBuilder(bucketName, blobName).build(), 10, TimeUnit.MINUTES, fields); | ||
|
||
StringBuilder htmlForm = | ||
new StringBuilder( | ||
"<form action='" | ||
+ policy.getUrl() | ||
+ "' method='POST' enctype='multipart/form-data'>\n"); | ||
for (Map.Entry<String, String> entry : policy.getFields().entrySet()) { | ||
htmlForm.append( | ||
" <input name='" | ||
+ entry.getKey() | ||
+ "' value='" | ||
+ entry.getValue() | ||
+ "' type='hidden' />\n"); | ||
} | ||
htmlForm.append(" <input type='file' name='file'/><br />\n"); | ||
htmlForm.append(" <input type='submit' value='Upload File'/><br />\n"); | ||
htmlForm.append("</form>\n"); | ||
|
||
System.out.println( | ||
"You can use the following HTML form to upload an object to bucket " | ||
+ bucketName | ||
+ " for the next ten minutes:"); | ||
System.out.println(htmlForm.toString()); | ||
} | ||
} | ||
// [END storage_generate_signed_post_policy_v4] |
35 changes: 35 additions & 0 deletions
35
samples/snippets/src/main/java/com/example/storage/GetServiceAccount.java
This file contains 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,35 @@ | ||
/* | ||
* Copyright 2020 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 | ||
* | ||
* http://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. | ||
*/ | ||
|
||
package com.example.storage; | ||
|
||
// [START storage_get_service_account] | ||
import com.google.cloud.storage.ServiceAccount; | ||
import com.google.cloud.storage.Storage; | ||
import com.google.cloud.storage.StorageOptions; | ||
|
||
public class GetServiceAccount { | ||
public static void getServiceAccount(String projectId) { | ||
// The ID of your GCP project | ||
// String projectId = "your-project-id"; | ||
|
||
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService(); | ||
ServiceAccount serviceAccount = storage.getServiceAccount(projectId); | ||
System.out.println( | ||
"The GCS service account for project " + projectId + " is: " + serviceAccount.getEmail()); | ||
} | ||
} | ||
// [END storage_get_service_account] |
This file contains 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
81 changes: 81 additions & 0 deletions
81
...les/snippets/src/main/java/com/example/storage/bucket/AddBucketIamConditionalBinding.java
This file contains 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,81 @@ | ||
/* | ||
* Copyright 2022 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 | ||
* | ||
* http://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. | ||
*/ | ||
|
||
package com.example.storage.bucket; | ||
|
||
// [START storage_add_bucket_conditional_iam_binding] | ||
|
||
import com.google.cloud.Binding; | ||
import com.google.cloud.Condition; | ||
import com.google.cloud.Policy; | ||
import com.google.cloud.storage.Storage; | ||
import com.google.cloud.storage.StorageOptions; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class AddBucketIamConditionalBinding { | ||
/** Example of adding a conditional binding to the Bucket-level IAM */ | ||
public static void addBucketIamConditionalBinding(String projectId, String bucketName) { | ||
// The ID of your GCP project | ||
// String projectId = "your-project-id"; | ||
|
||
// The ID of your GCS bucket | ||
// String bucketName = "your-unique-bucket-name"; | ||
|
||
// For more information please read: | ||
// https://cloud.google.com/storage/docs/access-control/iam | ||
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService(); | ||
|
||
Policy originalPolicy = | ||
storage.getIamPolicy(bucketName, Storage.BucketSourceOption.requestedPolicyVersion(3)); | ||
|
||
String role = "roles/storage.objectViewer"; | ||
String member = "group:example@google.com"; | ||
|
||
// Create a condition | ||
String conditionTitle = "Title"; | ||
String conditionDescription = "Description"; | ||
String conditionExpression = | ||
"resource.name.startsWith(\"projects/_/buckets/bucket-name/objects/prefix-a-\")"; | ||
Condition.Builder conditionBuilder = Condition.newBuilder(); | ||
conditionBuilder.setTitle(conditionTitle); | ||
conditionBuilder.setDescription(conditionDescription); | ||
conditionBuilder.setExpression(conditionExpression); | ||
|
||
// getBindingsList() returns an ImmutableList, we copy over to an ArrayList so it's mutable | ||
List<Binding> bindings = new ArrayList(originalPolicy.getBindingsList()); | ||
|
||
// Add condition to a binding | ||
Binding.Builder newBindingBuilder = | ||
Binding.newBuilder() | ||
.setRole(role) | ||
.setMembers(Arrays.asList(member)) | ||
.setCondition(conditionBuilder.build()); | ||
bindings.add(newBindingBuilder.build()); | ||
|
||
// Update policy with new conditional binding | ||
Policy.Builder updatedPolicyBuilder = originalPolicy.toBuilder(); | ||
updatedPolicyBuilder.setBindings(bindings).setVersion(3); | ||
|
||
storage.setIamPolicy(bucketName, updatedPolicyBuilder.build()); | ||
|
||
System.out.printf( | ||
"Added %s with role %s to %s with condition %s %s %s\n", | ||
member, role, bucketName, conditionTitle, conditionDescription, conditionExpression); | ||
} | ||
} | ||
// [END storage_add_bucket_conditional_iam_binding] |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
was this intentional?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
owlbot did this, i didn't touch that file at all. it's in one of the "updates from owlbot" commits
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think it's because of a mistake i made with the owlbot.py file at one point, i can fix it