Giter Site home page Giter Site logo

msoftware / googlecloudstorage Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mithrandir21/googlecloudstorage

0.0 2.0 0.0 84 KB

A library for simple communication with Google Cloud Storage.

License: GNU General Public License v3.0

Java 100.00%

googlecloudstorage's Introduction

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

Add this line to your Gradle configuration:

compile 'com.bahram:gcs-library:0.0.4'

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.";
}

Min Android SDK: 8

Future Features:

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

#### Cloud Storage explanations: ##### Bucket: "Buckets are the basic containers that hold your data. Everything that you store in Google Cloud Storage must be contained in a bucket."
See more about Buckets in the link below:
https://cloud.google.com/storage/docs/overview
Credential:

All communication between client and google cloud storage requires an initial Credential.
A Credential is simply a 'key' that contains the AccountID (service account email) for the cloud storage you wish to connect to and the P12 private key for verification.
See more on how create and retrieve these in the link below:
https://developers.google.com/identity/protocols/OAuth2ServiceAccount#creatinganaccount

GoogleStorage:

A simple singleton wrapper for the Storage object referencing a single cloud storage 'Bucket'.
All functions communicating with the cloud storage will require a GoogleStorage object as a parameter.

P12 Private key:

The private key required for communication with the google cloud server. This key must be stored securely and not shared with others. There a number of ways to access your .p12 key file, but for testing purposes, it can be placed into your applications "raw" folder and accessed via "R.raw.[filename-without-extension]".
See link for more information:
http://developer.android.com/guide/topics/resources/providing-resources.html

Object Full Path:

Google Cloud Storage buckets have a flat structure. This means that there are technically no folders. Everything is basically in the root folder. The illusion of a folder structure can be achieved by adding the character '/' to an object name. This means that if an Objects name is simply "image.png" it will be in the root folder. But if the Objects name is "Holiday/Portugal/Porto/image.png" it will presented as if it is in those folders.
This means 2 things: A) You can add anything, anywhere. B) Your application needs have a specific way of providing this path.
See the example provided to see how one implementation can be. See link for further information on object paths in Cloud Storage:
https://cloud.google.com/storage/docs/gsutil/addlhelp/HowSubdirectoriesWork


#### Credential and GoogleStorage options: ##### HttpTransport: Any HttpTransport can be specified during Credential building. ```java Credential cred = CredentialBuilder.setup(context, R.raw.gcs_key, APP_CLOUD_ACCOUNT_ID) .transporter(new NetHttpTransport()) // <-- Both Credential and GoogleStorage will use this .build(); ```
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();
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;
    }
}

googlecloudstorage's People

Contributors

mithrandir21 avatar

Watchers

Michael jentsch avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.