Get the Status of an Upload to Google Cloud Storage Java
Google Cloud Storage Customer for Java
Coffee idiomatic client for Cloud Storage.
- 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 theStorageExample
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.
- This app uses
-
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 | |
Configure Retries | source lawmaking | |
Generate Signed Post Policy V4 | source code | |
Get Service Account | source lawmaking | |
Quickstart Sample | source lawmaking | |
Add together Bucket Default Possessor | source code | |
Add Saucepan Iam Conditional Bounden | source code | |
Add Bucket Iam Member | source code | |
Add Bucket Label | source code | |
Add together Saucepan Owner | source code | |
Change Default Storage Class | source code | |
Configure Bucket Cors | source code | |
Create Saucepan | source code | |
Create Bucket Dual Region | source code | |
Create Bucket Pub Sub Notification | source code | |
Create Bucket With Storage Class And Location | source code | |
Create Saucepan With Turbo Replication | source code | |
Delete Bucket | source code | |
Delete Bucket Pub Sub Notification | source code | |
Disable Bucket Versioning | source code | |
Disable Default Event Based Hold | source code | |
Disable Lifecycle Management | source lawmaking | |
Disable Requester Pays | source lawmaking | |
Disable Uniform Bucket Level Admission | source lawmaking | |
Enable Saucepan Versioning | source lawmaking | |
Enable Default Event Based Concord | source code | |
Enable Lifecycle Management | source code | |
Enable Requester Pays | source code | |
Enable Uniform Bucket Level Access | source code | |
Get Bucket Metadata | source code | |
Become Bucket Rpo | source code | |
Get Default Issue Based Hold | source code | |
Get Public Access Prevention | source code | |
Get Requester Pays Status | source lawmaking | |
Get Retention Policy | source code | |
Get Compatible Saucepan Level Access | source lawmaking | |
List Bucket Iam Members | source code | |
List Buckets | source code | |
List Pub Sub Notifications | source code | |
Lock Retention Policy | source code | |
Brand Saucepan Public | source code | |
Print Bucket Acl | source code | |
Print Bucket Acl Filter By User | source code | |
Impress Pub Sub Notification | source code | |
Remove Bucket Cors | source code | |
Remove Bucket Default Kms Cardinal | source code | |
Remove Bucket Default Owner | source code | |
Remove Saucepan Iam Conditional Binding | source code | |
Remove Bucket Iam Member | source code | |
Remove Bucket Label | source code | |
Remove Saucepan Owner | source lawmaking | |
Remove Memory Policy | source code | |
Prepare Async Turbo Rpo | source lawmaking | |
Set Bucket Default Kms Key | source code | |
Set Bucket Website Info | source code | |
Set Client Endpoint | source lawmaking | |
Prepare Default Rpo | source code | |
Set Public Access Prevention Enforced | source code | |
Set Public Access Prevention Inherited | source code | |
Set Retention Policy | source code | |
Activate Hmac Primal | source code | |
Create Hmac Fundamental | source code | |
Conciliate Hmac Key | source code | |
Delete Hmac Fundamental | source code | |
Become Hmac Central | source lawmaking | |
List Hmac Keys | source lawmaking | |
Add File Owner | source code | |
Modify Object Csek To Kms | source lawmaking | |
Alter Object Storage Course | source code | |
Compose Object | source lawmaking | |
Copy Object | source lawmaking | |
Copy Old Version Of Object | source code | |
Delete Object | source code | |
Delete Old Version Of Object | source lawmaking | |
Download Encrypted Object | source lawmaking | |
Download Object | source code | |
Download Object Into Memory | source code | |
Download Public Object | source code | |
Download Requester Pays Object | source code | |
Generate Encryption Key | source lawmaking | |
Generate V4 Get Object Signed Url | source code | |
Generate V4 Put Object Signed Url | source lawmaking | |
Become Object Metadata | source code | |
List Objects | source code | |
Listing Objects With Old Versions | source code | |
Listing Objects With Prefix | source code | |
Make Object Public | source code | |
Move Object | source code | |
Print File Acl | source code | |
Impress File Acl For User | source lawmaking | |
Release Event Based Agree | source code | |
Release Temporary Hold | source code | |
Remove File Owner | source code | |
Rotate Object Encryption Primal | source code | |
Set Issue Based Hold | source lawmaking | |
Set Object Metadata | source code | |
Set Temporary Hold | source lawmaking | |
Stream Object Download | source code | |
Stream Object Upload | source code | |
Upload Encrypted Object | source code | |
Upload Kms Encrypted Object | source code | |
Upload Object | source code | |
Upload Object From Memory | source code |
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 | |
Java 8 OSX | |
Java 8 Windows | |
Java 11 |
Java is a registered trademark of Oracle and/or its affiliates.
Source: https://github.com/googleapis/java-storage
0 Response to "Get the Status of an Upload to Google Cloud Storage Java"
Post a Comment