Get the Status of an Upload to Google Cloud Storage Java

Google Cloud Storage Customer for Java

Coffee idiomatic client for Cloud Storage.

Maven Stability

  • Product Documentation
  • Client Library Documentation

Quickstart

If y'all are using Maven with BOM, add together this to your pom.xml file

<dependencyManagement>   <dependencies>     <dependency>       <groupId>com.google.cloud</groupId>       <artifactId>libraries-bom</artifactId>       <version>25.one.0</version>       <type>pom</type>       <telescopic>import</scope>     </dependency>   </dependencies> </dependencyManagement>  <dependencies>   <dependency>     <groupId>com.google.cloud</groupId>     <artifactId>google-cloud-storage</artifactId>   </dependency>            

If you are using Maven without BOM, add together this to your dependencies:

<dependency>   <groupId>com.google.cloud</groupId>   <artifactId>google-deject-storage</artifactId>   <version>2.6.0</version> </dependency>            

If yous are using Gradle 5.x or later on, add this to your dependencies

implementation platform(                'com.google.cloud:libraries-bom:25.one.0'              )  implementation                              'com.google.cloud:google-cloud-storage'                          

If y'all are using Gradle without BOM, add this to your dependencies

implementation                              'com.google.cloud:google-cloud-storage:two.six.0'                          

If you are using SBT, add this to your dependencies

libraryDependencies              +              =                              "com.google.cloud"                            %                              "google-cloud-storage"                            %                              "2.6.0"                          

Hallmark

Run into the Authentication section in the base of operations directory'southward README.

Authorization

The customer awarding making API calls must be granted potency scopes required for the desired Cloud Storage APIs, and the authenticated principal must take the IAM role(s) required to access GCP resource using the Cloud Storage API calls.

Getting Started

Prerequisites

You volition need a Google Deject Platform Console project with the Cloud Storage API enabled. You will demand to enable billing to apply Google Cloud Storage. Follow these instructions to get your projection ready upwardly. You will also demand to ready the local evolution environment by installing the Google Cloud SDK and running the following commands in command line: gcloud auth login and gcloud config set project [YOUR Projection ID].

Installation and setup

You'll need to obtain the google-cloud-storage library. Encounter the Quickstart department to add together google-deject-storage as a dependency in your code.

About Deject Storage

Deject Storage is a durable and highly available object storage service. Google Cloud Storage is nigh infinitely scalable and guarantees consistency: when a write succeeds, the latest copy of the object will be returned to whatever GET, globally.

See the Deject Storage client library docs to learn how to employ this Cloud Storage Client Library.

Creating an authorized service object

To brand authenticated requests to Google Cloud Storage, y'all must create a service object with credentials. You can then make API calls by calling methods on the Storage service object. The simplest way to cosign is to use Application Default Credentials. These credentials are automatically inferred from your surroundings, so you lot but need the following code to create your service object:

              import              com.google.cloud.storage.Storage;              import              com.google.cloud.storage.StorageOptions;              Storage              storage              =              StorageOptions.getDefaultInstance().getService();

For other authentication options, see the Hallmark page.

Storing data

Stored objects are called "blobs" in google-deject and are organized into containers called "buckets". Blob, a subclass of BlobInfo, adds a layer of service-related functionality over BlobInfo. Similarly, Bucket adds a layer of service-related functionality over BucketInfo. In this code snippet, we will create a new bucket and upload a blob to that bucket.

Add the following imports at the top of your file:

              import              static              java.nio.charset.StandardCharsets.UTF_8;              import              com.google.deject.storage.Blob;              import              com.google.deject.storage.Bucket;              import              com.google.cloud.storage.BucketInfo;

And so add the following code to create a bucket and upload a simple blob.

Important: Bucket names have to be globally unique (among all users of Cloud Storage). If you choose a bucket name that already exists, you lot'll get a helpful error bulletin telling you to choose another name. In the lawmaking below, supersede "my_unique_bucket" with a unique bucket name. See more about naming rules here.

              // Create a saucepan              String              bucketName              =              "my_unique_bucket";              // Change this to something unique              Bucket              bucket              =              storage.create(BucketInfo.of(bucketName));              // Upload a blob to the newly created bucket              BlobId              blobId              =              BlobId.of(bucketName,              "my_blob_name");              BlobInfo              blobInfo              =              BlobInfo.newBuilder(blobId).setContentType("text/plain").build();              Hulk              blob              =              storage.create(blobInfo,              "a simple blob".getBytes(UTF_8));

A complete example for creating a blob can be found at CreateBlob.coffee.

At this point, you will be able to run into your newly created saucepan and blob on the Google Developers Console.

Retrieving data

Now that we have content uploaded to the server, nosotros tin can come across how to read data from the server. Add together the post-obit line to your programme to become dorsum the blob we uploaded.

              BlobId              blobId              =              BlobId.of(bucketName,              "my_blob_name");              byte[]              content              =              storage.readAllBytes(blobId);              Cord              contentString              =              new              Cord(content,              UTF_8);

A complete case for accessing blobs tin can be plant at CreateBlob.java.

Updating data

Another thing we may want to do is update a blob. The following snippet shows how to update a Storage hulk if it exists.

              BlobId              blobId              =              BlobId.of(bucketName,              "my_blob_name");              Hulk              hulk              =              storage.get(blobId);              if              (blob              !=              nada) {              byte[]              prevContent              =              blob.getContent();              System.out.println(new              String(prevContent,              UTF_8));              WritableByteChannel              aqueduct              =              blob.writer();              channel.write(ByteBuffer.wrap("Updated content".getBytes(UTF_8)));              aqueduct.close(); }

The complete source code can be found at UpdateBlob.java.

Listing buckets and contents of buckets

Suppose that you've added more buckets and blobs, and now you desire to run into the names of your buckets and the contents of each one. Add the following code to list all your buckets and all the blobs inside each bucket.

              // Listing all your buckets              System.out.println("My buckets:");              for              (Bucket              bucket              :              storage.listing().iterateAll()) {              Arrangement.out.println(bucket);              // List all blobs in the saucepan              Organization.out.println("Blobs in the bucket:");              for              (Blob              blob              :              bucket.list().iterateAll()) {              Arrangement.out.println(hulk);   } }

Complete source lawmaking

In CreateAndListBucketsAndBlobs.java nosotros put together examples creating and listing buckets and blobs into one programme. The program assumes that you are running on Compute Engine or from your ain desktop. To run the example on App Engine, only move the code from the main method to your application's servlet class and change the print statements to display on your webpage.

Example Applications

  • StorageExample is a uncomplicated command line interface that provides some of Cloud Storage'south functionality. Read more about using the awarding on the StorageExample docs page.
  • Bookshelf - An App Engine application that manages a virtual bookshelf.
    • This app uses google-deject to interface with Cloud Datastore and Cloud Storage. It also uses Cloud SQL, some other Google Deject Platform service.
  • Flexible Environment/Storage example - An app that uploads files to a public Deject Storage bucket on the App Engine Flexible Environment runtime.

Samples

Samples are in the samples/ directory.

Sample Source Code Try it
Native Image Storage Sample source code Open in Cloud Shell
Configure Retries source lawmaking Open in Cloud Shell
Generate Signed Post Policy V4 source code Open in Cloud Shell
Get Service Account source lawmaking Open in Cloud Shell
Quickstart Sample source lawmaking Open in Cloud Shell
Add together Bucket Default Possessor source code Open in Cloud Shell
Add Saucepan Iam Conditional Bounden source code Open in Cloud Shell
Add Bucket Iam Member source code Open in Cloud Shell
Add Bucket Label source code Open in Cloud Shell
Add together Saucepan Owner source code Open in Cloud Shell
Change Default Storage Class source code Open in Cloud Shell
Configure Bucket Cors source code Open in Cloud Shell
Create Saucepan source code Open in Cloud Shell
Create Bucket Dual Region source code Open in Cloud Shell
Create Bucket Pub Sub Notification source code Open in Cloud Shell
Create Bucket With Storage Class And Location source code Open in Cloud Shell
Create Saucepan With Turbo Replication source code Open in Cloud Shell
Delete Bucket source code Open in Cloud Shell
Delete Bucket Pub Sub Notification source code Open in Cloud Shell
Disable Bucket Versioning source code Open in Cloud Shell
Disable Default Event Based Hold source code Open in Cloud Shell
Disable Lifecycle Management source lawmaking Open in Cloud Shell
Disable Requester Pays source lawmaking Open in Cloud Shell
Disable Uniform Bucket Level Admission source lawmaking Open in Cloud Shell
Enable Saucepan Versioning source lawmaking Open in Cloud Shell
Enable Default Event Based Concord source code Open in Cloud Shell
Enable Lifecycle Management source code Open in Cloud Shell
Enable Requester Pays source code Open in Cloud Shell
Enable Uniform Bucket Level Access source code Open in Cloud Shell
Get Bucket Metadata source code Open in Cloud Shell
Become Bucket Rpo source code Open in Cloud Shell
Get Default Issue Based Hold source code Open in Cloud Shell
Get Public Access Prevention source code Open in Cloud Shell
Get Requester Pays Status source lawmaking Open in Cloud Shell
Get Retention Policy source code Open in Cloud Shell
Get Compatible Saucepan Level Access source lawmaking Open in Cloud Shell
List Bucket Iam Members source code Open in Cloud Shell
List Buckets source code Open in Cloud Shell
List Pub Sub Notifications source code Open in Cloud Shell
Lock Retention Policy source code Open in Cloud Shell
Brand Saucepan Public source code Open in Cloud Shell
Print Bucket Acl source code Open in Cloud Shell
Print Bucket Acl Filter By User source code Open in Cloud Shell
Impress Pub Sub Notification source code Open in Cloud Shell
Remove Bucket Cors source code Open in Cloud Shell
Remove Bucket Default Kms Cardinal source code Open in Cloud Shell
Remove Bucket Default Owner source code Open in Cloud Shell
Remove Saucepan Iam Conditional Binding source code Open in Cloud Shell
Remove Bucket Iam Member source code Open in Cloud Shell
Remove Bucket Label source code Open in Cloud Shell
Remove Saucepan Owner source lawmaking Open in Cloud Shell
Remove Memory Policy source code Open in Cloud Shell
Prepare Async Turbo Rpo source lawmaking Open in Cloud Shell
Set Bucket Default Kms Key source code Open in Cloud Shell
Set Bucket Website Info source code Open in Cloud Shell
Set Client Endpoint source lawmaking Open in Cloud Shell
Prepare Default Rpo source code Open in Cloud Shell
Set Public Access Prevention Enforced source code Open in Cloud Shell
Set Public Access Prevention Inherited source code Open in Cloud Shell
Set Retention Policy source code Open in Cloud Shell
Activate Hmac Primal source code Open in Cloud Shell
Create Hmac Fundamental source code Open in Cloud Shell
Conciliate Hmac Key source code Open in Cloud Shell
Delete Hmac Fundamental source code Open in Cloud Shell
Become Hmac Central source lawmaking Open in Cloud Shell
List Hmac Keys source lawmaking Open in Cloud Shell
Add File Owner source code Open in Cloud Shell
Modify Object Csek To Kms source lawmaking Open in Cloud Shell
Alter Object Storage Course source code Open in Cloud Shell
Compose Object source lawmaking Open in Cloud Shell
Copy Object source lawmaking Open in Cloud Shell
Copy Old Version Of Object source code Open in Cloud Shell
Delete Object source code Open in Cloud Shell
Delete Old Version Of Object source lawmaking Open in Cloud Shell
Download Encrypted Object source lawmaking Open in Cloud Shell
Download Object source code Open in Cloud Shell
Download Object Into Memory source code Open in Cloud Shell
Download Public Object source code Open in Cloud Shell
Download Requester Pays Object source code Open in Cloud Shell
Generate Encryption Key source lawmaking Open in Cloud Shell
Generate V4 Get Object Signed Url source code Open in Cloud Shell
Generate V4 Put Object Signed Url source lawmaking Open in Cloud Shell
Become Object Metadata source code Open in Cloud Shell
List Objects source code Open in Cloud Shell
Listing Objects With Old Versions source code Open in Cloud Shell
Listing Objects With Prefix source code Open in Cloud Shell
Make Object Public source code Open in Cloud Shell
Move Object source code Open in Cloud Shell
Print File Acl source code Open in Cloud Shell
Impress File Acl For User source lawmaking Open in Cloud Shell
Release Event Based Agree source code Open in Cloud Shell
Release Temporary Hold source code Open in Cloud Shell
Remove File Owner source code Open in Cloud Shell
Rotate Object Encryption Primal source code Open in Cloud Shell
Set Issue Based Hold source lawmaking Open in Cloud Shell
Set Object Metadata source code Open in Cloud Shell
Set Temporary Hold source lawmaking Open in Cloud Shell
Stream Object Download source code Open in Cloud Shell
Stream Object Upload source code Open in Cloud Shell
Upload Encrypted Object source code Open in Cloud Shell
Upload Kms Encrypted Object source code Open in Cloud Shell
Upload Object source code Open in Cloud Shell
Upload Object From Memory source code Open in Cloud Shell

Troubleshooting

To get help, follow the instructions in the shared Troubleshooting document.

Supported Java Versions

Coffee 8 or above is required for using this client.

Google'due south Java client libraries, Google Cloud Client Libraries and Google Deject API Libraries, follow the Oracle Coffee SE support roadmap (run into the Oracle Coffee SE Product Releases section).

For new development

In general, new feature development occurs with support for the lowest Java LTS version covered by Oracle's Premier Support (which typically lasts v years from initial General Availability). If the minimum required JVM for a given library is inverse, it is accompanied by a semver major release.

Java 11 and (in September 2021) Java 17 are the best choices for new development.

Keeping product systems electric current

Google tests its client libraries with all current LTS versions covered past Oracle's Extended Support (which typically lasts viii years from initial General Availability).

Legacy support

Google'southward client libraries back up legacy versions of Coffee runtimes with long term stable libraries that don't receive feature updates on a best efforts ground equally it may not be possible to backport all patches.

Google provides updates on a best efforts basis to apps that go on to use Java vii, though apps might demand to upgrade to current versions of the library that supports their JVM.

Where to discover specific information

The latest versions and the supported Coffee versions are identified on the private GitHub repository github.com/GoogleAPIs/java-SERVICENAME and on google-cloud-java.

Versioning

This library follows Semantic Versioning, simply does update Storage interface to introduce new methods which can break your implementations if you implement this interface for testing purposes.

Contributing

Contributions to this library are always welcome and highly encouraged.

See CONTRIBUTING for more information how to become started.

Please note that this projection is released with a Correspondent Code of Conduct. Past participating in this project you lot concord to bide by its terms. Run across Code of Conduct for more information.

License

Apache 2.0 - See LICENSE for more than information.

CI Condition

Java Version Status
Java 8 Kokoro CI
Java 8 OSX Kokoro CI
Java 8 Windows Kokoro CI
Java 11 Kokoro CI

Java is a registered trademark of Oracle and/or its affiliates.

daleyyessist.blogspot.com

Source: https://github.com/googleapis/java-storage

0 Response to "Get the Status of an Upload to Google Cloud Storage Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel