GoogleCloudStorage

Additional

Language
Java
Version
N/A
Created
Aug 2, 2015
Updated
Feb 12, 2018 (Retired)
Owner
Bahram Malaekeh (Mithrandir21)
Contributor
Bahram Malaekeh (Mithrandir21)
1
Activity
Badge
Generate
Download
Source code

Announcement

GoogleCloudStorage

A library for simple communication with Google Cloud Storage in Android.

Features:

  • Simple CRUD (Create, Read, Update and Delete) functions for Bitmaps.
  • Simple Builder pattern for credentials and bucket storage.
  • Custom or standard JSON factory and HTTP transporter.
  • Easy to debug with existing, simple to understand debug log.

Setup:

There are 2 things necessary to work with Google Cloud Storage:

  • An AccountID from the Google Developer Console. Get AccountID
  • A Public/Private P12 key from the Google Developer Console. Create P12 Key

Min Android SDK: 14 Add this line to your Gradle configuration:

repositories 
{
  jcenter()
}
    
dependencies 
{
  compile 'com.bahram:gcs-library:0.1.+'
}

######Version explanation: Odd numbers - Bug fixes. No new features, safe to update. Even numbers - New feature implementations. Any depreciations or serious changes will be informed.


Usage:

Bitmap image = [any bitmap, with any image];
public static final String APP_CLOUD_BUCKET_NAME = "[BUCKET_NAME]";
public static final String APP_CLOUD_ACCOUNT_ID = "[service_accountID]@developer.gserviceaccount.com";
public static String IMAGE_FULL_PATH = "any/path/for/the/image.webp"; // See Object Full Path explanation.


// Build the Credentials needed for communication (Private P12 key as 'R.raw.gcs_key").
Credential cred = CredentialBuilder.setup(context, R.raw.gcs_key, APP_CLOUD_ACCOUNT_ID)
                  .build();

// Get the GoogleStorage instance need for any cloud storage action.
GoogleStorage gStorage = GoogleStorage.build(APP_CLOUD_BUCKET_NAME, cred);

// Insert Image (inside the Bitmap) to the given fullpath and store as given format.
if( CloudImageCRUD.insertCloudImage(googleStorage, fullPath, image, format) )
{
  Log.d(TAG, "Oh Happy Day! Image has been stored.";
}

Future Features:

  • Search functions
  • Listing functions
  • Auto-Scaling function for retrieved images (avoid OutOfMemory exceptions)



JsonFactory:

Any JsonFactory can be specified during Credential building.

Credential cred = CredentialBuilder.setup(context, R.raw.gcs_key, APP_CLOUD_ACCOUNT_ID)
                  .transporter(new JacksonFactory()) // <-- Both Credential and GoogleStorage will use this
                  .build();

OR

Credential cred = CredentialBuilder.setup(context, R.raw.gcs_key, APP_CLOUD_ACCOUNT_ID)
                  .transporter(new JacksonFactory()) // <-- Both Credential and GoogleStorage will use this
                  .build();
Scope:

The Credentials can specific what scope it should have (ENUM CredentialScope).

Credential cred = CredentialBuilder.setup(context, R.raw.gcs_key, APP_CLOUD_ACCOUNT_ID)
                  .scope(CredentialScope.DEVSTORAGE_READ_ONLY) // <-- Credential will use this
                  .build();
Full Path example:

Example of full path construction. (Simply an example, can be implemented any other way.)

public static String constructCloudObjectUri(String userDB_ID, MediaCategory category,
String folderID, ImageSize imageSize, String imageID, SupportedImageFormats format)
{
    if( folderID != null && folderID.length() > 0 )
    {
        return userDB_ID + "/" + category + "/" + folderID 
                + "/" + imageSize + "/" + imageID + "." + format;
    }
    else
    {
        return userDB_ID + "/" + category + "/" + imageSize 
                + "/" + imageID + "." + format;
    }
}