GithubHelp home page GithubHelp logo

kusium / simple-cloud-storage Goto Github PK

View Code? Open in Web Editor NEW

This project forked from pliablematter/simple-cloud-storage

0.0 2.0 0.0 386 KB

A simple Java wrapper around the Google Cloud Storage API

simple-cloud-storage's Introduction

simple-cloud-storage

This library provides 1 line access to common Google Cloud Storage functions. It also provides examples of accessing Google Cloud Storage via the Google APIs Client Library for Java (see the source).

To get started, you'll need to enable Google Cloud Storage on a project and enable authentication via a Service Account.

Configuration

Creating Credentials

(This process is a little cumbersome, but it gets easier from here)

Step 1: From the Google Cloud Console, click the "APIs" link

APIs Link

Step 2: Make sure "All" is selected, then click the toggle switch to enable "Google Cloud Storage JSON API"

Toggle

Step 3: Click the Gear next to "Google Cloud Storage JSON API" to access the Google APIs configuration page

Toggle

Step 4: In the left menu click "Overview" and note the "Project Number" (needed for later configuration)

Project Number

Step 5: In the left menu, select "API Access"

API Access

Step 6: Under "Client ID for Google Compute and App Engine", select "Create another Client ID..."

Create Another Client ID

Step 7: Select "Service Account", then click "Create client ID"

Create Another Client ID

Step 8: Click the "Download private key" button to save the private key (path needed for later configuration)

Download Private Key

Step 9: Under Service Account, note the "Client ID" (needed for later configuration)

Download Private Key

Defining Properties

The credentials created above now need to be added to a properties file so that the library can access your Google Cloud Storage account

  1. Copy src/main/resources/sample-cloudstorage.properties to a new file named cloudstorage.properties
  2. Edit the file to use the Project ID, Account ID, and Private Key Path noted above. The Application Name can be anything that makes sense to you (I use the Project Name)

NOTE: cloudstorage.properties needs to be in the root of the classpath. If you leave it in src/main/resources, it will work, but the credentials will be embedded in your jar. Just something to be aware of since there are possible security concerns.

Building

To build the jar you will need to have maven installed. Once that's done, just cd to the project directory and run:

mvn clean install

This will run the unit tests, which will verify that credentials that you configured above are correct.

Once the build is complete the compiled jar can be found in the target directory.

Usage

(Finally to the easy part)

package com.pliablematter.cloudstorage;

import java.util.List;

public class Demo {

	public static void main(String[] args) throws Exception {
		
		CloudStorage.createBucket("my-bucket");
		
		CloudStorage.uploadFile("my-bucket", "/var/uploads/some-file.txt");
		
		CloudStorage.downloadFile("my-bucket", "some-file.txt", "/var/downloads");
		
		List<String> buckets = CloudStorage.listBuckets();
		
		List<String> files = CloudStorage.listBucket("my-bucket");

	}
}

simple-cloud-storage's People

Contributors

dwburns avatar

Watchers

James Cloos 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.