Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
import org.threeten.bp.Duration;

public final class ConfigureRetries {
public static void main(String[] args) {
public static void main(String[] args) throws Exception {
String bucketName = "my-bucket";
String blobName = "blob/to/delete";
deleteBlob(bucketName, blobName);
}

static void deleteBlob(String bucketName, String blobName) {
static void deleteBlob(String bucketName, String blobName) throws Exception {
// Customize retry behavior
RetrySettings retrySettings =
StorageOptions.getDefaultRetrySettings().toBuilder()
Expand All @@ -53,14 +53,16 @@ static void deleteBlob(String bucketName, String blobName) {
.build();

// Instantiate a client
Storage storage = alwaysRetryStorageOptions.getService();
try (Storage storage = alwaysRetryStorageOptions.getService()) {

// Delete the blob
BlobId blobId = BlobId.of(bucketName, blobName);
boolean success = storage.delete(blobId);
// Delete the blob
BlobId blobId = BlobId.of(bucketName, blobName);
boolean success = storage.delete(blobId);

System.out.printf(
"Deletion of Blob %s completed %s.%n", blobId, success ? "successfully" : "unsuccessfully");
System.out.printf(
"Deletion of Blob %s completed %s.%n",
blobId, success ? "successfully" : "unsuccessfully");
}
}
}
// [END storage_configure_retries]
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class GenerateSignedPostPolicyV4 {
* Storage.generateSignedPostPolicyV4 for more details.
*/
public static void generateSignedPostPolicyV4(
String projectId, String bucketName, String blobName) {
String projectId, String bucketName, String blobName) throws Exception {
// The ID of your GCP project
// String projectId = "your-project-id";

Expand All @@ -47,37 +47,39 @@ public static void generateSignedPostPolicyV4(
// The name to give the object uploaded to GCS
// String blobName = "your-object-name"

Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
StorageOptions storageOptions = StorageOptions.newBuilder().setProjectId(projectId).build();
try (Storage storage = storageOptions.getService()) {

PostPolicyV4.PostFieldsV4 fields =
PostPolicyV4.PostFieldsV4.newBuilder().setCustomMetadataField("test", "data").build();
PostPolicyV4.PostFieldsV4 fields =
PostPolicyV4.PostFieldsV4.newBuilder().setCustomMetadataField("test", "data").build();

PostPolicyV4 policy =
storage.generateSignedPostPolicyV4(
BlobInfo.newBuilder(bucketName, blobName).build(), 10, TimeUnit.MINUTES, fields);
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");
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());
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]
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@
import com.google.cloud.storage.StorageOptions;

public class GetServiceAccount {
public static void getServiceAccount(String projectId) {
public static void getServiceAccount(String projectId) throws Exception {
// 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());
StorageOptions storageOptions = StorageOptions.newBuilder().setProjectId(projectId).build();
try (Storage storage = storageOptions.getService()) {
ServiceAccount serviceAccount = storage.getServiceAccount(projectId);
System.out.println(
"The GCS service account for project " + projectId + " is: " + serviceAccount.getEmail());
}
}
}
// [END storage_get_service_account]
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ public static void main(String... args) throws Exception {
.build())
.build();
StorageOptions options = StorageOptions.newBuilder().setOpenTelemetry(openTelemetry).build();
Storage storage = options.getService();
System.out.println("Created an instance of storage with OpenTelemetry configured");
try (OpenTelemetrySdk otel = openTelemetry;
Storage storage = options.getService()) {
System.out.println("Created an instance of storage with OpenTelemetry configured");
}
}
}
// [END storage_enable_otel_tracing]
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@
public class QuickstartSample {
public static void main(String... args) throws Exception {
// Instantiates a client
Storage storage = StorageOptions.getDefaultInstance().getService();
try (Storage storage = StorageOptions.getDefaultInstance().getService()) {

// The name for the new bucket
String bucketName = args[0]; // "my-new-bucket";
// The name for the new bucket
String bucketName = args[0]; // "my-new-bucket";

// Creates the new bucket
Bucket bucket = storage.create(BucketInfo.of(bucketName));
// Creates the new bucket
Bucket bucket = storage.create(BucketInfo.of(bucketName));

System.out.printf("Bucket %s created.%n", bucket.getName());
System.out.printf("Bucket %s created.%n", bucket.getName());
}
}
}
// [END storage_quickstart]
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,21 @@

public class AddBucketDefaultOwner {

public static void addBucketDefaultOwner(String bucketName, String userEmail) {
public static void addBucketDefaultOwner(String bucketName, String userEmail) throws Exception {

// The ID of your GCS bucket
// String bucketName = "your-unique-bucket-name";

// The email of the user you wish to add as a default owner
// String userEmail = "[email protected]"

Storage storage = StorageOptions.newBuilder().build().getService();
Bucket bucket = storage.get(bucketName);
Acl newDefaultOwner = Acl.of(new User(userEmail), Role.OWNER);
try (Storage storage = StorageOptions.newBuilder().build().getService()) {
Bucket bucket = storage.get(bucketName);
Acl newDefaultOwner = Acl.of(new User(userEmail), Role.OWNER);

bucket.createDefaultAcl(newDefaultOwner);
System.out.println("Added user " + userEmail + " as an owner on " + bucketName);
bucket.createDefaultAcl(newDefaultOwner);
System.out.println("Added user " + userEmail + " as an owner on " + bucketName);
}
}
}
// [END storage_add_bucket_default_owner]
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@

public class AddBucketIamConditionalBinding {
/** Example of adding a conditional binding to the Bucket-level IAM */
public static void addBucketIamConditionalBinding(String projectId, String bucketName) {
public static void addBucketIamConditionalBinding(String projectId, String bucketName)
throws Exception {
// The ID of your GCP project
// String projectId = "your-project-id";

Expand All @@ -38,44 +39,46 @@ public static void addBucketIamConditionalBinding(String projectId, String bucke

// For more information please read:
// https://cloud.google.com/storage/docs/access-control/iam
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
try (Storage storage =
StorageOptions.newBuilder().setProjectId(projectId).build().getService()) {

Policy originalPolicy =
storage.getIamPolicy(bucketName, Storage.BucketSourceOption.requestedPolicyVersion(3));
Policy originalPolicy =
storage.getIamPolicy(bucketName, Storage.BucketSourceOption.requestedPolicyVersion(3));

String role = "roles/storage.objectViewer";
String member = "group:[email protected]";
String role = "roles/storage.objectViewer";
String member = "group:[email protected]";

// 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);
// 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());
// 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());
// 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);
// Update policy with new conditional binding
Policy.Builder updatedPolicyBuilder = originalPolicy.toBuilder();
updatedPolicyBuilder.setBindings(bindings).setVersion(3);

storage.setIamPolicy(bucketName, updatedPolicyBuilder.build());
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);
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]
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

public class AddBucketIamMember {
/** Example of adding a member to the Bucket-level IAM */
public static void addBucketIamMember(String projectId, String bucketName) {
public static void addBucketIamMember(String projectId, String bucketName) throws Exception {
// The ID of your GCP project
// String projectId = "your-project-id";

Expand All @@ -37,28 +37,31 @@ public static void addBucketIamMember(String projectId, String bucketName) {

// For more information please read:
// https://cloud.google.com/storage/docs/access-control/iam
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
try (Storage storage =
StorageOptions.newBuilder().setProjectId(projectId).build().getService()) {

Policy originalPolicy =
storage.getIamPolicy(bucketName, Storage.BucketSourceOption.requestedPolicyVersion(3));
Policy originalPolicy =
storage.getIamPolicy(bucketName, Storage.BucketSourceOption.requestedPolicyVersion(3));

String role = "roles/storage.objectViewer";
String member = "group:[email protected]";
String role = "roles/storage.objectViewer";
String member = "group:[email protected]";

// getBindingsList() returns an ImmutableList and copying over to an ArrayList so it's mutable.
List<Binding> bindings = new ArrayList(originalPolicy.getBindingsList());
// getBindingsList() returns an ImmutableList and copying over to an ArrayList so it's
// mutable.
List<Binding> bindings = new ArrayList(originalPolicy.getBindingsList());

// Create a new binding using role and member
Binding.Builder newMemberBindingBuilder = Binding.newBuilder();
newMemberBindingBuilder.setRole(role).setMembers(Arrays.asList(member));
bindings.add(newMemberBindingBuilder.build());
// Create a new binding using role and member
Binding.Builder newMemberBindingBuilder = Binding.newBuilder();
newMemberBindingBuilder.setRole(role).setMembers(Arrays.asList(member));
bindings.add(newMemberBindingBuilder.build());

// Update policy to add member
Policy.Builder updatedPolicyBuilder = originalPolicy.toBuilder();
updatedPolicyBuilder.setBindings(bindings).setVersion(3);
Policy updatedPolicy = storage.setIamPolicy(bucketName, updatedPolicyBuilder.build());
// Update policy to add member
Policy.Builder updatedPolicyBuilder = originalPolicy.toBuilder();
updatedPolicyBuilder.setBindings(bindings).setVersion(3);
Policy updatedPolicy = storage.setIamPolicy(bucketName, updatedPolicyBuilder.build());

System.out.printf("Added %s with role %s to %s\n", member, role, bucketName);
System.out.printf("Added %s with role %s to %s\n", member, role, bucketName);
}
}
}
// [END storage_add_bucket_iam_member]
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

public class AddBucketLabel {
public static void addBucketLabel(
String projectId, String bucketName, String labelKey, String labelValue) {
String projectId, String bucketName, String labelKey, String labelValue) throws Exception {
// The ID of your GCP project
// String projectId = "your-project-id";

Expand All @@ -42,16 +42,24 @@ public static void addBucketLabel(
Map<String, String> newLabels = new HashMap<>();
newLabels.put(labelKey, labelValue);

Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
Bucket bucket = storage.get(bucketName);
Map<String, String> labels = bucket.getLabels();
if (labels != null) {
newLabels.putAll(labels);
}
bucket.toBuilder().setLabels(newLabels).build().update();
try (Storage storage =
StorageOptions.newBuilder().setProjectId(projectId).build().getService()) {
Bucket bucket = storage.get(bucketName);
Map<String, String> labels = bucket.getLabels();
if (labels != null) {
newLabels.putAll(labels);
}
bucket.toBuilder().setLabels(newLabels).build().update();

System.out.println(
"Added label " + labelKey + " with value " + labelValue + " to bucket " + bucketName + ".");
System.out.println(
"Added label "
+ labelKey
+ " with value "
+ labelValue
+ " to bucket "
+ bucketName
+ ".");
}
}
}
// [END storage_add_bucket_label]
Loading
Loading