GoogleCloudStorage


Source link: https://github.com/Mithrandir21/GoogleCloudStorage

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: 8
Add this line to your Gradle configuration:

repositories  {

jcenter() 
}

dependencies  {

compile 'com.bahram:gcs-library:0.1.+' // See Version explanation 
}

######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)

#### 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;
  
}
 
}

Resources

Compressor is a lightweight and powerful android image compression library. Compressor will allow you to compress large photos into smaller sized photos with very less or negligible loss in quality of the image.

The Asciidoctor Gradle Plugin is the official means of using Asciidoctor to convert all your AsciiDoc documentation using Gradle.

An Espresso IdlingResource for OkHttp.

Android view for displaying PDFs rendered with PdfiumAndroid.

A Series Of Badge Drawables For Android.

Twitter like toolbar indicator for Android.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes