Creating A Google Cloud Storage Bucket
Creating a Google Cloud Storage bucket is relatively simple. Just follow these steps:
Go to https://cloud.google.com/console and log in. You’ll see the following page. Create a project or click on an already-existing project (in this picture, Fact and Invalid are pre-existing projects).
Click on the Cloud Storage link in the next screen:
Now press the New Bucket button:
A prompt will open asking you to name your new bucket.
Your new bucket will be created. You can upload new files and folders to the bucket by clicking the Upload button in the middle of the screen:
Setting Permissions To Access Cloud Storage From App Engine
Serving URL For Images Stored In Cloud Storage
Writing Files To Google Cloud Storage
Writing a file to Google Cloud Storage is easy in Java. All you need is the Google Cloud Storage library.
The following code writes a file into GCS. Bucket represents the name of the Cloud Storage bucket, object represents the filename, mime represents the MIME type, and data represents a byte array with the contents of the file you’re writing.
String bucket = "your_bucket_name"; String object = "file_name_here_including_extension"; GcsFilename gcs_filename = new GcsFilename(bucket, object); //File Options GcsFileOptions.Builder options_builder = new GcsFileOptions.Builder(); options_builder = options_builder.mimeType(mime); GcsFileOptions options = options_builder.build(); GcsOutputChannel output = GcsServiceFactory.createGcsService().createOrReplace(gcs_filename, options); output.write(ByteBuffer.wrap(data)); output.close();
Remember to set bucket permissions in Google Cloud Storage so your app can access the files.